如何根据属性从向量中提取最大的对象?

发布于 2024-09-30 06:46:30 字数 102 浏览 0 评论 0原文

假设我有一堆 Donut 对象,每个甜甜圈都有一个公共整数属性 diameter。如果我有一个甜甜圈向量,如何提取直径最小或最大的甜甜圈?

Let's say I have a bunch of Donut objects, and each of these donuts has a public integer attribute diameter. If I have a vector of donuts, how can I extract the donut with the smallest or largest diameter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凯凯我们等你回来 2024-10-07 06:46:30

您使用 std::min_elementstd::max_element。例如,给定一个 std::vector

std::vector<int> v;
std::vector<int>::iterator it = std::max_element(v.begin(), v.end());
// 'it' points to the largest element in 'v'

如果您想使用 operator<(默认使用)以外的其他内容来比较元素,则需要编写自定义比较器:

bool compare_donut_diameters(const Donut& x, const Donut& y)
{
    return x.diameter < y.diameter;
}

用作:

std::vector<Donut> v;
std::vector<Donut>::iterator it = std::max_element(v.begin, v.end(), 
                                                   compare_donut_diameters);

您还可以使用函数对象(也称为函子)实现比较器,或者如果您的编译器支持 lambda 表达式,则可以使用 lambda:

auto it = std::max_element(v.begin(), v.end(), 
    [](const Donut& x, const Donut& y) {  return x.diameter < y.diameter; });

You use std::min_element and std::max_element. For example, given a std::vector<int>:

std::vector<int> v;
std::vector<int>::iterator it = std::max_element(v.begin(), v.end());
// 'it' points to the largest element in 'v'

If you want to compare elements using something other than operator< (which is used by default), you need to write a custom comparator:

bool compare_donut_diameters(const Donut& x, const Donut& y)
{
    return x.diameter < y.diameter;
}

Used as:

std::vector<Donut> v;
std::vector<Donut>::iterator it = std::max_element(v.begin, v.end(), 
                                                   compare_donut_diameters);

You can also implement the comparator using a function object (also called a functor) or if your compiler supports lambda expressions you can use a lambda:

auto it = std::max_element(v.begin(), v.end(), 
    [](const Donut& x, const Donut& y) {  return x.diameter < y.diameter; });
勿忘心安 2024-10-07 06:46:30

您可以使用 std::min_element

// You can use a functor instead!
bool compare_donut(const Donut& lhs, const Donut& rhs)
{ return lhs.diameter < rhs.diameter; }
...
// min_donut is an iterator to the smallest donut in donut_vector.
std::vector<Donut>::iterator min_donut = 
   std::min_element(donut_vector.begin(), donut_vector.end(), compare_donut);

如果您想获得最大的,您可以使用 std::max_element 与相同的比较器。

You can use std::min_element:

// You can use a functor instead!
bool compare_donut(const Donut& lhs, const Donut& rhs)
{ return lhs.diameter < rhs.diameter; }
...
// min_donut is an iterator to the smallest donut in donut_vector.
std::vector<Donut>::iterator min_donut = 
   std::min_element(donut_vector.begin(), donut_vector.end(), compare_donut);

If you want to get the largest, you can use std::max_element with the same comparator.

心在旅行 2024-10-07 06:46:30

您可以将 std::max_element/std::min_element 算法与运算符<结合使用使用直径属性或用户提供的比较函数进行比较。

http://www.cplusplus.com/reference/algorithm/max_element/
http://www.cplusplus.com/reference/algorithm/min_element/

You could use the std::max_element/std::min_element algorithm in combination with an operator< that compares using the diameter attribute or an user provided compare function.

http://www.cplusplus.com/reference/algorithm/max_element/
http://www.cplusplus.com/reference/algorithm/min_element/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文