cpp 排序函数的行为是什么,通过对 vector<vector<int>> 使用默认重载更大的函数
What is behaviour of cpp sort function, by using default overloaded greater function for vector<vector<int>>
sort(begin(ans), end(ans))
与 sort(begin(ans), end(ans), std::greater<std::vector<std::vector<int> > >())
之间的区别
as sort(begin(ans), end(ans))
如预期的那样完美工作,因为指定更大的不起作用。
vector<int> S = {12,13, 34, 9,10};
vector<vector<int> >ans;
vector<int> currSet;
subset(0, S, currSet, ans);
sort(begin(ans), end(ans), std::greater<std::vector<std::vector<int> > >());
cout << ans.size();
}
报错
In file included from subset.cpp:1:
In file included from /usr/local/include/bits/stdc++.h:52:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ccomplex:21:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/complex:247:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/sstream:174:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:138:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:216:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:15:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:505:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:176:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:57:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:3832:17: error: no
matching function for call to object of type
'std::__1::greater<std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >,
std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > > >'
if (__comp(*--__last, *__first))
^~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:4018:5: note: in
instantiation of function template specialization
'std::__1::__sort<std::__1::greater<std::__1::vector<std::__1::vector<int,
std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int,
std::__1::allocator<int> > > > > &, std::__1::vector<int, std::__1::allocator<int> >
*>' requested here
__sort<_Comp_ref>(__first, __last, __comp);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:4052:12: note: in
instantiation of function template specialization
'std::__1::sort<std::__1::vector<int, std::__1::allocator<int> > *,
std::__1::greater<std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >,
std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > > > &>'
requested here
_VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
^
subset.cpp:33:3: note: in instantiation of function template specialization
'std::__1::sort<std::__1::vector<int, std::__1::allocator<int> >,
std::__1::greater<std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >,
std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > > > >'
requested here
sort(all(ans), greater<vector<vector<int> > >());
^
std::greater<std::vector<std::vector<int> > >
用于比较 std::vector<std::vector<int> >
类型的对象。但是向量 ans
不包含该类型的对象。它包含 std::vector<int>
类型的对象。因此,您需要的比较器是 std::greater<std::vector<int>>
。或者,您可以简单地使用 std::greater<>
,它更易于阅读、编写并且不太可能导致错误。
std::greater
的模板参数需要是迭代器指向的元素类型。由于 std::vector<std::vector<int>>
的迭代器具有 std::vector<int>
的 value_type
(这是 ans
中元素的类型),这就是您所需要的。那给你
sort(begin(ans), end(ans), std::greater<std::vector<int> >());
随心所欲。
另请注意,模板参数列表中的结束 >
不再需要 space。您可以使用
sort(begin(ans), end(ans), std::greater<std::vector<int>>());
这将在 C++11+ 兼容的编译器中编译。
sort(begin(ans), end(ans))
与 sort(begin(ans), end(ans), std::greater<std::vector<std::vector<int> > >())
as sort(begin(ans), end(ans))
如预期的那样完美工作,因为指定更大的不起作用。
vector<int> S = {12,13, 34, 9,10};
vector<vector<int> >ans;
vector<int> currSet;
subset(0, S, currSet, ans);
sort(begin(ans), end(ans), std::greater<std::vector<std::vector<int> > >());
cout << ans.size();
}
报错
In file included from subset.cpp:1:
In file included from /usr/local/include/bits/stdc++.h:52:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ccomplex:21:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/complex:247:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/sstream:174:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ostream:138:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:216:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:15:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:505:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:176:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:57:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:3832:17: error: no
matching function for call to object of type
'std::__1::greater<std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >,
std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > > >'
if (__comp(*--__last, *__first))
^~~~~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:4018:5: note: in
instantiation of function template specialization
'std::__1::__sort<std::__1::greater<std::__1::vector<std::__1::vector<int,
std::__1::allocator<int> >, std::__1::allocator<std::__1::vector<int,
std::__1::allocator<int> > > > > &, std::__1::vector<int, std::__1::allocator<int> >
*>' requested here
__sort<_Comp_ref>(__first, __last, __comp);
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:4052:12: note: in
instantiation of function template specialization
'std::__1::sort<std::__1::vector<int, std::__1::allocator<int> > *,
std::__1::greater<std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >,
std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > > > &>'
requested here
_VSTD::sort<_Tp*, _Comp_ref>(__first.base(), __last.base(), __comp);
^
subset.cpp:33:3: note: in instantiation of function template specialization
'std::__1::sort<std::__1::vector<int, std::__1::allocator<int> >,
std::__1::greater<std::__1::vector<std::__1::vector<int, std::__1::allocator<int> >,
std::__1::allocator<std::__1::vector<int, std::__1::allocator<int> > > > > >'
requested here
sort(all(ans), greater<vector<vector<int> > >());
^
std::greater<std::vector<std::vector<int> > >
用于比较 std::vector<std::vector<int> >
类型的对象。但是向量 ans
不包含该类型的对象。它包含 std::vector<int>
类型的对象。因此,您需要的比较器是 std::greater<std::vector<int>>
。或者,您可以简单地使用 std::greater<>
,它更易于阅读、编写并且不太可能导致错误。
std::greater
的模板参数需要是迭代器指向的元素类型。由于 std::vector<std::vector<int>>
的迭代器具有 std::vector<int>
的 value_type
(这是 ans
中元素的类型),这就是您所需要的。那给你
sort(begin(ans), end(ans), std::greater<std::vector<int> >());
随心所欲。
另请注意,模板参数列表中的结束 >
不再需要 space。您可以使用
sort(begin(ans), end(ans), std::greater<std::vector<int>>());
这将在 C++11+ 兼容的编译器中编译。