C++ primer 第四版课后习题部分答案 -- Cap 10

  • 10.25  定义一个 vector 容器,存储你在未来六个月里要阅读的书,再定义一个set,用于记录你已经看过的书名。编写程序从 vector 中为你选择一本没有读过而现在要读的书。当它为你返回选中的书名后,应该将该书名放入记录已读书目的 set 中。如果实际上你把这本书放在一边没有看,则本程序应该支持从已读数目的set中删除该书的记录。在虚拟的六个月后,输出已读数目和还没有读的书目。

 

// 10-25
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cstdlib>
#include <ctime>
#include <cctype>
using namespace std;

int main() {
	// create vector: strVecBook to store all the books
	vector<string> strVecBook;
	string bName;
	cout << "Input book's name you wanna read:\n";
	while (getline(cin, bName)) {
		if (bName == "q")  break;
		strVecBook.push_back(bName);
		cout << "Input book's name you wanna read (q to quit):\n";
	}
	vector<string>::size_type numOfBooks = strVecBook.size();      // record number of books

	// create set: strSetReadedBook
	set<string> strSetReadedBook;
	string strChoice, bookName;
	bool _6MonthLater = false;
	// use system's time for seed of rand
	srand((unsigned)time(0));
	while (!_6MonthLater && !strVecBook.empty()) {
		cin.clear(); 
		cout << "==>> Do you wanna read a book ? (Yes or No): ";
		cin >> strChoice;
		if (toupper(strChoice[0]) == 'Y') {
			vector<string>::size_type i = rand() % strVecBook.size();
			bookName = strVecBook[i];
			cout << "==>> We choice this book for you: " << bookName << endl;
			
			strSetReadedBook.insert(bookName);
			strVecBook.erase(strVecBook.begin() + i);
			cout << "\tOne month later ... ..." << endl;
			cin.clear();

			cout << "==>> Did you read this book? (Yes or No): ";
			cin >> strChoice;
			if (toupper(strChoice[0]) == 'N') {
				// delete the book from strSetReadedBook and add to strVecBook
				strSetReadedBook.erase(bookName);
				strVecBook.push_back(bookName);
			}
		}

		// 6 month later?
		cin.clear();
		cout << "6 month later? (Yes or No): ";
		cin >> strChoice;
		if (toupper(strChoice[0]) == 'Y')
			_6MonthLater = true;
	}

	if (_6MonthLater) {
		if (!strSetReadedBook.empty()) {
			cout << "==>> During the latest 6 months, you have readed: \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 iter = strVecBook.begin(); iter != strVecBook.end(); iter++)
				cout << *iter << "   ";
		}
		cout << endl;
	}

	if (strSetReadedBook.size() == numOfBooks)
		cout << "\tGood, you have readed all the books." << endl;

	return 0;
}
  • 10.28  运用multimap容器,编写程序以下面的格式按姓名首字母的顺序输出作者名字:
  • Author Names Beginning with 'A':
  • Author, book, book,...
  • ...
  • Author Names Beginning with 'B':
  • ...
// 10-28
#include <iostream>
#include <map>
#include <utility>
#include <string>
using namespace std;

int main() {
	multimap<string, string> mmapAuthor;
	string author, book;
	
	cin.clear();
	cout << "==>>Input Author and his Book: ";
	while (getline(cin, author) && getline(cin, book)) {
		if (author == "q") break;
		mmapAuthor.insert(make_pair(author, book));
		cout << "==>>Input Author and his Book: (q to quit)";
	}

	typedef multimap<string, string>::iterator author_it;
	author_it iter = mmapAuthor.begin();
	if (iter == mmapAuthor.end()) {
		cout << "\n\tEmpty multimap!" << endl;
		return 0;
	}

	string currAuthor, preAuthor;
	do {
		currAuthor = iter->first;
		if (preAuthor.empty() || currAuthor[0] != preAuthor[0]) {
			cout << "Author Names Beginning with '" << iter->first[0] << "': " << endl;
		}
		cout << currAuthor;
		pair<author_it, author_it> pos = mmapAuthor.equal_range(iter->first);
		while (pos.first != pos.second) {
			cout << ", " << pos.first->second;
			++pos.first;
		}
		cout << endl;
		iter = pos.second;
		preAuthor = currAuthor;
	} while (iter != mmapAuthor.end());
	cout << endl;

	return 0;
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值