Sort vector of vectors

后端 未结 4 1622
离开以前
离开以前 2021-02-13 22:20

I have

    vector> vec 

in my c++ app.

Every vector of integers as an element of \"big\" vector has 4 INT valu

4条回答
  •  抹茶落季
    2021-02-13 22:43

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    // This makes the sort be according to column 2 and ascending
    bool sortFunc( const vector& p1,
               const vector& p2 ) {
     return p1[1] < p2[1];
     }
    
    int main() {
    
      srand(time(NULL));
    
      // Creates and initializes 10 x 4 vector
      vector< vector > vec;
      for( int i=0; i<10; i++ ) {
       vector tmpVec;
       for( int j=0; j<2; j++ ) {
      tmpVec.push_back( rand()%10 );
       }
       vec.push_back( tmpVec );
      }
    
      // Print out the pre-sorted vector
     cout << "Pre-sorting state:" << endl;
      for( int i=0; i

    source: https://shihho.wordpress.com/2012/11/28/sort_with_vectors/

提交回复
热议问题