c++ primer 第十一章习题

练习11.1 map下标是关键字,可以设定类型。vector下标是整数。 map的元素是pair, vector是一个单类型。

练习11.3 11.4

#include <iostream>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <string>
#include <cctype>
#include <algorithm>

using namespace std;

int main() {
	map<string, int> count;
	string word;
	while(cin >> word) {
		if(ispunct(word.back()))
			word = word.substr(0,word.size()-1);
		transform(word.begin(), word.end(), word.begin(), ::tolower);
		count[word]++;
	}
	for(auto x : count)
		cout << "word "<< x.first<<" appears "<< x.second<< " times."<<endl;
	return 0;
}

练习11.5 map是关键词对集合,set是关键词集合。映射关系用map,关键词是set。

练习11.6 set没有重复的关键字, list可以重复。set元素有序排列。

练习11.7

int main() {
	map<string, vector<string>> family = {{"jackson",{}},{"wang",{}}};
	family["jackson"].push_back("michael");
	family["jackson"].push_back("david");
	family["wang"].push_back("shi");
	for(auto x : family)
		cout << "family "<< x.first << " has "<<x.second.size()<< "persons"<<endl;
	return 0;
}

练习11.8 好处是set自动去重

int main() {
	vector<string> v = {"hello", "hello", "world"};
	set<string> s(v.begin(), v.end());
	sort(v.begin(), v.end());
	auto unique_end = unique(v.begin(), v.end());
	v.erase(unique_end, v.end());
	for(auto x : v)
		cout << "vector: "<< x<<endl;
	for(auto x : s)
		cout << "set: " << x << endl;
	return 0;
}

练习11.9

int main() {
	map<string, list<int>> mp;
	string word;
	int line;
	while(cin>>word>>line) {
		mp[word].push_back(line);
	}
	for(auto x : mp) {
		cout <<  "word "<< x.first << " appears in ";
		for_each(x.second.begin(), x.second.end(), [](int&a){cout << "line: "<<a<<' ';});
	    cout << endl;
	}
}

练习11.10

支持定义但不支持操作。实际不应该支持定义,应该是模板动态加载的问题。因为vector的迭代器定义了<操作而list的没有。

练习11.11

multiset<Sales_data, bool(*)(const Sales_data&, const Sales_data&)> bookStore(compareIsbn);

练习11.12

int main() {
    vector<pair<string, int>> v;
    string s;
    int i;
    while(cin>>s>>i) {
    	v.push_back({s,i});
    }
    for(auto x : v)
    	cout << x.first<<" "<<x.second<<endl;
	return 0;
}

练习11.13 make_pair  自动判断类型并返回,且不会有歧义。

练习11.14

int main() {
	map<string, vector<pair<string, string>>> family = {{"jackson",{}},{"wang",{}}};
	family["jackson"].push_back({"michael", "1999 1 1"});
	family["jackson"].push_back({"david", "2000 1 1"});
	family["wang"].push_back({"shi", "1988 12 2"});
	for(auto x : family)
		cout << "family "<< x.first << " has "<<x.second.size()<< "persons"<<endl;
	return 0;
}

练习11.15 vector<int>  int  pair<const int, vector<int>>

练习11.16

int main() {
	map<string,int> mp = {{"nihao",1}};
	map<string,int>::iterator iter = mp.begin();
	iter->second = 2;
	cout<<iter->first<<' '<<iter->second<<endl;

	return 0;
}

练习11.17 T   F multiset不支持push_back  T  T

练习11.18 map<string, size_t>::iterator。

练习11.19 multiset<Sales_data, bool(*) (const Sales_data&, const Sales_data&)>::iterator;

练习11.20

int main() {
	map<string, int> count;
	string word;
	while(cin >> word) {
		if(ispunct(word.back()))
			word = word.substr(0,word.size()-1);
		transform(word.begin(), word.end(), word.begin(), ::tolower);
		auto ret = count.insert({word, 1});
		if(!ret.second)
			++ret.first->second;
	}
	for(auto x : count)
		cout << "word "<< x.first<<" appears "<< x.second<< " times."<<endl;
	return 0;
}

练习11.21 将word对应的value加一。

练习11.22  pair<string, vector<int>>     pair<map<string, vector<int>>::iterator, bool>

练习11.23

int main() {
	multimap<string, string> family;
	family.insert({"jackson","michael"});
	family.insert({"jackson","david"});
	family.insert({"wang","shi"});
	for(auto x : family)
		cout << "family "<< x.first << " has "<<x.second <<endl;
	return 0;
}

练习11.24 insert({0,1})

练习11.25 报错

练习11.26 key_type  mapped_type

练习11.27 计数用count  是否存在用find

练习11.28 map<string, vector<int>>::iterator it = mp.find(s);

练习11.29 第一个大于关键字的元素迭代器或者end   同上  空范围对的一个pair,可插入位置,即第一个大于k的位置。

练习11.30 返回的迭代器范围pair的第一个迭代器中的value值

练习11.31

int main() {
	multimap<string, string> authors;
	authors.insert({"david","book1"});
	authors.insert({"david","kbb1"});
	authors.insert({"aang","222"});
	auto iter = authors.find("david");
	auto n = authors.count("david");
	while(n--) {
		authors.erase(iter++);
	}
	return 0;
}

练习11.32

int main() {
	multimap<string, string> authors;
	authors.insert({"david","book1"});
	authors.insert({"david","kbb1"});
	authors.insert({"aang","222"});
	auto iter = authors.begin();
	while(iter != authors.end()) {
		cout <<iter->first << " "<< iter->second<<endl;
		iter++;
	}
	return 0;
}

练习11.34 不变

练习11.35 不会更新重复的规则,只会保留第一次出现的规则。

练习11.36 报错

猜你喜欢

转载自blog.csdn.net/qq_25037903/article/details/81951524