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
我想找到列值的最大值和最小值(不是行值) 優選地使用STL算法
std::max_element, std::min_element
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
我想找到列值的最大值和最小值(不是行值) 優選地使用STL算法
std::max_element, std::min_element
創建一個比較某個列號的自定義函子,例如:
struct column_comparer
{
int column_num;
column_comparer(int c) : column_num(c) {}
bool operator()(const std::vector & lhs, const std::vector & rhs) const
{
return lhs[column_num] < rhs[column_num];
}
};
...
std::vector> v;
...
...//fill it with data
...
int column_num = 3;
int n = (*std::max_element(v.begin(), v.end(), column_comparer(column_num)))[column_num];