【问题标题】:Unable to insert to std map无法插入到标准映射
【发布时间】:2020-05-16 09:23:27
【问题描述】:

我有一个由 int 和指向结构的指针组成的标准映射。但是使用插入功能我无法添加相同的功能。错误消息对我来说似乎有点太棘手了。

#include <iostream>
#include <map>
using namespace std;

typedef struct 
{

}NODE;

int main()
{
    std::map<int, NODE*> mp;
    int i = 0;
    mp.insert(i, new NODE());

    return 0;
}

错误语言有点神秘。我正在尝试将一个 int 和一个 NODE 指针添加到 int 和 NODE 指针的映射。为什么这给了我不匹配的功能?

main.cpp: In function ‘int main()’:
main.cpp:22:28: error: no matching function for call to ‘std::map::insert(int&, NODE*)’
     mp.insert(i, new NODE());
                            ^
In file included from /usr/include/c++/7/map:61:0,
                 from main.cpp:10:
/usr/include/c++/7/bits/stl_map.h:621:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::insert_return_type std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::node_type&&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; std::map<_Key, _Tp, _Compare, _Alloc>::insert_return_type = std::_Node_insert_return >, std::_Node_handle, std::allocator > > > >; std::map<_Key, _Tp, _Compare, _Alloc>::node_type = std::_Node_handle, std::allocator > > >]
       insert(node_type&& __nh)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:621:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/7/bits/stl_map.h:626:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, std::map<_Key, _Tp, _Compare, _Alloc>::node_type&&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::node_type = std::_Node_handle, std::allocator > > >]
       insert(const_iterator __hint, node_type&& __nh)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:626:7: note:   no known conversion for argument 1 from ‘int’ to ‘std::map::const_iterator {aka std::_Rb_tree_const_iterator >}’
/usr/include/c++/7/bits/stl_map.h:795:7: note: candidate: std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; typename std::_Rb_tree<_Key, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair]
       insert(const value_type& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:795:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/7/bits/stl_map.h:802:7: note: candidate: std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::value_type&&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; typename std::_Rb_tree<_Key, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair]
       insert(value_type&& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:802:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/7/bits/stl_map.h:808:2: note: candidate: template std::__enable_if_t, _Pair>::value, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator, bool> > std::map<_Key, _Tp, _Compare, _Alloc>::insert(_Pair&&) [with _Pair = _Pair; _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >]
  insert(_Pair&& __x)
  ^~~~~~
/usr/include/c++/7/bits/stl_map.h:808:2: note:   template argument deduction/substitution failed:
main.cpp:22:28: note:   candidate expects 1 argument, 2 provided
     mp.insert(i, new NODE());
                            ^
In file included from /usr/include/c++/7/map:61:0,
                 from main.cpp:10:
/usr/include/c++/7/bits/stl_map.h:822:7: note: candidate: void std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::initializer_list >) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >]
       insert(std::initializer_list<value_type> __list)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:822:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/7/bits/stl_map.h:852:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, const value_type&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair]
       insert(const_iterator __position, const value_type& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:852:7: note:   no known conversion for argument 1 from ‘int’ to ‘std::map::const_iterator {aka std::_Rb_tree_const_iterator >}’
/usr/include/c++/7/bits/stl_map.h:862:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, std::map<_Key, _Tp, _Compare, _Alloc>::value_type&&) [with _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair]
       insert(const_iterator __position, value_type&& __x)
       ^~~~~~
/usr/include/c++/7/bits/stl_map.h:862:7: note:   no known conversion for argument 1 from ‘int’ to ‘std::map::const_iterator {aka std::_Rb_tree_const_iterator >}’
/usr/include/c++/7/bits/stl_map.h:867:2: note: candidate: template std::__enable_if_t, _Pair>::value, typename std::_Rb_tree<_Key, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator> std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, _Pair&&) [with _Pair = _Pair; _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >]
  insert(const_iterator __position, _Pair&& __x)
  ^~~~~~
/usr/include/c++/7/bits/stl_map.h:867:2: note:   template argument deduction/substitution failed:
main.cpp:22:28: note:   cannot convert ‘i’ (type ‘int’) to type ‘std::map::const_iterator {aka std::_Rb_tree_const_iterator >}’
     mp.insert(i, new NODE());
                            ^
In file included from /usr/include/c++/7/map:61:0,
                 from main.cpp:10:
/usr/include/c++/7/bits/stl_map.h:885:2: note: candidate: template void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >]
  insert(_InputIterator __first, _InputIterator __last)
  ^~~~~~
/usr/include/c++/7/bits/stl_map.h:885:2: note:   template argument deduction/substitution failed:
main.cpp:22:28: note:   deduced conflicting types for parameter ‘_InputIterator’ (‘int’ and ‘NODE*’)
     mp.insert(i, new NODE());

【问题讨论】:

  • mp[i]=new Node();
  • mp.insert({i, new NODE()});
  • 对于您传递的参数,insert 没有重载,请参见此处en.cppreference.com/w/cpp/container/map/insert
  • 请阅读给定的链接。您只需尝试使用不存在的接口。因此,您必须将正确的参数传递给现有接口。 insert 可以用 std::pair 调用,但不能用两个单独的参数调用。 {} arround args 在这种情况下被隐式转换为 std::pair。

标签: c++ c++17 stdmap


【解决方案1】:

如果您使用的是 C++11 或更高版本(默认情况下,在最近的编译器中就是这种情况),那么您可以改用 map::emplace 方法。使用 emplace,您可以简单地传递构成对的两个部分的两个参数,如下所示:

#include <iostream>
#include <string>
#include <map>

int main() {
    std::map<int, std::string> m;
    m.emplace(123, "ddd");
    for (const auto &p : m) {
        std::cout << p.first << " => " << p.second << '\n';
    }
}

看到这个running on Godbolt

注意事项:

  • 在我的示例中,我已将 NODE 替换为 std::string,以便您可以看到它正在打印。
  • 如果您使用的是 C++17 或更高版本,您可能更喜欢 map::try_emplace()

【讨论】:

    【解决方案2】:

    当你想把它插入到地图中时,你必须像这样将它插入为std::pair

    mp.insert(std::pair<int, NODE*>(i, new NODE()));
    

    【讨论】:

    • 谢谢。让我想知道是什么阻止了编译器做明显的事情,即查看输入是否是有效的对。
    • @J.Snow:因为重载解析本身就足够复杂,而且编译器太聪明了一半不会是一个改进。 (编译器在产生错误消息时尝试猜测您打算调用的函数可能是有意义的,但这无论如何都非常困难。)
    【解决方案3】:

    啊,好的。

    所以插入函数需要一对。

    /usr/include/c++/7/bits/stl_map.h:802:7: note:   candidate expects 1 argument, 2 provided
    /usr/include/c++/7/bits/stl_map.h:808:2: note: candidate: template std::__enable_if_t, _Pair>::value, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind >::other>::iterator, bool> > std::map<_Key, _Tp, _Compare, _Alloc>::insert(_Pair&&) [with _Pair = _Pair; _Key = int; _Tp = NODE*; _Compare = std::less; _Alloc = std::allocator >]
      insert(_Pair&& __x)
    

    感谢以上建议。

      mp.insert(std::make_pair(i, p));
    

    【讨论】:

      【解决方案4】:

      如果你想在 std::map 中插入一个元素,你可以使用方法 emplace ( https://www.cplusplus.com/reference/map/map/emplace/ ) 将 std::make_pair 构造函数参数直接传递给它。

      #include <iostream>
      #include <map>
      using namespace std;
      
      typedef struct 
      {
      }NODE;
      
      
      int main()
      {
          std::map<int, NODE*> mp;
          int i = 0;
          mp.emplace(i, new NODE());
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-21
        • 2018-08-08
        相关资源
        最近更新 更多