C++ Primer 第十一章编程答案

//11.3
#include<iostream>
#include<fstream>
#include<string>
#include<map>

using namespace std;

int main(int argc,char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
		cerr << "The file open erro" << endl;
	map<string, size_t> word_count;
	string word;
	while (in >> word)
		++word_count[word];//提取word的计数器并将其加1
	for (auto &tem : word_count)
		cout << tem.first << " appear " << tem.second << ((tem.second > 1) ? " times" : " time") << endl;
	return 0;
}
//11.4
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<map>

using namespace std;

string &trans(string &s)
{
	for (int i = 0;i < s.size();i++)
	{
		if (s[i] >= 'A' &&s[i] <= 'Z')
			s[i] += 'a' - 'A';
		else if (s[i] == '.' || s[i] == ',')
			s.erase(i, 1);
	}
	return s;
}

int main(int argc,char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
		cerr << "The file open erro" << endl;
	map<string, size_t> word_count;
	string word;
	while (in >> word)
		++word_count[trans(word)];//提取word的计数器并将其加1
	for (auto &tem : word_count)
		cout << tem.first << " appear " << tem.second << ((tem.second > 1) ? " times" : " time") << endl;
	return 0;
}
//11.7
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>

using namespace std;
void add_family(map < string, vector<string>> &families, const string &family)
{
	if (families.find(family) == families.end())
		families[family] == vector<string>();//空的string给vector<string>
}
void add_child(map<string, vector<string>> &families, const string &family, const string &child )
{
	families[family].push_back(child);
}
int main()
{
	map<string, vector<string>>  families;
	add_family(families, "Sun");
	add_child(families, "Sun", "Wukong");
	add_child(families, "Sun", "Wufan");

	for (auto f : families)
	{
		cout << f.first << "家的孩子:";
		for (auto c : f.second)
			cout << c << " ";
		cout << endl;
	}
	return 0;
}
//11.8
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<vector>

using namespace std;

string &trans(string &s)
{
	for (int i = 0;i < s.size();i++)
	{
		if (s[i] >= 'A' &&s[i] <= 'Z')
			s[i] += 'a' - 'A';
		else if (s[i] == '.' || s[i] == ',')
			s.erase(i, 1);
	}
	return s;
}

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
		cerr << "The file open erro" << endl;
	vector<string> unique_word;
	string word;
	while (in >> word)
	{
		trans(word);
		if (find(unique_word.begin(), unique_word.end(), word) == unique_word.end())//提取word的计数器并将其加1
			unique_word.push_back(word);
	}
		
	for (auto tem : unique_word)
		cout << tem<< " ";
	cout<<endl;
	return 0;
}

 

//11.9
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<algorithm>
#include<list>
#include<map>

using namespace std;

string &trans(string &s)
{
	for (int i = 0;i < s.size();i++)
	{
		if (s[i] >= 'A' &&s[i] <= 'Z')
			s[i] += 'a' - 'A';
		else if (s[i] == '.' || s[i] == ',')
			s.erase(i, 1);
	}
	return s;
}

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
		cerr << "The file open erro" << endl;
	map<string, list<int>> words_line;
	string line,word;
	int line_num = 0;
	while (getline(in,line))
	{
		line_num++;
		istringstream iss(line);
		while (iss >> word)
		{
			trans(word);
			words_line[word].push_back(line_num);
		}
	}
		
	for (auto &tem : words_line)
	{
		cout << tem.first << "所在行:";
		for (auto &w : tem.second)
			cout << w << " ";
		cout << endl;
	}
	return 0;
}

 

//11.12
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<utility>

using namespace std;

int main(int argc,char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
		cerr << "The file open erro" << endl;
	vector<pair<string, int>> vec;
	string s;
	int v;
	while (in >> s && in >> v)
		vec.push_back(make_pair(s,v));
	for (auto tem : vec)
		cout << tem.first << " " << tem.second << endl;
	return 0;
}


 

//11.14
#include<iostream>
#include<vector>
#include<utility>
#include<map>
#include<string>
#include<algorithm>

using namespace std;
void add_family(map < string, vector<pair<string, string >>> &families, const string &family)
{
	if (families.find(family) == families.end())
		families[family] == vector<pair<string, string >>();//空的string给vector<pair<string, string >>()
}
void add_child(map < string, vector<pair<string, string >>> &families, const string &family, const string &child, const string &birthday)
{
	families[family].push_back({ child,birthday });
}
int main()
{
	map < string, vector<pair<string,string >>> families;
	add_family(families, "Sun");
	add_child(families, "Sun", "Wukong","19980908");
	add_child(families, "Sun", "Wufan", "19880705");

	for (auto f : families)
	{
		cout << f.first << "家的孩子:";
		for (auto c : f.second)
			cout << c.first << " " << c.second << " ";
		cout << endl;
	}
	return 0;
}
//11.20
#include<iostream>
#include<fstream>
#include<string>
#include<map>

using namespace std;

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
		cerr << "The file open erro" << endl;
	map<string, size_t> word_count;
	string word;
	while (in >> word)
	{
		auto ret = word_count.insert({ word,1 });
		if(!ret.second)
			++ret.first->second;
	}
	for (auto &tem : word_count)
		cout << tem.first << " appear " << tem.second << ((tem.second > 1) ? " times" : " time") << endl;
	return 0;
}
//11.23
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>

using namespace std;

void add_child(multimap<string, string> &families, const string &family, const string &child)
{
	families.insert({family,child});
}
int main()
{
	multimap<string, string>  families;
	add_child(families, "Sun", "Wukong");
	add_child(families, "Sun", "Wufan");

	for (auto f : families)
	{
		cout << f.first << "家的孩子:" << f.second;
		cout << endl;
	}
	return 0;
}
//11.31
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>

using namespace std;

void remove_author(multimap<string, string> &books, const string &author)
{
	auto pos = books.equal_range(author);
	if (pos.first == pos.second)
		cout << "没有此作者" << endl;
	books.erase(pos.first, pos.second);
}

void print_books(multimap<string, string> &books)
{
	for (auto &book : books)
	{
		cout << book.first << "的书《" << book.second << "》" << endl;
	}
}

int main()
{
	multimap<string, string> mulm;
	mulm.insert({ "金庸","倚天屠龙记" });
	mulm.insert({ "金庸","射雕英雄传" });
	mulm.insert({ "古龙","碧血剑" });
	mulm.insert({ "古龙","浣花洗剑录" });

	print_books(mulm);
	remove_author(mulm, "王国维");

	remove_author(mulm, "金庸");
	print_books(mulm);

	return 0;
}
//11.31
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#include<stdexcept>

using namespace std;

map<string, string> buildMap(ifstream &map_file)
{
	map<string, string> trans_map;
	string key;
	string value;
	while (map_file >> key && getline(map_file, value))
		if (value.size() > 1)
			trans_map[key] = value.substr(1);
		else
			throw runtime_error("no rule for" + key);
	return trans_map;
}

const string transform(const string &s, const map<string, string> &m)
{
	auto map_it = m.find(s);
	if (map_it != m.end())
		return map_it->second;
	else
		return s;
}

void word_transform(ifstream &map_file, ifstream &input)
{
	auto trans_map = buildMap(map_file);
	string text;
	while (getline(input, text))
	{
		istringstream stream(text);
		string word;
		bool firstword = true;
		while (stream >> word)
		{
			if (firstword)
				firstword = false;
			else
				cout << " ";
			cout << transform(word, trans_map);
		}
		cout << endl;
	}
}
int main(int argc,char *argv[])
{
	ifstream map_file(argv[1]), input(argv[2]);
	if (!map_file || !input)
		cout << "The file open erro" << endl;
	word_transform(map_file, input);
	return 0;
}
//11.38.1
#include<iostream>
#include<fstream>
#include<string>
#include<unordered_map>

using namespace std;

int main(int argc, char *argv[])
{
	ifstream in(argv[1]);
	if (!in)
		cerr << "The file open erro" << endl;
	unordered_map<string, size_t> word_count;
	string word;
	while (in >> word)
		++word_count[word];//提取word的计数器并将其加1
	for (auto &tem : word_count)
		cout << tem.first << " appear " << tem.second << ((tem.second > 1) ? " times" : " time") << endl;
	return 0;
}
//11.38.2
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<unordered_map>
#include<string>
#include<algorithm>
#include<stdexcept>

using namespace std;

unordered_map<string, string> buildMap(ifstream &map_file)
{
	unordered_map<string, string> trans_map;
	string key;
	string value;
	while (map_file >> key && getline(map_file, value))
		if (value.size() > 1)
			trans_map[key] = value.substr(1);
		else
			throw runtime_error("no rule for" + key);
	return trans_map;
}

const string transform(const string &s, const unordered_map<string, string> &m)
{
	auto map_it = m.find(s);
	if (map_it != m.end())
		return map_it->second;
	else
		return s;
}

void word_transform(ifstream &map_file, ifstream &input)
{
	auto trans_map = buildMap(map_file);
	string text;
	while (getline(input, text))
	{
		istringstream stream(text);
		string word;
		bool firstword = true;
		while (stream >> word)
		{
			if (firstword)
				firstword = false;
			else
				cout << " ";
			cout << transform(word, trans_map);
		}
		cout << endl;
	}
}
int main(int argc, char *argv[])
{
	ifstream map_file(argv[1]), input(argv[2]);
	if (!map_file || !input)
		cout << "The file open erro" << endl;
	word_transform(map_file, input);
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值