首页 > 解决方案 > 将一对映射到结构

问题描述

我编写的一些代码遇到了麻烦,但我尝试编写它的简化版本以可能重现我遇到的错误:

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

struct test{
    float y;
};

typedef pair<int, int> intpair;
map<intpair, test> mp;

void func(test &obj){
    obj.y = 23.5;
}



int main () {
   test* obj = new test;
   auto key = make_pair(1,1);
   mp.insert(key, obj);
   func(*obj);
   //cout << *(obj->y);
   delete obj;
   return 0;
}

我想做的是将 a 映射到我创建pair的自定义。struct test我收到以下错误:

main.cpp: In function ‘int main()’:
main.cpp:22:22: error: no matching function for call to ‘std::map, test>::insert(std::pair&, test*&)’
    mp.insert(key, obj);
                      ^
In file included from /usr/include/c++/6/map:61:0,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/stdc++.h:81,
                 from main.cpp:2:
/usr/include/c++/6/bits/stl_map.h:731:7: note: candidate: std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Allocator>::rebind >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = std::pair; _Tp = test; _Compare = std::less >; _Alloc = std::allocator, test> >; typename std::_Rb_tree<_Key, std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Allocator>::rebind >::other>::iterator = std::_Rb_tree_iterator, test> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair, test>]
       insert(const value_type& __x)
       ^~~~~~
/usr/include/c++/6/bits/stl_map.h:731:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/6/bits/stl_map.h:739:9: note: candidate: template std::pair, std::_Select1st >, _Compare, typename __gnu_cxx::__alloc_traits<_Allocator>::rebind >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(_Pair&&) [with _Pair = _Pair;  = ; _Key = std::pair; _Tp = test; _Compare = std::less >; _Alloc = std::allocator, test> >]
         insert(_Pair&& __x)
         ^~~~~~
/usr/include/c++/6/bits/stl_map.h:739:9: note:   template argument deduction/substitution failed:
main.cpp:22:22: note:   candidate expects 1 argument, 2 provided
    mp.insert(key, obj);
                      ^
In file included from /usr/include/c++/6/map:61:0,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/stdc++.h:81,
                 from main.cpp:2:
/usr/include/c++/6/bits/stl_map.h:752:7: note: candidate: void std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::initializer_list >) [with _Key = std::pair; _Tp = test; _Compare = std::less >; _Alloc = std::allocator, test> >]
       insert(std::initializer_list<value_type> __list)
       ^~~~~~
/usr/include/c++/6/bits/stl_map.h:752:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/6/bits/stl_map.h:781: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 = std::pair; _Tp = test; _Compare = std::less >; _Alloc = std::allocator, test> >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator, test> >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator, test> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair, test>]
       insert(const_iterator __position, const value_type& __x)
       ^~~~~~
/usr/include/c++/6/bits/stl_map.h:781:7: note:   no known conversion for argument 1 from ‘std::pair’ to ‘std::map, test>::const_iterator {aka std::_Rb_tree_const_iterator, test> >}’
/usr/include/c++/6/bits/stl_map.h:792:9: note: candidate: template std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator, _Pair&&) [with _Pair = _Pair;  = ; _Key = std::pair; _Tp = test; _Compare = std::less >; _Alloc = std::allocator, test> >]
         insert(const_iterator __position, _Pair&& __x)
         ^~~~~~
/usr/include/c++/6/bits/stl_map.h:792:9: note:   template argument deduction/substitution failed:
main.cpp:22:22: note:   cannot convert ‘key’ (type ‘std::pair’) to type ‘std::map, test>::const_iterator {aka std::_Rb_tree_const_iterator, test> >}’
    mp.insert(key, obj);
                      ^
In file included from /usr/include/c++/6/map:61:0,
                 from /usr/include/x86_64-linux-gnu/c++/6/bits/stdc++.h:81,
                 from main.cpp:2:
/usr/include/c++/6/bits/stl_map.h:807:9: note: candidate: template void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Key = std::pair; _Tp = test; _Compare = std::less >; _Alloc = std::allocator, test> >]
         insert(_InputIterator __first, _InputIterator __last)
         ^~~~~~
/usr/include/c++/6/bits/stl_map.h:807:9: note:   template argument deduction/substitution failed:
main.cpp:22:22: note:   deduced conflicting types for parameter ‘_InputIterator’ (‘std::pair’ and ‘test*’)
    mp.insert(key, obj);

另外,我确定我访问我obj的成员 y 的方式出错了(cout 语句已注释掉);我该如何解决这些问题?

标签: c++stlhashmap

解决方案


没有重载std::map::insert需要一个键和一个值。

您可以使用operator[]添加新元素:

   mp[key] = *obj;

或者std::map::insert_or_assign从 C++17 开始:

   mp.insert_or_assign(key, *obj);

另一点是 type ofobjtest*而 map 的值的类型是mptype test。指针应该被取消引用(就像我上面的例子)或者值的类型应该改变为指针来做这个插入。


推荐阅读