我的问题很简单,我想要一个集合映射,其中集合使用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); // works fine
map1[0] = set<int>(); // works fine
map2[0] = set<int, decltype(comp)>(comp); // compile error
map2[0] = set1; // compile error
有些语法是错误的,我不知道,也无法在网络上找到正在做我想做的任何资源
编译器错误是一条很长的消息,我在下面粘贴了完整的图片。
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.
map2[0]
尝试默认构造set<int, decltype(comp)>
的实例,但是该类没有默认构造函数。改为使用insert
或emplace
,如