C++ Primer 第5版--练习11.7
2017-05-12 22:55
274 查看
练习 11.7:定义一个map,关键字是家庭的姓,值是一个vector,保存家中孩子(们)的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
int main()
{
map<string, vector<string>> family = { {"Green", {"Jim", "Kati", "Josh"}},
{"Eisenberg", {"Sam", "Eric"}} };
vector<string> children;
string surname, name;
cout << "Add new family: ";
cin >> surname;
cout << "Add children's name: ";
while(cin >> name)
children.push_back(name);
family.insert(pair<string,vector<string>>(surname, children));
for (const auto &f : family)
{
cout << "family name: " << f.first << "\nchildren's name: ";
for (const auto &c : f.second)
cout << c << " ";
cout << endl;
}
return 0;
}
#include <iostream>
#include <map>
#include <vector>
#include <string>
using namespace std;
int main()
{
map<string, vector<string>> family = { {"Green", {"Jim", "Kati", "Josh"}},
{"Eisenberg", {"Sam", "Eric"}} };
vector<string> children;
string surname, name;
cout << "Add new family: ";
cin >> surname;
cout << "Add children's name: ";
while(cin >> name)
children.push_back(name);
family.insert(pair<string,vector<string>>(surname, children));
for (const auto &f : family)
{
cout << "family name: " << f.first << "\nchildren's name: ";
for (const auto &c : f.second)
cout << c << " ";
cout << endl;
}
return 0;
}
相关文章推荐
- C++ Primer 第5版--练习10.14
- C++ Primer 第5版--练习10.33
- C++ Primer 第5版--练习6.56
- C++ Primer 第5版--练习9.47
- C++ Primer 第5版--练习10.15
- C++ Primer 第5版--练习10.25
- C++ Primer 第5版--练习10.35
- C++ Primer 第5版--练习10.13
- C++ Primer 第5版--练习10.36.
- C++ Primer 第5版--练习11.33
- C++ Primer 第5版--练习12.6
- C++ Primer 第5版--练习10.18
- C++ Primer 第5版--练习10.37
- C++ Primer 第5版--练习12.7
- C++ Primer 第5版--练习9.16
- C++ Primer 第5版--练习9.26
- C++ Primer 第5版--练习11.3
- C++ Primer 第5版--练习10.11
- C++ Primer 第5版--练习11.4
- C++ Primer 第5版--练习11.31