UVA 230

关于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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值