不一定真的插入删除才能完成模拟过程。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
struct book{
string name,author;
int tag;
book(const string & n ="n",const string & a="a",int t = 1):name(n),author(a),tag(t){}
bool operator<(const book & b)const{
if(author != b.author)return author < b.author;
return name < b.name;
}
};
int main(){
string s,name,author,cmd;
vector<book> books;
map<string,int> mmap;
while(getline(cin,s) && s != "END"){
name = s.substr(0,s.find_last_of('\"')+1);
author = s.substr(s.find_last_of('\"')+1);
books.push_back(book(name,author));
}
sort(books.begin(),books.end());
for(int i=0;i<books.size();i++){
mmap[books[i].name]=i;
}
while(cin>>cmd && cmd != "END"){
if(cmd == "BORROW"){
getchar();
getline(cin,name);
books[mmap[name]].tag = -1;
}else if(cmd =="RETURN"){
getchar();
getline(cin,name);
books[mmap[name]].tag = 0;
}else if(cmd =="SHELVE"){
for(int i=0;i<books.size();i++){
if(books[i].tag == 0){
int j;
for(j=i-1;j>=0;j--){
if(books[j].tag == 1)break;
}
if(j >= 0)printf("Put %s after %s\n",books[i].name.c_str(),books[j].name.c_str());
else printf("Put %s first\n",books[i].name.c_str());
books[i].tag = 1;
}
}
cout<<"END\n";
}
}
system("pause");
return 0;
}