首页 / C++ / C++ Primer 第十一章编程答案
C++ Primer 第十一章编程答案
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ Primer 第十一章编程答案,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含8877字,纯文字阅读大概需要13分钟。
内容图文
![C++ Primer 第十一章编程答案](/upload/InfoBanner/zyjiaocheng/831/909104bc4e834597a4633cfc9819ff20.jpg)
//11.3
#include<iostream>
#include<fstream>
#include<string>
#include<map>
using namespace std;
int main(int argc,char *argv[])
{
ifstream in(argv[1]);
if (!in)
cerr << "The file open erro" << endl;
map<string, size_t> word_count;
string word;
while (in >> word)
++word_count[word];//提取word的计数器并将其加1
for (auto &tem : word_count)
cout << tem.first << " appear " << tem.second << ((tem.second > 1) ? " times" : " time") << endl;
return 0;
}
//11.4
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
string &trans(string &s)
{
for (int i = 0;i < s.size();i++)
{
if (s[i] >= 'A' &&s[i] <= 'Z')
s[i] += 'a' - 'A';
else if (s[i] == '.' || s[i] == ',')
s.erase(i, 1);
}
return s;
}
int main(int argc,char *argv[])
{
ifstream in(argv[1]);
if (!in)
cerr << "The file open erro" << endl;
map<string, size_t> word_count;
string word;
while (in >> word)
++word_count[trans(word)];//提取word的计数器并将其加1
for (auto &tem : word_count)
cout << tem.first << " appear " << tem.second << ((tem.second > 1) ? " times" : " time") << endl;
return 0;
}
//11.7
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
void add_family(map < string, vector<string>> &families, const string &family)
{
if (families.find(family) == families.end())
families[family] == vector<string>();//空的string给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, "Sun");
add_child(families, "Sun", "Wukong");
add_child(families, "Sun", "Wufan");
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<string>
#include<algorithm>
#include<vector>
using namespace std;
string &trans(string &s)
{
for (int i = 0;i < s.size();i++)
{
if (s[i] >= 'A' &&s[i] <= 'Z')
s[i] += 'a' - 'A';
else if (s[i] == '.' || s[i] == ',')
s.erase(i, 1);
}
return s;
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
cerr << "The file open erro" << endl;
vector<string> unique_word;
string word;
while (in >> word)
{
trans(word);
if (find(unique_word.begin(), unique_word.end(), word) == unique_word.end())//提取word的计数器并将其加1
unique_word.push_back(word);
}
for (auto tem : unique_word)
cout << tem<< " ";
cout<<endl;
return 0;
}
//11.9
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<algorithm>
#include<list>
#include<map>
using namespace std;
string &trans(string &s)
{
for (int i = 0;i < s.size();i++)
{
if (s[i] >= 'A' &&s[i] <= 'Z')
s[i] += 'a' - 'A';
else if (s[i] == '.' || s[i] == ',')
s.erase(i, 1);
}
return s;
}
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
cerr << "The file open erro" << endl;
map<string, list<int>> words_line;
string line,word;
int line_num = 0;
while (getline(in,line))
{
line_num++;
istringstream iss(line);
while (iss >> word)
{
trans(word);
words_line[word].push_back(line_num);
}
}
for (auto &tem : words_line)
{
cout << tem.first << "所在行:";
for (auto &w : tem.second)
cout << w << " ";
cout << endl;
}
return 0;
}
//11.12
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<utility>
using namespace std;
int main(int argc,char *argv[])
{
ifstream in(argv[1]);
if (!in)
cerr << "The file open erro" << endl;
vector<pair<string, int>> vec;
string s;
int v;
while (in >> s && in >> v)
vec.push_back(make_pair(s,v));
for (auto tem : vec)
cout << tem.first << " " << tem.second << endl;
return 0;
}
//11.14
#include<iostream>
#include<vector>
#include<utility>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
void add_family(map < string, vector<pair<string, string >>> &families, const string &family)
{
if (families.find(family) == families.end())
families[family] == vector<pair<string, string >>();//空的string给vector<pair<string, string >>()
}
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()
{
map < string, vector<pair<string,string >>> families;
add_family(families, "Sun");
add_child(families, "Sun", "Wukong","19980908");
add_child(families, "Sun", "Wufan", "19880705");
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<fstream>
#include<string>
#include<map>
using namespace std;
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
cerr << "The file open erro" << endl;
map<string, size_t> word_count;
string word;
while (in >> word)
{
auto ret = word_count.insert({ word,1 });
if(!ret.second)
++ret.first->second;
}
for (auto &tem : word_count)
cout << tem.first << " appear " << tem.second << ((tem.second > 1) ? " times" : " time") << endl;
return 0;
}
//11.23
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
void add_child(multimap<string, string> &families, const string &family, const string &child)
{
families.insert({family,child});
}
int main()
{
multimap<string, string> families;
add_child(families, "Sun", "Wukong");
add_child(families, "Sun", "Wufan");
for (auto f : families)
{
cout << f.first << "家的孩子:" << f.second;
cout << endl;
}
return 0;
}
//11.31
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
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 << "没有此作者" << endl;
books.erase(pos.first, pos.second);
}
void print_books(multimap<string, string> &books)
{
for (auto &book : books)
{
cout << book.first << "的书《" << book.second << "》" << endl;
}
}
int main()
{
multimap<string, string> mulm;
mulm.insert({ "金庸","倚天屠龙记" });
mulm.insert({ "金庸","射雕英雄传" });
mulm.insert({ "古龙","碧血剑" });
mulm.insert({ "古龙","浣花洗剑录" });
print_books(mulm);
remove_author(mulm, "王国维");
remove_author(mulm, "金庸");
print_books(mulm);
return 0;
}
//11.31
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<map>
#include<string>
#include<algorithm>
#include<stdexcept>
using namespace std;
map<string, string> buildMap(ifstream &map_file)
{
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 map<string, string> &m)
{
auto map_it = m.find(s);
if (map_it != m.end())
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(int argc,char *argv[])
{
ifstream map_file(argv[1]), input(argv[2]);
if (!map_file || !input)
cout << "The file open erro" << endl;
word_transform(map_file, input);
return 0;
}
//11.38.1
#include<iostream>
#include<fstream>
#include<string>
#include<unordered_map>
using namespace std;
int main(int argc, char *argv[])
{
ifstream in(argv[1]);
if (!in)
cerr << "The file open erro" << endl;
unordered_map<string, size_t> word_count;
string word;
while (in >> word)
++word_count[word];//提取word的计数器并将其加1
for (auto &tem : word_count)
cout << tem.first << " appear " << tem.second << ((tem.second > 1) ? " times" : " time") << endl;
return 0;
}
//11.38.2
#include<iostream>
#include<fstream>
#include<sstream>
#include<vector>
#include<unordered_map>
#include<string>
#include<algorithm>
#include<stdexcept>
using namespace std;
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.end())
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(int argc, char *argv[])
{
ifstream map_file(argv[1]), input(argv[2]);
if (!map_file || !input)
cout << "The file open erro" << endl;
word_transform(map_file, input);
return 0;
}
内容总结
以上是互联网集市为您收集整理的C++ Primer 第十一章编程答案全部内容,希望文章能够帮你解决C++ Primer 第十一章编程答案所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。