main169.cpp
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <cctype> #include <memory> using namespace std; static struct Review{ string title; int rating; double price; }; static bool sortT(const Review &r1,const Review &r2) { if(r1.title<r2.title) return true; else return false; }; static bool sortR(const Review &r1,const Review &r2) { if(r1.rating<r2.rating) return true; else return false; }; static bool sortP(const Review &r1,const Review &r2) { if(r1.price<r2.price) return true; else return false; }; //static bool FillReview(shared_ptr<Review> *rr) static bool FillReview(Review &rr) { //rr=new Review; shared_ptr<Review> ps(new Review);//创建一个shared_ptr指针 cout<<"\nEnter book title (q to quit):"; getline(cin,ps->title); //if(rr.title[0]=='q'&&sizeof(rr.title)==1)//这里是ps好吧,我就奇了怪了 if(ps->title=="q") return false; cout<<"\nEnter the book rating:"; cin>>ps->rating; cout<<"\nEnter the price:"; cin>>ps->price; if(!cin) return false; while(cin.get()!='\n') continue; rr=*ps;//p667 ps是一个指针,对其解除* return true; }; static void output(const Review &rr) { cout<<rr.rating<<"\t"<<rr.title<<"\t"<<rr.price<<endl; } void main1610() { //vector<shared_ptr<Review> > books; vector<Review> books; //shared_ptr<Review> *temp; Review temp; while(FillReview(temp)) books.push_back(temp); if(books.size()>0) { cout<<"Thank you ,You entered the following " <<books.size()<<" ratings:"<<endl <<"Rating\tBook\tPrices"<<endl; for_each(books.begin(),books.end(),output); } cout<<"Please select the sort:"<<endl <<"t.sort by title r.sort by rating"<<endl <<"p.sort by price f.sort by title reserve"<<endl <<"q.quit"<<endl;//我只演示一个逆序输出f char select; cin>>select; while(select!='q') { /* while(cin.get()!='\n') continue;*/ switch(select) { case 't': sort(books.begin(),books.end(),sortT), cout<<"sort by title:"<<endl; for_each(books.begin(),books.end(),output), cout<<endl; break; case 'f': sort(books.begin(),books.end(),sortT), cout<<"sort by title:"<<endl; //for_each(books.rend(),books.rbegin(),output), for_each(books.rbegin(),books.rend(),output),//这是关键 cout<<endl; break; case 'r': sort(books.begin(),books.end(),sortR), cout<<"sort by Rating:"<<endl; for_each(books.begin(),books.end(),output), cout<<endl; break; case 'p': sort(books.begin(),books.end(),sortP), cout<<"sort by prices:"<<endl; for_each(books.begin(),books.end(),output), cout<<endl; break; case 'q': default: break; } cout<<"Please select the sort:"<<endl <<"t.sort by title r.sort by rating"<<endl <<"p.sort by price f.sort by title reserve"<<endl <<"q.quit"<<endl;//我只演示一个逆序输出f cin>>select; } cout<<"Bye"<<endl; cin.get(); }
——————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:https://www.royalchen.com/
author:royalchen
Email:royalchen@royalchen.com
———————————————————————————————————————————————————