My comparison function for STL sort is failing. I was wanting to pass in a column variable to sort on a column. I am still new to programming so Boost just buries me in more things I don't understand. This doesn't have any error checking etc. eg
int columnToSort = 1;
sort(vec2d.begin(), vec2d.end(),
[columnToSort](const vector< int >& a, const vector< int >& b){ return a[columnToSort] < b[columnToSort]; } );
Error = no matching function for call to 'sort(std::vector >::iterator, std::vector >::iterator, main()::&,...etc..
If I try to make it a class that takes a 2dvector and an integer columnToSort
I get the same error:
The following Does work:
bool compareFunction0(const vector<int>& a, const vector<int>& b )
{
return( a[0] < b[0] );
}
void vec2dSort( vector< vector<int> > &refVec2d, int sortCol )
{
//check to see if sortCol is out of bounds
sort(refVec2d.begin(), refVec2d.end(), compareFunction0 );
}
Full Source is below. Problems areas are /////////////////////////////// MARKED LIKE THIS ////////////////////////////
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
#include <cstdlib> // rand()
#include <ctime> // rand()
using namespace std;
void printVec2d( vector< vector<int> > &refVec2d )
{
vector<int>::iterator itCol;
vector< vector<int> >::iterator itRow;
for(itRow = refVec2d.begin(); itRow != refVec2d.end(); ++itRow)
{
for(itCol = itRow->begin(); itCol != itRow->end(); ++itCol)
cout << (*itRow)[0] << ":" << *itCol << "\t";
cout << endl;
}
}
bool compareFunction0(const vector<int>& a, const vector<int>& b )
{
return( a[0] < b[0] );
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////// THIS WORKS but compareFunction0 has hard coded column////////////////
//////////////////////////////////////////////////////////////////////////////////////
void vec2dSort( vector< vector<int> > &refVec2d, int sortCol )
{
//check to see if sortCol is out of bounds
//sort(refVec2d.begin(), refVec2d.end(), [](const vector< int >& a, const vector< int >& b){ return a[sortCol] < b[sortCol]; } );
sort(refVec2d.begin(), refVec2d.end(), compareFunction0 );
}
class SortVec2d
{
private:
int sortCol;
public:
SortVec2d( vector< vector<int> >&, int);
~SortVec2d();
bool comparison(const vector<int>&, const vector<int>& );
};
bool SortVec2d::comparison(const vector<int>& a, const vector<int>& b )
{
return( a[0] < b[0] ); // I want to do return( a[sortCol] < b[sortCol] )
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////// THIS DOESN'T WORK "no such comparison function"//////////////////////
//////////////////////////////////////////////////////////////////////////////////////
SortVec2d::SortVec2d( vector< vector<int> > &refVec2d, int sortColumn )
{
int sortCol = sortColumn;
//sort(refVec2d.begin(), refVec2d.end(), comparison );
//sort(refVec2d.begin(), refVec2d.end(), [](const vector< int >& a, const vector< int >& b){ return a[0] < b[0]; } );
}
int randBetween(int min, int max)
{
return (rand()%(max-min))+min;
}
int main()
{
vector<int> vecRow ;
vector< vector<int> > vec2d;
int rowSize = 3;
for(int c = 0; c < 10; ++c)
{
for(int r = 0; r < rowSize; ++r)
vecRow.push_back( randBetween(0,255) );
vec2d.push_back(vecRow);
vecRow.clear();
}
cout << "New 2d Vector Created: " << endl;
printVec2d(vec2d);
sort(vec2d.begin(), vec2d.end(), [](const vector< int >& a, const vector< int >& b){ return a[1] < b[1]; } );
cout << "2d Vector Sorted on Col 1: " << endl;
printVec2d(vec2d);
vec2dSort( vec2d, 0);
cout << "2d Vector Sorted on Col 0: " << endl;
printVec2d(vec2d);
return 0;
}
Use a capture in your lambda to accept the column number to sort on.
int columnToSort = 2;
sort(vec2d.begin(), vec2d.end(), [columnToSort](const vector< int >& a, const vector< int >& b)
{
return a[columnToSort] < b[columnToSort];
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.