11.1
map是关联容器,里面的元素是按关键字来保存和访问的,vector属于顺序容器,,是根据元素保存的位置来保存和访问的
11.3
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string, int> count_word;
string word;
while (cin >> word)
++count_word[word];
for (auto cw : count_word)
cout << cw.first << " occurs " << cw.second << ((cw.second > 1) ? " times" : " time") << endl;
return 0;
}
11.4
#include<iostream>
#include<string>
#include<map>
using namespace std;
void change(string&s)
{
string::size_type i = 0;
while (i < s.size())
{
if (ispunct(s[i]))
{
s.erase(i, 1);
continue;
}
s[i] = tolower(s[i]);
++i;
}
}
int main()
{
map<string, int> count_word;
string word;
while (cin >> word)
{
change(word);
++count_word[word];
}
for (auto cw : count_word)
cout << cw.first << " occurs " << cw.second << ((cw.second > 1) ? " times" : " time") << endl;
return 0;
}
11.5
定义一个map时必须既指明关键字又指明值,而set只需指明关键字,并不保存值.
需要保存关键字的值时使用map,只需要保存关键字时使用set
11.6
set中只保存了关键字,不会保存重复的值,并且内部元素是有序的,list既可以保存重复的元素,并且内部是无序的,并且插入,删除会更加效率,所以如果频繁需要插入删除操作的时候选择list
11.7
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<sstream>
using namespace std;
int main()
{
map<string, vector<string>> family;
string name;
string first_name, second_name;
while (getline(cin, name))
{
istringstream scin(name);
scin >> first_name >> second_name;
family[second_name].push_back(first_name);
}
for (auto f : family)
{
cout << f.first << " family:" << endl;
for (string sn : f.second)
cout << sn << " ";
cout << endl;
}
return 0;
}
11.8
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<string> unique_word;
string word;
bool flag = true;
while (cin >> word)
{
for (string s : unique_word)
if (word == s)
{
flag = false; break;
}
if (flag)
unique_word.push_back(word);
flag = true;
}
for (string s : unique_word)
cout << s << " ";
return 0;
}
11.10
关键字必须定义过严格弱序,list<int>::iterator上不支持<
11.11
using pf=bool(*) (const Sales_data&lhs,constSales_data&rhs);
multiset<Sales_data,pf>bookstore(compareIsbn);
11.12
#include<iostream>
#include<string>
#include<vector>
#include<utility>
using namespace std;
int main()
{
vector<pair<string, int>> pvec;
string word;
int num;
while (cin >> word >> num)
pvec.push_back(pair<string, int>(word, num));
for (auto elem : pvec)
{
cout << elem.first << " " << num << endl;
}
return 0;
}
11.13
pvec.push_back(make_pair(word, num));
pvec.push_back({word,num});
pair形式最容易编写和理解
11.14
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<sstream>
using namespace std;
int main()
{
map<string, vector<pair<string,string>>> family;
string name;
string first_name, second_name,birthday;
while (getline(cin, name))
{
istringstream scin(name);
scin >> first_name >> second_name>>birthday;
family[second_name].push_back({ first_name,birthday });
}
for (auto f : family)
{
cout << f.first << " family:" << endl;
for (auto sn : f.second)
cout << sn.first << " birthday: " << sn.second << endl;
}
return 0;
}
11.15
mapped_type是vector<int>,key_type是int,value_type是pair<int,vector<int>>
11.17
a)合法,将vector中元素拷贝到multiset中
b)不合法,multiset没有push_back操作
c)合法
d)合法
11.18
map<string,int>::iterator
11.19
using pf=bool(*) (const Sales_data&lhs,constSales_data&rhs);
multiset<Sales_data,pf>::iterator::it
11.20
#include<iostream>#include<string>
#include<map>
using namespace std;
int main()
{
map<string, int> word_count;
string word;
while (cin >> word)
{
auto ret = word_count.insert({ word,1 });
if (!ret.second)
++ret.first->second;
}
for (auto w : word_count)
cout << w.first << " " << w.second << endl;
return 0;
}
11.21
单词计数,同前面一样功能
11.22
map<string, vector<int>> ma;
pair< map<string,vector<int>>::iterator,bool> ret=ma.insert({ "word",{1,2,3} });
11.24
添加关键字为0的元素,并将其值初始化为1
11.25
非法,空vector
11.26
只能能转化为关键字类型的都可以对map进行下标操作,下标运算符返回mapped_type
11.27
需要统计相同关键字的数量时用count,只是需要确定有没有这个关键字的时候用 find
11.28
map<sring,vector<int>> ::iterator
11.29
upper_bound和lower_bound返回尾后迭代器,enquea_range返回由一对尾后迭代器组成的pair
11.30
pos是一对指向map里元素的迭代器组成pair,pos.first指向这个pair的第一个元素,是一个指向map的迭代器,
pos.first->second打印这个迭代器指向元素的值
11.31
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
multimap<string, string>authors{ {"hu","C++"}
,{"hu","C--"}
,{"lily","liang"} };
authors.erase("ok");
for (auto n : authors)
cout << n.first << ":" << n.second<<endl;
return 0;
}
11.32
#include<iostream>
#include<string>
#include<map>
#include<set>
using namespace std;
int main()
{
multimap<string, string>authors{ {"hu","C11"}
,{"hu","C22"}
,{ "lily","liang" }
,{"hu","AK47"}
};
set<string> title;
auto pos = authors.equal_range("hu");
string name;
if (pos.first != authors.end())
name = pos.first->first;
for (; pos.first != pos.second; ++pos.first)
title.insert(pos.first->second);
cout << name << " works show:\n";
for (string s : title)
cout << s << endl;
return 0;
}
11.33
#include<iostream>
#include<string>
#include<fstream>
#include<map>
#include<sstream>
using namespace std;
map<string, string> buildMap(ifstream&map_file)
{
map<string, string> trans;
string key;
string value;
while (map_file >> key&&getline(map_file, value))
if (value.size() > 1)
trans[key] = value.substr(1);
else
throw runtime_error("no rules for" + key);
return trans;
}
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;
return s;
}
void main_transform(ifstream&map_dic, ifstream&input)
{
map<string, string> dictionary = buildMap(map_dic);
string text;
while (getline(input, text))
{
istringstream stream(text);
bool isfirstword = true;
string word;
while (stream >> word)
{
if (isfirstword)
isfirstword = false;
else
cout << " ";
cout << transform(word,dictionary);
}
cout << endl;
}
}
int main()
{
ifstream dic("dictionary.txt"), in("article.txt");
main_transform(dic, in);
return 0;
}
11.34
首先两个返回值就不同,find返回迭代器,下标运算符返回key的值,并且如果该key不存在容器中,会进行添加,这样就做不到字典翻译功能,其次参数是const型不允许修改,所以会导致编译错误
11.35
如果重复出现同样的key值,原代码会进行替换,改写之后则不会,因为 insert插入失败
11.36
会跳出runtime_erro
11.37
有序版本可以不考虑排序问题,自动帮排,无序版本性能上更好点,因为无序版本不用维护排序
11.38
将map换成unordered_map即可,只是不按字典序输出了