I have a problem sorting vectors in 2D vector? I would like to sort them based on their capacity from the largest to the smallest.
Example: ROWS {{1,2,3},{1,2},{1,2,3,4,5}}
it should be sorted as ROWS.capacity(); // ROWS {{1,2,3,4,5},{1,2,3},{1,2}}
The following is part of the code i did until now:
std::vector< std::vector<int> >::iterator row;
std::vector<int>::iterator col;
for (row=ROWS.begin(); row<ROWS.end(); row++){
Logger::logln(LOG_DEBUG, "ROW: %d",row->capacity());
for (col = row->begin(); col != row->end(); col++){
Logger::logln(LOG_DEBUG, " CONTENT: %d ",*col);
}
}
i need to the following:
if(row1.capacity > row2.capacity)
then swap or something like this.
Thanks in advance 🙂
You could use
std::sortwith a custom ordering predicate:This should work well if
std::sortusesstd::swapinternally, otherwise the copying of rows could get quite expensive and you probably need to implement your own sorting function.You should also think, if you really need
capacity()and notsize().