如何在C++中使用map作为multiset中的数据类型
How to use map as data type inside multiset in C++
我正在尝试为multiset使用映射数据类型结构。这是我在下面的代码
#include <iostream>
#include <set>
#include <iterator>
#include <map>
#include <functional>
using namespace std;
int main()
{
map<string, int> student_marks_map;
student_marks_map["Student1"] = 100;
student_marks_map["Student2"] = 20;
student_marks_map["Student3"] = 230;
multiset<int, greater<int>> multiset1 = {1,2,3,2,10};
multiset<int, greater<int>>::iterator itr;
//multiset1.insert(1);
for(itr = multiset1.begin(); itr != multiset1.end(); itr++)
{
cout<<*itr<<endl;
}
multiset<map<int, int>> multiset_map;
multiset_map.insert(1,4);
/*for(const auto& iter: multiset1)
{
cout<< iter <<endl;
}*/
return 0;
}
我知道有一种多映射类型的数据结构,但我想在多集中实现它。当我尝试插入一个元素时,它会显示编译错误。
这是什么原因?我试着在网上搜索,但似乎没有人做过这种事
错误:
In file included from /usr/include/c++/7/set:60:0,
from main.cpp:2:
/usr/include/c++/7/bits/stl_tree.h: In instantiation of ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_equal(_II, _II) [with _InputIterator = int; _Key = std::map<int, int>; _Val = std::map<int, int>; _KeyOfValue = std::_Identity<std::map<int, int> >; _Compare = std::less<std::map<int, int> >; _Alloc = std::allocator<std::map<int, int> >]’: /usr/include/c++/7/bits/stl_multiset.h:542:4: required from ‘void std::multiset<_Key, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = int; _Key = std::map<int, int>; _Compare = std::less<std::map<int, int> >; _Alloc = std::allocator<std::map<int, int> >]’ <span class="error_line" onclick="ide.gotoLine('main.cpp',26)">main.cpp:26:28</span>: required from here /usr/include/c++/7/bits/stl_tree.h:2464:28: error: invalid type argument of unary ‘*’ (have ‘int’) _M_insert_equal_(end(), *__first, __an);
multiset
的两个参数insert
成员都没有从参数中构造值,即使它们构造了值,1, 4
也无法初始化map<int, int>
。
如果您想要一个具有单个元素1, 4
的地图,您需要指定
multiset_map.insert(map<int, int>{{1,4}});
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 将Ref对象作为类成员
- 虚拟决赛作为安全
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 使用指向成员的指针将成员函数作为参数传递
- 我应该使用什么来代替void作为变体中的替代类型之一
- 何时提供默认参数作为模板参数
- 如何将 I->getType() 作为参数传递给 llvm 中的 CreateCall?
- C++使用数组作为多维数组,尽管将其初始化为带有指针的 1D
- C++匿名结构作为std::映射值
- C++:如何使函数只返回作为列表一部分的字符串
- 如何制作一个将函数作为参数的类方法
- 如何在C++中使用非静态成员函数作为回调函数
- 修改函数中的指针(将另一个指针作为参数传递)
- Clang bug?使用指针作为模板参数
- 函数作为模板参数,是否对返回类型强制约束
- 在C++中传递给函数时,为什么要指定数组大小作为参数
- 为什么 Clang 不允许"and"作为函数名称?
- 为什么 std::function 可以作为 std::not2 的参数?