c++primer(十)关联容器

习题10.25的详细解答

#include"stdafx.h"
#include<iostream>
#include<set>
#include<vector>
#include<utility>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
    vector<string> strVecBook;
    string bName;
    string strChoice;
    cout<<"input the book you want to read:\n";
    while(cin>>bName)
        strVecBook.push_back(bName);
    size_t NumOfBook=strVecBook.size();
    set<string>strReadBook;
    string bookName;
    srand((unsigned)time(NULL));
    bool _6MounthLater=false;
    while(!_6MounthLater&&!strVecBook.empty())           //边界条件
    {
        cin.clear();
        cout<<"Do you want to read the book? (Yes/No)\n"<<endl;
        cin>>strChoice;
        if(strChoice[0]=='Y'||strChoice[0]=='y')
        {
            int i=rand()%strVecBook.size();
            bookName=strVecBook[i];
            cout<<"we choose this book\t"<<bookName<<endl;
            strReadBook.insert(bookName);
            strVecBook.erase(strVecBook.begin()+i);
            cout<<"after some time,do you really read it?(Yes/No)\n"<<endl;
            cin>>strChoice;
            if(strChoice[0]=='N'||strChoice[0]=='n')
            {
                strVecBook.push_back(bookName);
                strReadBook.erase(bookName);
            }
            cout<<"6 mouth later?"<<endl;
            cin>>strChoice;
            if(strChoice[0]=='Y'||strChoice[0]=='y')
                _6MounthLater=true;
        }
        cout<<"6 mouth later"<<endl;
        if(_6MounthLater)
        {
            cout<<"the book you have read :\n";
            for(set<string>::iterator it=strReadBook.begin();it!=strReadBook.end();++it)
                cout<<*it<<"  ";
            cout<<endl;
            cout<<"the book you have not read:\n";
            for(vector<string>::iterator iter=strVecBook.begin();iter!=strVecBook.end();++iter)
                cout<<*iter<<"   ";
        }
    }   
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值