C++11 如何在set使用比较器的情况下创建set的STL映射
我的问题很简单,我想要一个集合的映射,集合使用lambda比较器C++11 如何在set使用比较器的情况下创建set的STL映射,c++11,lambda,stl,set,comparator,C++11,Lambda,Stl,Set,Comparator,我的问题很简单,我想要一个集合的映射,集合使用lambda比较器 auto comp = [](const int& i, const int& j) { return i < j; }; map<int, set<int>> map1; map<int, set<int, decltype(comp)>> map2; set<int, decltype(comp)> set1(comp);
auto comp = [](const int& i, const int& j) {
return i < j;
};
map<int, set<int>> map1;
map<int, set<int, decltype(comp)>> map2;
set<int, decltype(comp)> set1(comp); // works fine
map1[0] = set<int>(); // works fine
map2[0] = set<int, decltype(comp)>(comp); // compile error
map2[0] = set1; // compile error
auto comp=[](常量int&i,常量int&j){
返回i
有一些语法是错误的,我不知道,我也找不到任何资源在网上做我想做的事
编译器错误是一条很长的消息,我在下面粘贴了完整的图片
In file included from a.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/set:458:19: error: no matching constructor for initialization of 'std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> >::value_compare' (aka '(lambda at a.cpp:8:17)')
: __tree_(value_compare()) {}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/tuple:1360:7: note: in instantiation of member function 'std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> >::set' requested here
second(_VSTD::forward<_Args2>(_VSTD::get<_I2>(__second_args))...)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:509:11: note: in instantiation of function template specialization 'std::__1::pair<const int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >::pair<int &&, 0>' requested here
: pair(__pc, __first_args, __second_args,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1825:31: note: in instantiation of function template specialization 'std::__1::pair<const int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >::pair<int &&>' requested here
::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1717:18: note: in instantiation of function template specialization 'std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, void *> >::construct<std::__1::pair<const int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, const std::__1::piecewise_construct_t &, std::__1::tuple<int &&>, std::__1::tuple<> >' requested here
{__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1560:14: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, void *> > >::__construct<std::__1::pair<const int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, const std::__1::piecewise_construct_t &, std::__1::tuple<int &&>, std::__1::tuple<> >' requested here
{__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__tree:2202:20: note: in instantiation of function template specialization 'std::__1::allocator_traits<std::__1::allocator<std::__1::__tree_node<std::__1::__value_type<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, void *> > >::construct<std::__1::pair<const int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, const std::__1::piecewise_construct_t &, std::__1::tuple<int &&>, std::__1::tuple<> >' requested here
__node_traits::construct(__na, _NodeTypes::__get_ptr(__h->__value_), _VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__tree:2147:29: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, std::__1::__map_value_compare<int, std::__1::__value_type<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, std::__1::less<int>, true>, std::__1::allocator<std::__1::__value_type<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > > > >::__construct_node<const std::__1::piecewise_construct_t &, std::__1::tuple<int &&>, std::__1::tuple<> >' requested here
__node_holder __h = __construct_node(_VSTD::forward<_Args>(__args)...);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:1431:20: note: in instantiation of function template specialization 'std::__1::__tree<std::__1::__value_type<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, std::__1::__map_value_compare<int, std::__1::__value_type<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > >, std::__1::less<int>, true>, std::__1::allocator<std::__1::__value_type<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > > > >::__emplace_unique_key_args<int, const std::__1::piecewise_construct_t &, std::__1::tuple<int &&>, std::__1::tuple<> >' requested here
return __tree_.__emplace_unique_key_args(__k,
^
a.cpp:17:9: note: in instantiation of member function 'std::__1::map<int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> >, std::__1::less<int>, std::__1::allocator<std::__1::pair<const int, std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> > > > >::operator[]' requested here
map2[0] = set<int, decltype(comp)>(comp);
^
a.cpp:8:17: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
auto comp = [](const int& i, const int& j) {
^
a.cpp:8:17: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
In file included from a.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/set:402:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__tree:1752:14: error: object of type 'std::__1::__compressed_pair<unsigned long, (lambda at a.cpp:8:17)>' cannot be assigned because its copy assignment operator is implicitly deleted
__pair3_ = _VSTD::move(__t.__pair3_);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__tree:1819:5: note: in instantiation of member function 'std::__1::__tree<int, (lambda at a.cpp:8:17), std::__1::allocator<int> >::__move_assign' requested here
__move_assign(__t, integral_constant<bool,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/set:562:21: note: in instantiation of member function 'std::__1::__tree<int, (lambda at a.cpp:8:17), std::__1::allocator<int> >::operator=' requested here
__tree_ = _VSTD::move(__s.__tree_);
^
a.cpp:17:13: note: in instantiation of member function 'std::__1::set<int, (lambda at a.cpp:8:17), std::__1::allocator<int> >::operator=' requested here
map2[0] = set<int, decltype(comp)>(comp);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2208:27: note: copy assignment operator of '__compressed_pair<unsigned long, (lambda at a.cpp:8:17)>' is implicitly deleted because base class '__compressed_pair_elem<(lambda at a.cpp:8:17), 1>' has a deleted copy assignment operator
private __compressed_pair_elem<_T2, 1> {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:2169:50: note: copy assignment operator of '__compressed_pair_elem<(lambda at a.cpp:8:17), 1, true>' is implicitly deleted because base class '(lambda at a.cpp:8:17)' has a deleted copy assignment operator
struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
^
a.cpp:8:17: note: lambda expression begins here
auto comp = [](const int& i, const int& j) {
^
2 errors generated.
a.cpp中包含的文件中的:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/set:458:19:错误:没有匹配的构造函数初始化“std::_u1::set::value_compare”(aka)(lambda at a.cpp:8:17)
:uu树u值u比较()){}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/tuple:1360:7:注意:在成员函数“std::u 1::set::set”的实例化中,此处请求
第二个(_-VSTD::forward(_-VSTD::get(u-second_-args))…)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/utility:509:11:注意:在函数模板专门化的实例化中,此处请求“std::u 1::pair::pair”
:配对(\uuuu pc、\uuu第一个参数、\uuu第二个参数、,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1825:31:注意:在函数模板专门化的实例化中,此处请求“std::u 1::pair::pair”
::新建((void*)\u p)\u向上(\u VSTD::forward(\u args)…);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1717:18:注意:在函数模板专门化的实例化中,此处请求了“std::u 1::allocator::construct”
{{构造({p,{VSTD::forward({args)…);}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/memory:1560:14:注意:在函数模板专门化的实例化中,此处请求了“std::u 1::allocator\u traits::u construct”
{{uuuu构造({uu有\u构造(),
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/u树:2202:20:注意:在函数模板专门化的实例化中,此处请求“std:u 1::allocator\u traits::construct”
__node_traits::construct(u-na,_-NodeTypes::u-get_-ptr(u-h->u-value),_-VSTD::forward(u-args)…);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/u-tree:2147:29:注意:在函数模板专门化的实例化中,此处请求“std:u-1::u-tree::u-construct\u-node”
__node_holder_uuh=uu构造_节点(_VSTD::forward(u args)…);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/map:1431:20:注意:在函数模板专门化的实例化中,std::u 1::u tree::u emplace\u unique\u key\u args在此请求
返回树、模板、唯一键、参数,
^
a、 cpp:17:9:注意:在成员函数“std::u 1::map::operator[]”的实例化中,此处请求
map2[0]=集合(comp);
^
a、 cpp:8:17:注意:候选构造函数(隐式副本构造函数)不可行:需要1个参数,但提供了0
自动补偿=[](常数int&i、常数int&j){
^
a、 cpp:8:17:注意:候选构造函数(隐式移动构造函数)不可行:需要1个参数,但提供了0
在a.cpp中包含的文件中:1:
在/Applications/Xcode.app/Contents/Developer/Toolchains/xcodefault.xctoolchain/usr/include/c++/v1/set:402中包含的文件中:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/u树:1752:14:错误:无法分配“std::u 1::u compressed_pair”类型的对象,因为其复制分配运算符已隐式删除
__pair3=\u VSTD::move(\uu t.\uu pair3);
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/u-tree:1819:5:注意:在成员函数的实例化中,此处请求“std::u-tree::u-move\u-assign”
__move\u assign(\uu t,integral\u constantmap2[0]
尝试默认构造set
的实例,但该类没有默认构造函数。请改用insert
或emplace
map2.emplace(0, set<int, decltype(comp)>(comp));
map2.emplace(0,set(comp));