#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;

struct R{
   string title;
   int rating;
};
bool operator<(const R&r1,const R&r2);
bool worsethan(const R&r1,const R&r2);
bool fillR(R& rr);
void showR(const R& rr);
void main(){
vector<R>books;
R temp;
while(fillR(temp))
   books.push_back(temp);
cout<<"thank you you entered the following"<<books.size()<<"rating:\n"<<"rating\tbook\n";
for_each(books.begin(),books.end(),showR);

sort(books.begin(),books.end());
cout<<"sorted bu title:\nrating\tbook\n";
for_each(books.begin(),books.end(),showR);

sort(books.begin(),books.end(),worsethan);
cout<<"sorted by rating:\nrating\tbook\n";
for_each(books.begin(),books.end(),showR);

random_shuffle(books.begin(),books.end());
cout<<"after shuffling:\nrating\tbook\n";
for_each(books.begin(),books.end(),showR);
cout<<"Bye.\n";

}

bool operator<(const R&r1,const R&r2){
if(r1.title<r2.title)
return true;
else if(r1.title==r2.title&&r1.rating<r2.rating)
return true;
else
 return false;
}
bool worsethan(const R&r1,const R&r2){
if(r1.rating<r2.rating)
return true;
else
return false;
}
bool fillR(R& rr){
cout<<"enter book title(quit to quit):"<<endl;
getline(cin,rr.title);
if(rr.title=="quit")return false;
cout<<"enter book ratingg:";
cin>>rr.rating;
if(!cin)return false;
  cin.get();
return true;
}
void showR(const R & rr)
{
cout<<rr.rating<<"\t"<<rr.title<<endl;
}

posted on 2013-02-23 15:10  叶城宇  阅读(162)  评论(0编辑  收藏  举报