【问题标题】:Method of inserting into a map in c++11 and c++17?在 c++11 和 c++17 中插入地图的方法?
【发布时间】:2020-05-18 15:23:13
【问题描述】:

我用

创建了一张地图
std::map<long,std::vector<long> > indices

当尝试插入一个键和向量元素时

indices.insert(std::pair<long,std::vector<long> >(0,{0,1,2})); 

我收到大量以

结尾的错误
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_map.h:685:9: note:   template argument deduction/substitution failed:
Main.cpp:44:66: note:   candidate expects 2 arguments, 1 provided
     indices.insert(std::pair<long,std::vector<long> > (0,{0,1,2}));

如果在编译期间我这样做了

g++ Main.cpp -std=c++17

但是它编译没有任何错误

g++ Main.cpp -std=c++11

c++17 中的过程是否不同?如果是,那是什么? 还是我错过了什么?

【问题讨论】:

  • 您应该使用std::pair&lt;const long,std::vector&lt;long&gt; &gt;。该对的第一个元素 (value_type) 是一个 const 对象。
  • 如果我使用 -std=c++17 编译仍然显示相同的错误
  • 无法复制godbolt.org/z/MCEx9r
  • 不知道我做错了什么,编译器错误?
  • 可能是您发布的行上方的错误。这些位置并不总是准确的

标签: c++ stl


【解决方案1】:

您上面的insert(std::pair&lt;long,std::vector&lt;long&gt; &gt;(0,{0,1,2})) 看起来不错。如果它是 GNU C++ 17,我认为它应该可以工作,因为它在我的情况下工作。

我认为以下内容现在也可以在 C++17 中使用,

indices.insert({ {0},{{0,1,2}} }); 

C++ 17 引入了一个新的重载以允许 insert( { {key}, {value, args} } ) 语法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多