您的位置:首页 > 编程语言 > C语言/C++

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

2013-03-17 22:48 375 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: