11.3:
#include <iostream>
using namespace std;
#include<map>
#include<set>
int main()
{
map<string, size_t>words_count;
string word;
set<string>ss = { "the","an","a" };
while (cin >> word)
{
if (ss.find(word) == ss.end())
{
++words_count[word];
}
}
for (const auto& s : words_count)
{
cout << s.first << " occur " << s.second <<
((s.second > 1) ? "times" : "time") << endl;
}
return 0;
}
11.4:
#include <iostream>
using namespace std;
#include<map>
#include<set>
int main()
{
map<string, size_t>words_count;
string word;
while (cin >> word)
{
for (auto& c : word)
{
c = tolower(c);
}
word.erase(remove_if(word.begin(), word.end(), ispunct), word.end());
++words_count[word];
}
for (const auto& s : words_count)
{
cout << s.first << " occur " << s.second <<
((s.second > 1) ? " times" : " time") << endl;
}
return 0;
}
10.7:
#include <iostream>
using namespace std;
#include<map>
#include<vector>
int main()
{
map<string, vector<string>>family;
for (string secondname; cout << "请输入姓: ", cin >> secondname&&secondname!="end";)
{
for (string firstname; cout << "请输入名: ", cin >> firstname&&firstname!="end";)
{
family[secondname].push_back(firstname);
}
}
for (auto f : family)
{
for (auto f2 : f.second)
{
cout << f.first << " " << f2 << endl;
}
}
return 0;
}
11.8:
#include <iostream>
using namespace std;
#include<vector>
int main()
{
vector<string>words;
for (string word; cout << "请输入单词: ", cin >> word;)
{
if (find(words.cbegin(), words.cend(), word) == words.cend())
{
words.push_back(word);
}
else
{
cout << "已有重复单词" << endl;
}
}
return 0;
}
11.13