2011-05-15 58 views
2

我有一个2D阵列与INT的值,如这些(int型矢量的矢量)
如何使用STL算法

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

我想找到max和列中的值的最小值中的2D阵列找到的列值(未行值)一个最大和最小值 优选使用STL算法

std::max_element, std::min_element 
+3

它是行向量还是列向量?这显然很重要。 – 2011-05-15 01:43:39

+2

你的意思是最大列值?列总和的最大值还是列max的向量? – 2011-05-15 01:44:50

回答

6

创建自定义函子,其比较在一定列数,例如:

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]; 
+1

快速提示:除非您使用C++ 0x,否则您需要在'std :: vector '和'v'声明中的“>”字符后面添加一个额外的空格,以便进行编译。 – Jason 2011-05-15 02:50:22