Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

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

$
0
0
#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

作者:DiffenYu 发表于2013-3-17 22:48:07 原文链接
阅读:62 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>