码迷,mamicode.com
首页 > 其他好文 > 详细

P678-vect2.cpp

时间:2018-08-31 21:12:52      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:容器   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;
}

运行效果如下
技术分享图片

P678-vect2.cpp

标签:容器   push   ret   lse   .com   name   continue   src   iterator   

原文地址:https://www.cnblogs.com/Manual-Linux/p/9543470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!