由网友(卑微的等待)分享简介:我有一个二维数组(整数向量的向量)为int值,如这些34 19 89 4521 34 67 3287 12 23 18我想找到一个最大值和最小值的列值(不是行值)pferably使用STL算法$ P $ 的std :: max_element,性病:: min_element解决方案 创建自定义函子来比较特定列数...
我有一个二维数组(整数向量的向量)为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];
相关推荐
最新文章