// CH1608.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include#include #include using namespace std;struct Review{ std::string title; int rating;};bool FillReview(Review &rr); //输入Review对象void ShowReview(const Review &rr); //输出Review对象int _tmain(int argc, _TCHAR* argv[]){ vector books; Review temp; while( FillReview(temp) ) books.push_back(temp); int num=books.size(); if(num>0) { cout<<"Thank you ,you entered the following:\n" <<"Rating\tBook\n"; for(int i=0;i ::iterator pr; for(pr=books.begin();pr!=books.end();pr++) ShowReview(*pr); vector oldlist(books); //copy constructor used if(num>3) { //remove 2 items 移除两项 books.erase(books.begin()+1,books.begin()+3); cout<<"After erasure:\n"; for(pr=books.begin();pr!=books.end();pr++) ShowReview(*pr); //insert 1 items插入一项数据 books.insert(books.begin(),oldlist.begin()+1,oldlist.begin()+2); cout<<"After insert:\n"; for(pr=books.begin();pr!=books.end();pr++) ShowReview(*pr); } books.swap(oldlist); //交换两个容器的内容 cout<<"swapping oldlist with boos:\n"; for(pr=books.begin();pr!=books.end();pr++) { ShowReview(*pr); } }//end if(num>0) else { cout<<"Nothing entered,nothing gained.\n"; } return 0;}bool FillReview(Review & rr){ cout<<"Enter book title(quit to quit):"; getline(cin,rr.title); if(rr.title == "quit") { return false; } cout<<"Enter book rating:"; cin>>rr.rating; if(!cin) { return false; } while(cin.get() != '\n') continue; return true;}void ShowReview(const Review &rr){ cout< <<"\t"< <
运行效果如下