11.1
map下标是关键字,不必是整数,而vector下标是整数;从map中提取一个元素,会得到一个pair类型对象。
11.3
#include<iostream>
#include<map>
#include<set>
#include<string>
using namespace std;
int main()
{
map<string, size_t> word_count;
set<string> exclude = {
"The", "But", "And", "Or", "An", "A",
"the", "but", "and", "or", "an", "a" };
string word;
while (cin >> word)
{
if (exclude.find(word) == exclude.end())
++word_count[word];
}
for (const auto &w : word_count)
cout << w.first << " occurs " << w.second
<< ((w.second > 1) ? " times" : " time") << endl;
return 0;
}
11.4
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<cctype>
#include<algorithm>
using namespace std;
int main()
{
map<string, size_t> word_count;
set<string> exclude = {
"The", "But", "And", "Or", "An", "A",
"the", "but", "and", "or", "an", "a" };
string word;
while (cin >> word)
{
word.erase(find_if(word.begin(), word.end(), ::ispunct), word.end());
for_each(word.begin(), word.end(), [](char &c) {
c = tolower(c); });
++word_count[word];
}
for (const auto &w : word_count)
cout << w.first << " occurs " << w.second
<< ((w.second > 1) ? " times" : " time") << endl;
return 0;
}
11.5
当定义一个map时,必须既指明关键字类型又指明值类型;而定义一个set时,只需指定关键字类型。
11.6
set不能重复,而list可以重复
11.8
#include<iostream>
#<