在c++中插入多对

Insert more than one pair in c++

本文关键字:插入 c++      更新时间:2023-10-16

我有一个这样的地图:

map<string, map<int, int>> collector;  

我不知道如何在地图中插入数据。如果我有

map<string, int> collector;

只有键值,我会使用

collector.insert(pair<string, int>)(name,money));

但是,当我们在地图中有地图时,插入的方式是什么呢。我试着做:

typedef map<int, int> map_;
  for(iteration = collector.begin(); iteration != collector.end(); iteration++) {
    iteration = collector.find(word);
    if(iteration == collector.end()) {
        iteration = collector.insert(map_::value_type(num,num).first;
    }
}

这种方式对我不起作用。

以下是插入数据结构的一些方法:

#include <iostream>  // cout
#include <map>
#include <string>
#include <utility>  // make_pair
using namespace std;
int main()
{
    using Collector = map<string, map<int, int>>;
    Collector collector;
    collector["USD"] = Collector::mapped_type{ { 1, 3 }, { 0, 8 } };
    collector["EUR"].insert(make_pair(4, 5));
    collector["EUR"].insert(make_pair(6, 7));
    collector["CHF"][2] = 4;
    const Collector::mapped_type jpyIntegers { { 10, 20 }, { 100, 200 } };
    collector.insert(make_pair("JPY", jpyIntegers));
    collector["MMK"];
    for (const auto& a: collector) {
        cout << a.first << ": ";
        for (const auto& i: a.second) {
            cout << "(" << i.first << "|" << i.second << ")";
        }
        cout << "n";
    }
}