C++ Primer 习题11.7

来源:互联网 发布:怎么弄公司企业域名 编辑:程序博客网 时间:2024/06/05 22:45
#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格式

原创粉丝点击