您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++Primer 练习