【问题标题】:Inserting into map with types <int, vector<int>>插入类型为 <int, vector<int>> 的地图
【发布时间】:2014-02-24 17:39:10
【问题描述】:

我有这个代码:

map< int , vector< int>> testmap;
vector<int> testvector;
testvector.push_back(10);
testmap.insert(1, testvector);

这段代码给了我一个错误,告诉我没有重载函数来匹配参数列表。

谁能告诉我为什么会这样?我正在尝试将矢量插入地图,但这种方法似乎不起作用。

【问题讨论】:

标签: c++ vector map insert


【解决方案1】:

std::map::insert 没有与您传递的参数匹配的重载。这会起作用:

auto p = testmap.insert(std::make_pair(1, testvector));

std::cout << std::boolalpha;
std::cout << "Did insert succeed? " << p.second << std::endl;

如果地图中没有键为1的元素,这将成功。

【讨论】:

    【解决方案2】:
    testmap.insert(1, testvector);
    

    你可能打算这样做

    testmap[1] = testvector;
    

    改为。

    【讨论】:

      【解决方案3】:

      由于您使用的是 C++11(正如您使用 &gt;&gt; 所证明的那样:)),您也可以使用 emplace

      testmap.emplace(1, testvector);
      

      【讨论】:

        【解决方案4】:

        试试

        testmap.insert({1, testvector});
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-01-20
          • 1970-01-01
          • 2016-11-11
          • 1970-01-01
          • 1970-01-01
          • 2014-03-03
          • 1970-01-01
          • 2018-06-28
          相关资源
          最近更新 更多