在指定变量的结构 std::vector 中查找最大值
Find max value in std::vector of structure of specified variable
我有下面描述的结构向量:
struct Point {
double x,y;
};
现在我有 vector<Point>
,其中包含大约 2000 个元素。我想在 Point
.
中找到包含变量 y
最大值的元素
我知道有 std::max_element 但我不知道它是否适用于存储在 vector 内部结构中的变量。
I don't know if it works with variables stored in structures inside vector.
是的。使用自定义比较器:
auto it = std::max_element(v.begin(),
v.end(),
[](const auto& a,const auto& b) {
return a.y < b.y;
});
我有下面描述的结构向量:
struct Point {
double x,y;
};
现在我有 vector<Point>
,其中包含大约 2000 个元素。我想在 Point
.
y
最大值的元素
我知道有 std::max_element 但我不知道它是否适用于存储在 vector 内部结构中的变量。
I don't know if it works with variables stored in structures inside vector.
是的。使用自定义比较器:
auto it = std::max_element(v.begin(),
v.end(),
[](const auto& a,const auto& b) {
return a.y < b.y;
});