如何在c++;,基于结构中的某个字段? 如何根据结构中的某个字段,在C++中构造一个向量的MIN或Max元素?

如何在c++;,基于结构中的某个字段? 如何根据结构中的某个字段,在C++中构造一个向量的MIN或Max元素?,c++,c++11,C++,C++11,例如: struct Size { int width, height; }; vector<Size> sizes; struct Size{ int宽度、高度; }; 向量大小; 现在我想根据宽度来解决这个问题,并为它创建一个新的向量, 然后根据高度排序,并为此创建一个新向量 谢谢使用带有比较器的std::min/std::max/std::minmax\u元素。 在C++11中,您可以使用标准函数,该函数(给定一对迭代器)和可能的自定义比较器(允许您定义排序所基于的

例如:

struct Size {
    int width, height;
};
vector<Size> sizes;
struct Size{
int宽度、高度;
};
向量大小;
现在我想根据宽度来解决这个问题,并为它创建一个新的向量, 然后根据高度排序,并为此创建一个新向量


谢谢

使用带有比较器的
std::min/std::max/std::minmax\u元素

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

例如:

#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;
    });
#include.

您可以使用合适的函子并与之配合使用:

bool cmp(const Size& lhs, const Size& rhs)
{
  return lhs.width < rhs.width;
}
在C++11中,可以用lambda表达式替换
cmp

另见

向量大小;
...
矢量分类宽度(尺寸);
矢量分类重量(尺寸);
排序(sortedByWidths.begin()、sortedByWidths.end(),
[](大小s1,大小s2){返回s1.width
使用带有lambda表达式的std::minmax_元素的解决方案:

#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;
}
#包括
#包括
结构大小{
int宽度、高度;
};
int main()
{
std::向量大小;
大小。推回({4,1});
尺寸。推回({2,3});
大小。推回({1,2});
auto minmax_widths=std::minmax_元素(size.begin(),size.end(),
[](Size const&lhs,Size const&rhs){返回lhs.width难道我不知道其他答案是如何忽略了OP需要2个新的向量lmao的事实吗
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;});
#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;
}