[英]How to find max and min values of columns values in a 2D array using STL algorithms
I have a 2D array ( vector of vector of ints ) with int values such as these 我有一个2D数组(int的vector),其int值如下
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 我想查找列值(而不是行值)的最大值和最小值,最好使用STL算法
std::max_element, std::min_element
Create a custom functor that compares a certain column number, eg: 创建一个比较特定列号的自定义函子,例如:
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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.