Map练习2—Primer1132

来源:互联网 发布:网络时间服务器地址 编辑:程序博客网 时间:2024/06/01 19:03
/*************************************************************************    > File Name: main1131.cpp    > Author:keson    > Mail:keson@bupt.edu.cn    > Created Time: 2014年11月01日 星期六 12时19分48秒 ************************************************************************/#include<iostream>using namespace std;#include<map>#include<string>#include<vector>multimap<string,vector<string>> authors;void Authors_Set();void Authors_Print();void Authors_Erase();int main(){    Authors_Set();    Authors_Print();    Authors_Erase();    Authors_Print();    return 0;}void Authors_Set(){    cout<<"Enter the number of authors: "<<endl;    int num=0;    cin>>num;    vector<vector<string>> vec1(num);                  //初次设定vector大小,为了下面的下标操作不溢出,vector元素是vector<string>,用来保存每个作者的书    for(int i=0;i!=num;++i)    {        cout<<"Enter the name of the author and books:"<<endl;        string name;        string book;        cin>>name;        while(cin>>book)        {           vec1.at(i).push_back(book);               //对vec1容器中的vector对象插入对应的string        }             authors.insert(make_pair(name,vec1[i]));     //插入string和对应的vector对象到multimap                       //为了防止ctrl+影响下次的cin读入        cin.clear();                                //更改cin的状态标示符        cin.sync();                                 //清除缓存区的数据流的    }}void Authors_Erase(){    cout<<"Enter the name of author you want to erase:"<<endl;    string s;    cin>>s;    auto n=authors.erase(s);    cout<<"ERASE OK"<<endl;}void Authors_Print() {     for(auto &c:authors)    {        cout<<c.first<<" ";        for(auto &b:c.second)        {            cout<<b<<" ";        }        cout<<endl;    }}


0 0
原创粉丝点击