vector、set举例BOOK

/*
定义一个 vector 的容器,存储你在未来六个月里要阅读的书,再定义一个 set,用于记录你已经看过的书名。
编写程序从 vector 中为你选择一本没有读过而现在要读的书。当它为你返回选中的书名后,应该将该书名放
入记录已读书目的 set 中。如果实际上你把这本书放在一边没有看,则本程序应该支持从已读书目的 set 中删
除该书的记录。在虚拟的六个月后,输出已读书目和还没有读的书目。
*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
// 1   create vector:bookVec
vector<string> strVecBook;
string bName;
cout <<   " Input books' name you wanna read ( ctrl+z to end):\n";
while ( cin >> bName )//vector 所有书名输入
strVecBook.push_back( bName );

//所有书名长度,即共有几本书
size_t NumOfBooks = strVecBook.size();  // record number of books

// 2 create set:strSetReadedBook
set<string> strSetReadedBook;
string strChoice, bookName;

bool _6MonthLater = false;
// use system's time for seed of rand
srand ( ( unsigned ) time(NULL) );

while ( !_6MonthLater&& !strVecBook.empty() )
{
cin.clear();
cout << "\n ==>>Do you wanna read a book? ( Yes / No ): ";
cin >> strChoice;
if ( strChoice[0] == 'Y' || strChoice[0] == 'y' )//要读一本书
{
int i = rand() % strVecBook.size();//随机选择的书
bookName = strVecBook[i];
cout << " =>> We choose this book for you: " << bookName << endl;

strSetReadedBook.insert( bookName );
strVecBook.erase( strVecBook.begin() + i );//从总表中删除了原先那本书

cout << "\tOne month later ... ... " << endl<< " Did you read this book? ( Yes/ No ): ";//是否读过
//cin.clear();
cin >> strChoice;
if ( strChoice[0] == 'n' || strChoice[0] == 'N' )
{
// delete the book from strSetReadedBook    and add to strVecBook
strSetReadedBook.erase( bookName );//
strVecBook.push_back( bookName );
}
}
// 6 month later ?
cout << "   6 month later ? ( Yes / No ): ";
//cin.clear();
cin >> strChoice;

if ( strChoice[0] == 'Y' || strChoice[0] == 'y' )
_6MonthLater = true;
}
if ( _6MonthLater )
{
if ( !strSetReadedBook.empty() ) 
{
cout << "\n ==>>>During the latest 6 months , you read: \n\t";
for ( set<string>::iterator iter = strSetReadedBook.begin(); iter != strSetReadedBook.end(); ++iter )
{
cout << *iter << "   ";
}
}
if ( !strVecBook.empty() )
{
cout << "\n ==>>>The books you have not read : \n\t";
for ( vector<string>::iterator it = strVecBook.begin(); it != strVecBook.end(); ++it )
{
cout << *it << "   ";
}
}
cout << endl;
}
if ( strSetReadedBook.size() == NumOfBooks )
cout << "\t Good, you read all the books. " << endl;
system("pause");
return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值