11.3
#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cout << "打开输入文件失败" << endl;
exit(1);
}
map<string, size_t>word_count;
string word;
while (in >> word)
++word_count[word];
for (const auto &w : word_count)
cout << w.first << "出现了" << w.second << "次" << endl;
return 0;
}
11.4
#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
string &trans(string &s)
{
for (decltype(s.size()) p = 0; p < s.size(); p++)
{
if (s[p] >= 'A'&&s[p] <= 'Z')
s[p] -= ('A' - 'a');
else if (s[p] == ',' || s[p] == '.')
s.erase(p, 1);
}
return s;
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cout << "打开输入文件失败" << endl;
exit(1);
}
map<string, size_t>word_count;
string word;
while (in >> word)
++word_count[trans(word)];
for (const auto &w : word_count)
cout << w.first << "出现了" << w.second << "次" << endl;
return 0;
}
11.7
#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
void add_family(map<string, vector<string>>&families, const string &family)
{
if (families.find(family) == families.end())
families[family] = vector<string>();
}
void add_child(map<string, vector<string>>&families, const string &family, const string &child)
{
families[family].push_back(child);
}
int main()
{
map<string, vector<string>>families;
add_family(families, "张");
add_child(families, "张", "强");
add_child(families, "张", "刚");
add_child(families, "王", "五");
add_family(families, "王");
for (auto f : families)
{
cout << f.first << "家的孩子:";
for (auto c : f.second)
cout << c << " ";
cout << endl;
}
return 0;
}
11.8
#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include<algorithm>
#include<set>
using namespace std;
string &trans(string &s)
{
for (decltype(s.size()) p = 0; p < s.size(); p++)
{
if (s[p] >= 'A'&&s[p] <= 'Z')
s[p] -= ('A' - 'a');
else if (s[p] == ',' || s[p] == '.')
s.erase(p, 1);
}
return s;
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cout << "打开输入文件失败" << endl;
exit(1);
}
set<string> unique_word;
string word;
while(in >> word)
{
trans(word);
unique_word.insert(word);
}
for (const auto &w : unique_word)
cout << w << endl;
cout << endl;
return 0;
}
#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
string &trans(string &s)
{
for (decltype(s.size()) p = 0; p < s.size(); p++)
{
if (s[p] >= 'A'&&s[p] <= 'Z')
s[p] -= ('A' - 'a');
else if (s[p] == ',' || s[p] == '.')
s.erase(p, 1);
}
return s;
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cout << "打开输入文件失败" << endl;
exit(1);
}
vector<string> unique_word;
string word;
while (in >> word)
{
trans(word);
if (find(unique_word.begin(), unique_word.end(), word) == unique_word.end())
unique_word.push_back(word);
}
for (const auto &w : unique_word)
cout << w << endl;
cout << endl;
return 0;
}
11.9
#include<iostream>
#include<fstream>
#include<map>
#include<string>
#include<algorithm>
#include<set>
#include<vector>
#include<list>
#include <sstream>
using namespace std;
string &trans(string &s)
{
for (decltype(s.size()) p = 0; p < s.size(); p++)
{
if (s[p] >= 'A'&&s[p] <= 'Z')
s[p] -= ('A' - 'a');
else if (s[p] == ',' || s[p] == '.')
s.erase(p, 1);
}
return s;
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cout << "打开输入文件失败" << endl;
exit(1);
}
map<string, list<int>>word_lineno;
string line;
string word;
int lineno = 0;
while (getline(in, line))
{
lineno++;
istringstream l_in(line);
while (l_in >> word)
{
trans(word);
word_lineno[word].push_back(lineno);
}
}
for (const auto &w : word_lineno)
{
cout << w.first << "所在行:";
for (const auto &i : w.second)
cout << i << " ";
cout << endl;
}
return 0;
}
11.12
#include<iostream>
#include<fstream>
#include<utility>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cout << "打开输入文件失败!" << endl;
exit(1);
}
vector<pair<string, int>>data;
string s;
int v;
while (in >> s&&in >> v)
//data.push_back(pair<string, int>(s, v));
//data.push_back({ s, v });
data.push_back(make_pair(s, v));
for (const auto &d : data)
{
cout << d.first << " " << d.second << endl;
}
return 0;
}
11.14
#include<iostream>
#include<map>
#include<utility>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
void add_family(map<string, vector<pair<string, string>>>&families, const string &family)
{
families[family];
}
void add_child(map<string, vector<pair<string, string>>>&families, const string &family, const string &child, const string &birthday)
{
families[family].push_back({ child, birthday });
}
int main(int argc, char *argv[])
{
map<string, vector<pair<string, string>>>families;
add_family(families, "张");
add_child(families, "张", "强", "1970-1-1");
add_child(families, "张", "刚", "1980-1-1");
add_child(families, "王", "五","1990-1-1");
add_family(families, "王");
for (auto f : families)
{
cout << f.first << "家的孩子:";
for (auto c : f.second)
cout << c.first << "(生日" << c.second << "), ";
cout << endl;
}
return 0;
}
11.20
#include<iostream>
#include<map>
#include<utility>
#include<string>
#include<algorithm>
#include<vector>
#include<fstream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cout << "打开文件失败" << endl;
exit(1);
}
map<string, size_t> word_count;
string word;
while (in >> word)
{
auto ret = word_count.insert({ word, 1 });
if (!ret.second)
++ret.first->second;
}
/*while (in >> word)
{
auto ret = word_count.insert({ word, 0 }).first->second;
}*/
for (const auto&w : word_count)
cout << w.first << "出现了" << w.second << "次" << endl;
return 0;
}
11.23
#include<iostream>
#include<map>
#include<utility>
#include<string>
#include<algorithm>
#include<vector>
#include<fstream>
using namespace std;
void add_child(multimap<string, string> &families, const string &family, const string &child)
{
families.insert({ family, child });
}
int main(int argc, char *argv[])
{
multimap<string, string>families;
add_child(families, "张", "强");
add_child(families, "张", "刚");
add_child(families, "王", "五");
for (auto f : families)
cout << f.first << "家的孩子:" << f.second << endl;
return 0;
}
11.31
#include<iostream>
#include<map>
#include<utility>
#include<string>
#include<algorithm>
#include<vector>
#include<fstream>
using namespace std;
void remove_author(multimap<string, string> &books, const string &author)
{
auto pos = books.equal_range(author);
if (pos.first == pos.second)
cout << "没有" << author << "这个作者" << endl << endl;
else
books.erase(pos.first, pos.second);
}
void print_books(multimap<string, string>&books)
{
cout << "当目前书包括:" << endl;
for (auto &book : books)
cout << book.first << ",《" << book.second << "》" << endl;
cout << endl;
}
int main(int argc, int *argv[])
{
multimap<string, string> books;
books.insert({ "Barth,John", "Sot-Weed Factor" });
books.insert({ "Barth,John", "Lost in the Funhouse" });
books.insert({ "金庸", "射雕英雄传" });
books.insert({ "金庸", "天龙八部" });
print_books(books);
remove_author(books, "张三");
remove_author(books, "Barth,John");
print_books(books);
return 0;
}
11.38
#include<iostream>
#include<fstream>
#include<unordered_map>
#include<string>
#include<algorithm>
using namespace std;
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
{
cout << "打开输入文件失败" << endl;
exit(1);
}
unordered_map<string, size_t>word_count;
string word;
while (in >> word)
++word_count[word];
for (const auto &w : word_count)
cout << w.first << "出现了" << w.second << "次" << endl;
return 0;
}
打不开
#include<unordered_map>
#include<vector>
#include<iostream>
#include<fstream>
#include<string>
#include<stdexcept>
#include<sstream>
using std::unordered_map;
using std::string;
using std::vector;
using std::ifstream;
using std::cout;
using std::endl;
using std::getline;
using std::runtime_error;
using std::istringstream;
unordered_map<string, string> buildMap(ifstream &map_file)
{
unordered_map<string, string> trans_map;
string key;
string value;
while (map_file >> key&&getline(map_file, value))
if (value.size() > 1)
trans_map[key] = value.substr(1);
else
throw runtime_error("no rule for " + key);
return trans_map;
}
const string & transform(const string &s, const unordered_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);
cout << "Here is our transformation map: \n\n";
for (auto entry : trans_map)
cout << "key: " << entry.first << "\tvalue: " << entry.second << endl;
cout << "\n\n";
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(int argc, char*argv[])
{
if (argc != 3)
throw runtime_error("wrong number of arguments");
ifstream map_file(argv[1]);
if (!map_file)
throw runtime_error("no transformation file");
ifstream input(argv[2]);
if (!input)
throw runtime_error("no input file");
word_transform(map_file, input);
system("pause");
return 0;
}