#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<iterator>
using namespace std;
int main(int argc, char **argv) {
map<string, vector<string>> imap = { {"张",{"张1","张2"} } };
//绑定一个输出流的迭代器
ostream_iterator<string> out(cout, " ");
cout << "现在的家庭为:" << endl;
for (const auto tmp : imap) {
cout << "家庭 " << tmp.first << "\n孩子:";
copy(tmp.second.cbegin(), tmp.second.cend(), out);
}
while (1) {
cout << "\n请输入操作:\n1.添加家庭\t2.添加孩子" << endl;
int operate;
cin >> operate;
//选择添加家庭
if (operate == 1) {
cout << "请输入家庭名" << endl;
string fName;
cin >> fName;
cout << "请输入孩子名" << endl;
string cName;
while (cin >> cName) {
//输入结束符
if (cName == "EOF")
break;
imap[fName].push_back(cName);
}
cout << "\n现在的家庭为:" << endl;
for (const auto tmp : imap) {
cout << "\n家庭 " << tmp.first << "\n孩子:";
copy(tmp.second.cbegin(), tmp.second.cend(), out);
}
}
//选择添加孩子
else if (operate == 2) {
cout << "请输入家庭名" << endl;
string fName;
cin >> fName;
if (imap.find(fName) == imap.end()) {
cout << "没有此家庭" << endl;
break;
}
cout << "请输入孩子名" << endl;
string cName;
while (cin >> cName) {
//输入结束符
if (cName == "EOF")
break;
imap[fName].push_back(cName);
}
cout << "\n现在的家庭为:" << endl;
for (const auto tmp : imap) {
cout << "\n家庭 " << tmp.first << "\n孩子:";
copy(tmp.second.cbegin(), tmp.second.cend(), out);
}
}
else
cout << "请正确输入操作数!" << endl;
}
return 0;
}
定义一个map,关键字是家庭的姓,值为vector,保存家中的孩子的名
最新推荐文章于 2023-08-07 13:46:51 发布