UVA 230

来源:互联网 发布:wap站长之家源码 编辑:程序博客网 时间:2024/05/01 17:00

关于map, string, vector的综合运用
其中关于compare函数的写法和知识点要好好学学!!

#include<iostream>#include<map>#include<vector>#include<string>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;class Book{public:    string author;    int status;};map<string, Book> Books; //map<键,值>vector<string> name;////bool compare(string &a, string &b) //传进来的是不同的书名,所以可以通过书名去检索//{//    if(Books[a].author < Books[b].author) return a< b;//    else//    {//        if(Books[a].author == Books[b].author && a < b) return a < b;//    }//}//自己写的这个不行,改一下bool compare(string a, string b){    if(Books[a].author == Books[b].author) return a < b;    else return Books[a].author < Books[b].author;}int main(){    string s, bookname, bookauthor;    Book b;   // while(cin >> s) 注意不要用cin cin 遇到空格就停了, 输入名字时会很费事,所以用getline    while(getline(cin, s))    {        if(s == "END") break;        bookname = s.substr(0, s.find_last_of("\"")+1);        b.author = s.substr(s.find_last_of("\"")+1);        name.push_back(bookname);        Books[bookname] = b;    }    sort(name.begin(), name.end(), compare);    for(int i = 0; i < name.size(); i++) //原来vector也可以取size        Books[name[i]].status = 1;    string x, y, z;    while(cin >> x)    {        if(x == "END") break;        else if(x == "BORROW") {            getchar();          getline(cin , y); // //wowo getline(cin, s)好好用,你直接输入即可,也不须担心空格的问题,他直接都给原封不动的弄到s里面了        Books[y].status = 0;        }        if(x == "RETURN") {            getchar();            getline(cin, y);            Books[y].status = -1;        }        if(x == "SHELVE") {            for(int i = 0; i < name.size(); i++){                //if(Books[name[i]].status == 1) cout << "##########" << endl;                //if(Books[name[i]].status == 0) cout << "$$$$$$$$$$$" << endl;                if(Books[name[i]].status == -1) {                    int j;                    for(j = i; j >= 0; j--){                        if(Books[name[j]].status == 1)                         { break; }                    }                    cout << "Put " << name[i];                    if(j == -1) cout << " first" << endl;                    else  cout << " after " << name[j] << endl;                    Books[name[i]].status = 1;                }            }                cout << "END" << endl;;        }    }    return 0;}
0 0
原创粉丝点击