C++ 如何根据结构中的某个字段在c ++中的结构向量中获取最小或最大元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16772477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:37:51  来源:igfitidea点击:

How to get min or max element in a vector of structures in c++, based on some field in the structure?

c++c++11

提问by user2381422

How to get min or max element in a vector of structures in c++, based on some field in the structure?

如何根据结构中的某个字段在c ++中的结构向量中获取最小或最大元素?

For example:

例如:

struct Size {
    int width, height;
};
vector<Size> sizes;

And now I want to solve that based on width and create a new vector for that, and then sort based on height and create a new vector for that.

现在我想根据宽度解决这个问题并为此创建一个新向量,然后根据高度进行排序并为此创建一个新向量。

Thanks

谢谢

采纳答案by Armen Tsirunyan

vector<Size> sizes;
...
vector<Size> sortedByWidths(sizes);
vector<Size> sortedByHeights(sizes);
sort(sortedByWidths.begin(), sortedByWidths.end(), 
    [](Size s1, Size s2) {return s1.width < s2.width;});
sort(sortedByHeights.begin(), sortedByHeights.end(), 
    [](Size s1, Size s2) {return s1.height< s2.height;});

回答by Andy Prowl

In C++11, you can use the std::minmax_element()standard function, which (given a pair of iterators) and possibly a custom comparator (that would allow you to define the field on which the ordering is based), will return you an iterator to the minimum and an iterator to the maximum element, packed in an std::pair.

在 C++11 中,您可以使用std::minmax_element()标准函数,它(给定一对迭代器)和可能的自定义比较器(允许您定义排序所基于的字段),将返回一个迭代器到最小和最大元素的迭代器,打包在std::pair.

So for instance:

所以例如:

#include <algorithm> // For std::minmax_element
#include <tuple> // For std::tie
#include <vector> // For std::vector
#include <iterator> // For global begin() and end()

std::vector<Size> sizes = { {4, 1}, {2, 3}, {1, 2} };

decltype(sizes)::iterator minEl, maxEl;
std::tie(minEl, maxEl) = std::minmax_element(begin(sizes), end(sizes),
    [] (Size const& s1, Size const& s2)
    {
        return s1.width < s2.width;
    });

Here is a live example.

这是一个活生生的例子

回答by juanchopanza

You can use std::min_elementand std::max_elementwith a suitable functor:

您可以使用std::min_elementstd::max_element使用合适的函子:

bool cmp(const Size& lhs, const Size& rhs)
{
  return lhs.width < rhs.width;
}

then

然后

auto min_it = std::min_element(sizes.begin(), sizes.end(), cmp);
auto max_it = std::max_element(sizes.begin(), sizes.end(), cmp);

In C++11 you can replace cmpwith a lambda expression.

在 C++11 中,您可以cmp用 lambda 表达式替换。

See also: std::minmax_element

另见std::minmax_element

回答by Dan

Solution using std::minmax_element with lambda expression:

使用 std::minmax_element 和 lambda 表达式的解决方案:

#include <iostream>
#include <vector>

struct Size {
    int width, height;
};

int main()
{
     std::vector<Size> sizes;

     sizes.push_back({4,1});
     sizes.push_back({2,3});
     sizes.push_back({1,2});

     auto minmax_widths = std::minmax_element(sizes.begin(), sizes.end(),
         [] (Size const& lhs, Size const& rhs) {return lhs.width < rhs.width;});
     auto minmax_heights = std::minmax_element(sizes.begin(), sizes.end(),
         [] (Size const& lhs, Size const& rhs) {return lhs.height < rhs.height;});

     std::cout << "Minimum (based on width): " << minmax_widths.first->width << std::endl;
     std::cout << "Maximum (based on width): " << minmax_widths.second->width << std::endl;

     std::cout << "Minimum (based on height): " << minmax_heights.first->height << std::endl;
     std::cout << "Maximum (based on height): " << minmax_heights.second->height << std::endl;
}

回答by ForEveR

Use std::min/std::max/std::minmax _elementwith comparator. http://en.cppreference.com/w/cpp/algorithm/min_element

std::min/std::max/std::minmax _element与比较器一起使用。 http://en.cppreference.com/w/cpp/algorithm/min_element