#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
using namespace std;
int main(){
map<string, vector<string>> family = {{ "Green",{"Blue","Terry","Eric"}},{ "White",{"Terry","Eric" }}};;
cout << "The Families Are: " << endl;
for (const auto &s : family) {
cout << s.first << endl;
string f = s.first;
for_each(s.second.cbegin(), s.second.cend(), [f](const string &a) {cout << f + a << " ";});
cout << endl;
}
cout << endl;
//assume each family has the different first_name
string first_name;
cout << "Now enter names, word by word in lines: " << endl;
string person;
while(getline(cin, person)){
auto pos = person.find_first_of(" ");
first_name = person.substr(0, pos);
person.erase(0, pos+1);
family[first_name].push_back(person);
}
cout << endl;
cout << "Modified! Now The Families Are: " << endl;
for(const auto &s : family){
cout << s.first << endl;
string f = s.first;
for_each(s.second.cbegin(), s.second.cend(), [f](const string &a){cout << f+a << " ";});
cout << endl;
}
system("pause");
return 0;
}
输入时为Brown Eric格式,逐行输入名字
输出时为一个家庭一行,BrownEirc格式