Sort Vector Vectors

I have

vector<vector<int>> vec 

in my C ++ application.

Each integer vector as an element of the "large" vector has 4 INT values. I want to sort vec based on the third value of its ints content vectors (I mean every "internal" vector third element) - is this possible?

EDIT

Say I have a function

 COST(vector<int>) 

which calculates me some value based on my vector values ​​- can I use it also in the comparison parameter? That would help me a lot more.

+8
c ++ vector stl
source share
3 answers

Of course. std::sort can take a third parameter, which is used for comparison when sorting. For example, you can use the lambda function:

 std::vector<std::vector<int>> vec; // Fill it std::sort(vec.begin(), vec.end(), [](const std::vector<int>& a, const std::vector<int>& b) { return a[2] < b[2]; }); 

Alternatively, you can pass anything that might be caused by a bool(const std::vector<int>&, const std::vector<int>&) signature bool(const std::vector<int>&, const std::vector<int>&) , for example, a pointer to a functor or function.


Editing response: just apply the COST function to a and b :

 std::sort(vec.begin(), vec.end(), [](const std::vector<int>& a, const std::vector<int>& b) { return COST(a) < COST(b); }); 
+21
source share

If you want to compare two vectors in cost, try the following:

 bool predicate(const std::vector<int>& a, const std::vector<int>& b) { return COST(a) < COST(b); } 

Notes:

  • The above works with C ++ 98, I'm also not sure how widespread the use of C ++ 11 is, and whether you have a compatible compiler. Otherwise, you can of course use the lambda expression, as sftrabbit suggested.
  • You do not say that COST returns, I just assumed some specific value like float or long.
  • Hope you don't copy the vector when passing it to COST (), which would be terribly inefficient.
  • COST offers a macro, like all UPPERCASE_NAMES. Do not use macros. Do not use macro names for functions.
+4
source share
 #include <vector> #include <algorithm> #include <cstdlib> #include <ctime> using namespace std; // This makes the sort be according to column 2 and ascending bool sortFunc( const vector<int>& p1, const vector<int>& p2 ) { return p1[1] < p2[1]; } int main() { srand(time(NULL)); // Creates and initializes 10 x 4 vector vector< vector<int> > vec; for( int i=0; i<10; i++ ) { vector<int> 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<vec.size(); i++ ) { for( int j=0; j<vec[i].size(); j++ ) { cout << vec[i][j] << " "; } cout << endl; } cout << endl; // Do the sorting according to column 2 sort(vec.begin(), vec.end(), sortFunc); // Print out the post-sorted vector cout << "Post-sorting state:" << endl; for( int i=0; i<vec.size(); i++ ) { for( int j=0; j<vec[i].size(); j++ ) { cout << vec[i][j] << " "; } cout << endl; } return 0; } 

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

0
source share

All Articles