[英]Sorting vector of vectors without comparator or lambda function?
Today I came across some C++ code which I thought was not going to compile:今天我遇到了一些我认为不会编译的 C++ 代码:
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<vector<int>> vectorOfVectors = { { 2, 3, 5 }, { 1, 2, 3 } };
sort(vectorOfVectors.begin(), vectorOfVectors.end());
return 0;
}
As far as I know, there is no default comparator in C++ for vectors of ints, so one would have to implement a custom comparator or lambda function in order to pass it to the sort() function.据我所知,C++ 中没有用于整数向量的默认比较器,因此必须实现自定义比较器或 lambda 函数才能将其传递给 sort() 函数。
However, the fact that this code compiled made me want to ask this question;然而,这段代码编译的事实让我想问这个问题; is there a default comparator for vectors of ints?
是否有整数向量的默认比较器? Is there one for floats, doubles, and so on?
有没有花车、双打等的? Or does the compiler automatically generate it?
还是编译器会自动生成? It should be noted that this way of sorting a vector of vectors is nowhere to be found online.
需要注意的是,这种对向量的向量进行排序的方式在网上是找不到的。
Thanks in advance!提前致谢!
From cppreference on std::sort
, for the overload void sort( RandomIt first, RandomIt last );
从
std::sort
上的 cppreference ,对于重载void sort( RandomIt first, RandomIt last );
: :
1) Elements are compared using
operator<
.1) 使用
operator<
比较元素。
std::vector<T>
providesoperator<
. std::vector<T>
提供operator<
。 Its behavior is :它的行为是:
Compares the contents of
lhs
andrhs
lexicographically.按字典顺序比较
lhs
和rhs
的内容。 The comparison is performed by a function equivalent to std::lexicographical_compare.比较由等效于 std::lexicographical_compare 的函数执行。
The behavior of the overload of std::lexicographical_compare
that doesn't take a comparator is :不带比较器的
std::lexicographical_compare
重载的行为是:
1) Elements are compared using
operator<
.1) 使用
operator<
比较元素。
So as long as the type T
in std::vector<T>
is comparable with operator<
then std::vector<T>
can be compared with operator<
and is thus compatible with std::sort
.因此,只要
std::vector<T>
的类型T
与operator<
具有可比性,那么std::vector<T>
就可以与operator<
进行比较,从而与std::sort
兼容。 Since int
is comparable with operator<
, so is std::vector<int>
and therefore so is std::vector<std::vector<int>>
.由于
int
与operator<
具有可比性,因此std::vector<int>
也是如此,因此std::vector<std::vector<int>>
。 Each of those types will work with std::sort
without an explicit comparator.这些类型中的每一种都可以在没有显式比较器的情况下与
std::sort
使用。
As far as I know, there is no default comparator in C++ for vectors ...
据我所知,C++ 中没有向量的默认比较器......
There are defined comparison operators for vector
s: vector
s 定义了比较运算符:
https://en.cppreference.com/w/cpp/container/vector/operator_cmp https://en.cppreference.com/w/cpp/container/vector/operator_cmp
std::vector
has defined operators == != < <= > >=
. std::vector
已经定义了运算符== != < <= > >=
。
Since it is a template class, you do not need to define operator<
for every possible type that a vector
can hold.由于它是一个模板类,因此您不需要为
vector
可以容纳的每种可能类型定义operator<
。 If the type meets LessThanComparable requirements, it will be generated.如果类型满足LessThanComparable要求,则会生成它。
As int
can obviously be compared with <
, vector<int>
will have operator<
generated for it.由于
int
显然可以与<
进行比较,因此vector<int>
将为它生成operator<
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.