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:
#include <iostream>
using namespace std;
#include<vector>
int main()
{
vector<pair<string, int>>vep;
string str;
int i = 0;
while (cin >> str >> i)
{
vep.push_back(make_pair(str, i));
vep.push_back(pair<string, int>(str, i));
vep.push_back({ str,i });
vep.emplace_back(str, i);
}
return 0;
}
11.14:
#include <iostream>
using namespace std;
#include<map>
#include<vector>
int main()
{
map<string, vector<pair<string,string>>>family;
for (string secondname; cout << "请输入姓: ", cin >> secondname && secondname != "end";)
{
for (string firstname; cout << "请输入名: ", cin >> firstname && firstname != "end";)
{
cout << "请输入生日: ";
string birthday;
cin >> birthday;
family[secondname].push_back(make_pair(firstname,birthday));
}
}
for (auto f : family)
{
for (auto f2 : f.second)
{
cout << f.first << " " << f2.first << " " << f2.second << endl;
}
}
return 0;
}
11.16:
#include <iostream>
using namespace std;
#include<map>
int main()
{
map<int, int>m;
m[1] = 2;
auto beg = m.begin();
cout << beg->first << " " << beg->second << endl;
beg->second = 1;
cout << beg->first << " " << beg->second << endl;
return 0;
}
11.20:
#include <iostream>
using namespace std;
#include<map>
#include<set>
int main()
{
map<string, size_t>words_count;
string word;
while (cin >> word)
{
auto it = words_count.insert({ word,1 });
if (!it.second)
{
++it.first->second;
}
}
for (const auto& s : words_count)
{
cout << s.first << " occur " << s.second <<
((s.second > 1) ? "times" : "time") << endl;
}
return 0;
}
11.23:
#include <iostream>
using namespace std;
#include<map>
int main()
{
multimap<string, string>families;
string fname, lname;
while (cin >> fname >> lname)
{
families.insert({ fname,lname });
}
for (auto f : families)
{
cout << f.first << " " << f.second << endl;
}
return 0;
}
11.26:
#include <iostream>
#include <map>
#include <string>
#include <typeinfo>
int main()
{
// ex11.26
std::map<int, std::string> m = { { 1,"ss" },{ 2,"sz" } };
using KeyType = std::map<int, std::string>::key_type;
std::cout << "type to subscript: " << typeid(KeyType).name() << std::endl;
std::cout << "returned from the subscript operator: " << typeid(decltype(m[1])).name() << std::endl;
return 0;
}
11.31:
第一版:
#include <iostream>
using namespace std;
#include <map>
int main()
{
multimap<string, string>authors = {
{"jojo","<fly>"},
{"jojo","<jump>"},
{"jojo","<map>"},
{"coco","<computer>"},
{"coco","<chanel>"} };
for (auto it = authors.find("jojo");it != authors.end();++it)
{
if (it->second == "<fly>")
{
authors.erase(it);
break;
}
}
for (auto m : authors)
{
cout << m.first << " " << m.second << endl;
}
return 0;
}
第二版:
#include <iostream>
using namespace std;
#include <map>
int main()
{
multimap<string, string>authors = {
{"jojo","<fly>"},
{"jojo","<jump>"},
{"jojo","<map>"},
{"coco","<computer>"},
{"coco","<chanel>"} };
auto it = authors.find("jojo");
auto count = authors.count("jojo");
while (count)
{
if (it->second == "<jump>")
{
authors.erase(it);
break;
}
++it;
--count;
}
for (const auto& m : authors)
{
cout << m.first << " " << m.second << endl;
}
return 0;
}
11.32;
#include <iostream>
using namespace std;
#include <map>
#include<set>
int main()
{
multimap<string, string>authors = {
{"jojo","<fly>"},
{"jojo","<jump>"},
{"jojo","<map>"},
{"coco","<computer>"},
{"coco","<chanel>"} };
map<string, set<string>>oder;
for (auto a : authors)
{
oder[a.first].insert(a.second);
}
for (auto a : oder)
{
for (auto b : a.second)
{
cout << a.first << " " << b << endl;
}
}
return 0;
}
11.33:
我去,这个代码我查了好几遍,后来发现自己的return写在while里面了,吐血
#include <map>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
using std::string; using std::ifstream;
std::map<string, string> buildMap(ifstream& map_file)
{
std::map<string, string> trans_map;
for (string key, value; map_file >> key && getline(map_file, value); )
if (value.size() > 1) trans_map[key] = value.substr(1).substr(0, value.find_last_not_of(' '));
return trans_map;
}
const string& transform(const string& s, const std::map<string, string>& m)
{
auto map_it = m.find(s);
return map_it == m.cend() ? s : map_it->second;
}
void word_transform(ifstream& map, ifstream& input)
{
auto trans_map = buildMap(map);
for (string text; getline(input, text); ) {
std::istringstream iss(text);
for (string word; iss >> word; )
std::cout << transform(word, trans_map) << " ";
std::cout << std::endl;
}
}
int main()
{
ifstream ifs_map("1.txt"), ifs_content("2.txt");
if (ifs_map && ifs_content) word_transform(ifs_map, ifs_content);
else std::cerr << "can't find the documents." << std::endl;
}
#include <iostream>
using namespace std;
#include <map>
#include<fstream>
#include<string>
#include<sstream>
map<string, string> buildMap(ifstream& map_file)
{
map<string, string>trans_map;
string key, value;
while (map_file >> key && getline(map_file, value))
{
if (value.size() > 1)
{
trans_map[key] = value.substr(1).substr(0, value.find_last_not_of(' '));
}
}
return trans_map;
}
const string& transform(const string& s, const map<string, string>& m)
{
auto map_it = m.find(s);
if (map_it != m.cend())
{
return map_it->second;
}
else
{
return s;
}
}
void word_transform(ifstream& map_file, ifstream& input)
{
auto trans_map = buildMap(map_file);
string text;
while (getline(input, text))
{
istringstream stream(text);
string word;
bool firstword = true;
while (stream >> word)
{
if (firstword)
{
firstword = false;
}
else
{
cout << " ";
}
cout << transform(word, trans_map);
}
cout << endl;
}
}
int main()
{
ifstream ifs_map("1.txt"), ifs_content("2.txt");
word_transform(ifs_map, ifs_content);
return 0;
}
11.38:
#include <unordered_map>
#include <set>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
using std::string;
void wordCounting()
{
std::unordered_map<string, size_t> word_count;
for (string word; std::cin >> word; ++word_count[word]);
for (const auto &w : word_count)
std::cout << w.first << " occurs " << w.second << (w.second > 1 ? "times" : "time") << std::endl;
}
void wordTransformation()
{
std::ifstream ifs_map("../data/word_transformation.txt"), ifs_content("../data/given_to_transform.txt");
if (!ifs_map || !ifs_content) {
std::cerr << "can't find the documents." << std::endl;
return;
}
std::unordered_map<string, string> trans_map;
for (string key, value; ifs_map >> key && getline(ifs_map, value); )
if (value.size() > 1) trans_map[key] = value.substr(1).substr(0, value.find_last_not_of(' '));
for (string text, word; getline(ifs_content, text); std::cout << std::endl)
for (std::istringstream iss(text); iss >> word; ) {
auto map_it = trans_map.find(word);
std::cout << (map_it == trans_map.cend() ? word : map_it->second) << " ";
}
}
int main()
{
//wordCounting();
wordTransformation();
}