C++ Primer Chapter 10 关联容器之set & mutimap

#if 0
//Chapter 10 Exercise 10.24
//此程序在dos下输入test.exe exfile.txt
//exfile.txt 中存放的是排除集单词
#include <iostream>
#include <set>
#include <string>
#include <fstream>

using namespace std;

ifstream& open_file(ifstream& in, const string& file)
{
	in.close();
	in.clear();
	in.open(file.c_str());//由于历史原因呢,IO标准库使用的C风格字符串而不是C++string类型的字符串作为文件名
	return in;
}
int main(int argc, char **argv)
{
	if (argc != 2)
	{
		throw runtime_error("input error");
	}
	ifstream excludedfile;
	if (!open_file(excludedfile, argv[1]))
	{
		throw runtime_error("no input file");
	}
	set<string> excluded;
	string excluded_word;
	while (excludedfile >> excluded_word)
	{
		excluded.insert(excluded_word);
	}
	string word;
	while(cin >> word)
	{
		if (excluded.count(word) || *(word.end() - 1) != 's')
		{
			cout << word << endl;
		}
		else
		{
			word.resize(word.size() - 1);
			cout << word << endl;
		}			
	}
}
#endif

#if 0
//Chapter 10 Exercise 10.25
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
	vector<string> books;
	set<string> readedbooks;
	
	cout << "Enter the books you want to read(Ctrl + z to end)" << endl;
	string name;
	while (getline(cin, name))//一行一个书名
	{
		books.push_back(name);
	}
	cin.clear();

	srand((unsigned)time(NULL));
	bool timeOver = false;
	string answer, bookname;
	while (!timeOver && !books.empty())
	{
		cout << "Would you like to read a book? (Yes/No)" << endl;
		cin >> answer;
		if (answer[0] == 'Y' || answer[0] == 'y')
		{
			int i = rand() % books.size();
			bookname = books[i];
			cout << "You can read this book:\t" << bookname << endl;
			books.erase(books.begin() + i);
			readedbooks.insert(bookname);
			
			cout << "Did you read it?(Yes/No)" << endl;
			cin >> answer;
			if (answer[0] == 'N' || answer[0] == 'n')
			{
				readedbooks.erase(bookname);
				books.push_back(bookname);
			}
		}
		cout << "Time over?(Yes/No)" << endl;
		cin >> answer;
		if (answer[0] == 'Y' || answer[0] == 'y')
		{
			timeOver = true;
		}
	}
	if (timeOver)
	{
		cout << "books read: " << endl;
		for (set<string>::iterator iter = readedbooks.begin(); iter != readedbooks.end(); ++iter)
		{
			cout << *iter << endl;
		}
		cout << "books not read: " << endl;
		for (vector<string>::iterator iter = books.begin(); iter != books.end(); ++iter)
		{
			cout << *iter << endl;
		}
	}
	if (books.empty())
	{
		cout << "Congratulations! You have read all these books." << endl;
	}
	system("pause");
	return 0;
}
#endif

#if 0
//Chapter 10 Exercise 10.26
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main()
{
	multimap<string, string> author;
	string name, book;
	do 
	{
		cout << "Enter author's name(Ctrl + z to end)" << endl;
		cin >> name;
		if (!cin)
		{
			break;
		}		
		cout << "Enter author's works(Ctrl + z to end)" << endl;
		while(cin >> book)
		{
			author.insert(make_pair(name, book));
		}
		cin.clear();
	} while (cin);
	cin.clear();
	cout << "Enter the author you want to erase: " << endl;
	string searchauthor;
	cin >> searchauthor;
	multimap<string, string>::iterator iter = author.find(searchauthor);
	if (iter != author.end())
	{
		author.erase(searchauthor);
	}
	else
		cout << "Can not find this author" << endl;
	cout << "author\t\tbook:" << endl;
	for (iter = author.begin(); iter != author.end(); ++iter)
	{
		 cout << iter->first << "\t\t" << iter->second << endl;
	}
	
	return 0;
}
#endif

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值