自学内容网 自学内容网

C++ //练习 11.22 给定一个map<string, vector<int>>,对此容器的插入一个元素的insert版本,写出其参数类型和返回类型。

C++ Primer(第5版) 练习 11.22

练习 11.22 给定一个map<string, vector<int>>,对此容器的插入一个元素的insert版本,写出其参数类型和返回类型。

环境:Linux Ubuntu(云服务器)
工具:vim

 

代码块
/*************************************************************************
> File Name: ex11.22.cpp
> Author: 
> Mail: 
> Created Time: Sun 07 Apr 2024 02:18:57 PM CST
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<string>
#include<vector>
#include<map>
#include<iterator>
using namespace std;

int main(){
    map<string, vector<int>> map;
    string word;
    int count;

    while(cin>>word>>count){
        vector<int> num;
        num.push_back(count);
        auto ret = map.insert({word, num});
        if(!ret.second){
            ret.first->second.push_back(count);
        }
    }

    cout<<"word"<<"       "<<"page"<<endl;
    for(const auto m : map){
        cout<<setw(8)<<left<<m.first<<"    ";
        for(const auto w : m.second){
            cout<<w<<" ";
        }
        cout<<endl;
    }

    return 0;
}
解释

参数类型为pair<string, vector<int>>,返回类型为pair<map<string, vector<int>>::iterator, bool>。

运行结果显示如下

在这里插入图片描述


原文地址:https://blog.csdn.net/navicheung/article/details/137462444

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!