定义一个map,关键字是家庭的姓,值为vector,保存家中的孩子的名

#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;

    }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值