标签:容器 push ret lse .com name continue src iterator
// CH1608.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
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<Review> 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<num;i++)
{
ShowReview(books[i]);
}
cout<<"Reprising:\n"
<<"Rating\tBook\n";
vector<Review>::iterator pr;
for(pr=books.begin();pr!=books.end();pr++)
ShowReview(*pr);
vector <Review>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<<rr.rating<<"\t"<<rr.title<<endl;
}
运行效果如下
标签:容器 push ret lse .com name continue src iterator
原文地址:https://www.cnblogs.com/Manual-Linux/p/9543470.html