c++ primer 第十一章习题

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

练习11.3 11.4

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

int main() {
	map 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."<

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

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

练习11.7

int main() {
	map> 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 "<

练习11.8 好处是set自动去重

int main() {
	vector v = {"hello", "hello", "world"};
	set 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<

练习11.9

int main() {
	map> 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: "<

练习11.10

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

练习11.11

multiset bookStore(compareIsbn);

练习11.12

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

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

练习11.14

int main() {
	map>> 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 "<

练习11.15 vector  int  pair>

练习11.16

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

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

练习11.18 map::iterator。

练习11.19 multiset::iterator;

练习11.20

int main() {
	map 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."<

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

练习11.22  pair>     pair>::iterator, bool>

练习11.23

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

练习11.24 insert({0,1})

练习11.25 报错

练习11.26 key_type  mapped_type

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

练习11.28 map>::iterator it = mp.find(s);

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

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

练习11.31

int main() {
	multimap 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 authors;
	authors.insert({"david","book1"});
	authors.insert({"david","kbb1"});
	authors.insert({"aang","222"});
	auto iter = authors.begin();
	while(iter != authors.end()) {
		cout <first << " "<< iter->second<

练习11.34 不变

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

练习11.36 报错

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(C++)