清泉逐流

做着努力,等待幸福到来

二维vector的使用和排序

作者:Eamonn 时间 : 2014-09-30 19:48 分类:C/C++

#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
// Binary function that accepts two elements in the range as arguments,
// and returns a value convertible to bool. The value returned indicates
// whether the element passed as first argument is considered to go
// before the second in the specific strict weak ordering it defines.
// The function shall not modify any of its arguments.
// This can either be a function pointer or a function object.
struct SORT{
    inline bool operator()(vector<int> const &e1, vector<int> const &e2)
    {
        if(e1[1] == e2[1]){
            return e1[2] < e2[2];
        }else{
            return e1[1] < e2[1];
        }
    }
}op;
int main()
{
       
    vector<vector<int>> v( 3, vector<int>( 3, 0 ) );
       
       
    v[0][0] = 1;
    v[0][1] = 2;
    v[0][2] = 3;
       
    v[1][0] = 1;
    v[1][1] = 1;
    v[1][2] = 1;
       
    v[2][0] = 1;
    v[2][1] = 2;
    v[2][2] = 2;
       
    sort(v.begin(), v.end(), op);
       
    for( int j=0; j<v.size(); j++ ) {
        for ( int i=0; i<v.at(0).size(); i++ ) {
            std::cout<< v[j][i] << " ";
        }
        std::cout<< "\n";
    }
       
       
    return 0;
}


转载注明: http://www.eamonning.com/note/view/40
» 笔记大类