1. 程式人生 > >c++ primer 第十一章習題

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 報錯