2011-05-15 86 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