C++ prime 第十一章

11.4

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<cctype>
using namespace std;

using namespace placeholders;

void process(string& s)
{
	for (auto i = s.begin(); i != s.end(); ++i)
	{
		*i = tolower(*i);   //处理大写字母
		s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号

	}
}

int main(int argc, char** argv)
{
	map<string, size_t> word_count;
	string word;


	while (cin >> word)
	{
		process(word);
		++word_count[word];
	}
	for (const auto &w : word_count)
	{
		cout << w.first << "  " << w.second << ((w.second > 1) ? "times" : "time") << endl;
	}
	return 0;
}

10.7

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<cctype>
using namespace std;

using namespace placeholders;

void process(string& s)
{
	for (auto i = s.begin(); i != s.end(); ++i)
	{
		*i = tolower(*i);   //处理大写字母
		s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号

	}
}

int main(int argc, char** argv)
{
	map<string, vector<string>> family;

	string last_name, child_name;
	cout << "请输入家族姓氏" << endl;
	cin >> last_name;
	cout << "请输入孩子的名字" << endl;
	while (cin >> child_name)
	{
		family[last_name].push_back(child_name);
	}
	for (auto i : family)
	{
		cout << "该姓氏 : " << i.first << "成员有 : " << endl;

		for (auto j : i.second)
		{
			cout << j << "  ";
		}
		cout << endl;
	}




	return 0;
}

11.12

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<cctype>
#include<utility>
using namespace std;

using namespace placeholders;

/*void process(string& s)
{
	for (auto i = s.begin(); i != s.end(); ++i)
	{
		*i = tolower(*i);   //处理大写字母
		s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号

	}
}*/

int main(int argc, char** argv)
{
	vector<pair<string, int>> vec;
	string s;
	int num;
	cout << "请输入要读入的字符串 和 数字" << endl;

	while (cin >> s >> num)
	{
	
        vec.push_back(pair<string, int>(s, num)); //第一种
		vec.push_back({ s,num }); //第二种 
		vec.emplace_back(s, num); //第三种
		vec.push_back(make_pair(s, num)); //第四种

	}
	for (auto i : vec)
	{
		cout << i.first << " " << i.second << endl;
	}
	


	return 0;
}

11.14

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<cctype>
using namespace std;

using namespace placeholders;

//void process(string& s)
//{
//	for (auto i = s.begin(); i != s.end(); ++i)
//	{
//		*i = tolower(*i);   //处理大写字母
//		s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号
//
//	}
//}

int main(int argc, char** argv)
{
	map<string, vector<pair<string,string>>> family;

	string last_name, child_name, birthday;
	cout << "请输入家族姓氏" << endl;
	cin >> last_name;
	cout << "请输入孩子的名字和生日" << endl;
	while (cin >> child_name >> birthday)
	{
		family[last_name].push_back({ child_name,birthday });
	}
	for (auto i : family)
	{
		cout << "该姓氏 : " << i.first << "成员有 : " << endl;

		for (auto j : i.second)
		{
			cout << j.first <<  "   " << j.second;
		}
		cout << endl;
	}




	return 0;
}

11.31


#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<cctype>
using namespace std;

using namespace placeholders;

/*void process(string& s)
{
	for (auto i = s.begin(); i != s.end(); ++i)
	{
		*i = tolower(*i);   //处理大写字母
		s.erase(remove_if(s.begin(), s.end(), ispunct), s.end()); //处理标点符号

	}
}*/

int main(int argc, char** argv)
{
	multimap<string, string> mp = { {"Joker","renzhendexue"},{"Joker","yanyuan"},{"Joker","weibeideqingchun"} ,{"Jony J","nuli"}};
	auto iter = mp.find("Joker");
	auto num = mp.count("Joker");

	while (num)
	{
		if (iter->second == "renzhendexue")
		{
			mp.erase(iter);
			break;
		}
		++iter;
		--num;
	}
	for (auto i : mp)
	{
		cout << i.first << "的作品" << i.second << endl;

	}

	cout << endl;

	system("pause");
	cin.get();
}

11.32


#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
#include<forward_list>
#include<deque>
#include<algorithm>
#include<list>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<cctype>
using namespace std;

using namespace placeholders;


int main(int argc, char** argv)
{
	multimap<string, string> mp = { {"Joker","renzhendexue"},{"Joker","yanyuan"},{"Joker","weibeideqingchun"} ,{"Jony J","nuli"}};
	auto iter = mp.find("Joker");
	auto num = mp.count("Joker");
	map<string, set<string>> ans;  //对于set,系统能根据元素的值自动进行排序
	for (auto i : mp) {
		ans[i.first].insert(i.second);
	}
	for (auto i : ans) {
		cout << i.first << " : ";
		for (auto j : i.second) {
			cout << j << " ";
		}
		cout << endl;
	}

	system("pause");
	return 0;
	cin.get();
}

上一篇:记录为什么迭代器类型用auto进行推导的一个原因


下一篇:一、pod容器的分类