如何使用STL算法找到最大和列值的最小值二维数组 [英] How to find max and min values of columns values in a 2D array using STL algorithms

查看:752
本文介绍了如何使用STL算法找到最大和列值的最小值二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维数组(整数向量的向量)为int值,如这些

34 19 89 45
21 34 67 32
87 12 23 18

我想找到一个最大值和最小值的列值(不是行值) pferably使用STL算法$ P $

 的std :: max_element,性病:: min_element
 

解决方案

创建自定义函子来比较特定列数,例如:

 结构column_comparer
{
    INT column_num;
    column_comparer(INT C):column_num(C){}

    布尔运算符()(常量的std ::矢量< INT>&安培; LHS,常量性病::矢量< INT>&放大器; RHS)常量
    {
        返回LHS [column_num] LT; RHS [column_num]
    }
};

...

的std ::矢量<的std ::矢量< INT>>伏;
...
... //用数据填充
...
INT column_num = 3;
INT N =(*的std :: max_element(v.begin(),V.END(),column_comparer(column_num)))[column_num]
 

I have a 2D array ( vector of vector of ints ) with int values such as these

34  19  89  45
21  34  67  32
87  12  23  18

I want to find a max and min value for the column values ( not row values ) preferably using the STL algorithms

std::max_element, std::min_element

解决方案

Create a custom functor that compares a certain column number, e.g.:

struct column_comparer
{
    int column_num;
    column_comparer(int c) : column_num(c) {}

    bool operator()(const std::vector<int> & lhs, const std::vector<int> & rhs) const
    {
        return lhs[column_num] < rhs[column_num];
    }
};

...

std::vector<std::vector<int>> v;
...
... // fill it with data
...
int column_num = 3;
int n = (*std::max_element(v.begin(), v.end(), column_comparer(column_num)))[column_num];

这篇关于如何使用STL算法找到最大和列值的最小值二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆