我的STL排序比较函数失败了。我想传入一个column变量来对一个列进行排序。我还是个编程新手,所以Boost只会让我埋头于更多我不理解的事情。这没有任何错误检查等。
int columnToSort = 1;
sort(vec2d.begin(), vec2d.end(),
[columnToSort](const vector< int >& a, const vector< int >& b){ return a[columnToSort] < b[columnToSort]; } );错误=没有匹配的函数调用‘sort( std::vector >::iterator,std::vector >::iterator,main()::&,...etc..
如果我尝试让它成为一个接受2dvector和一个整数columnToSort的类
我得到了相同的错误:
以下方法确实起作用:
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 );
}完整的源代码在下面。问题区域/
#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;
}发布于 2013-12-31 13:32:43
在lambda中使用捕获来接受要排序的列号。
int columnToSort = 2;
sort(vec2d.begin(), vec2d.end(), [columnToSort](const vector< int >& a, const vector< int >& b)
{
return a[columnToSort] < b[columnToSort];
});https://stackoverflow.com/questions/20852114
复制相似问题