首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用类对列进行C++排序2d向量失败(prob w/ compare函数)

使用类对列进行C++排序2d向量失败(prob w/ compare函数)
EN

Stack Overflow用户
提问于 2013-12-31 13:17:18
回答 1查看 418关注 0票数 0

我的STL排序比较函数失败了。我想传入一个column变量来对一个列进行排序。我还是个编程新手,所以Boost只会让我埋头于更多我不理解的事情。这没有任何错误检查等。

代码语言:javascript
复制
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的类

我得到了相同的错误:

以下方法确实起作用:

代码语言:javascript
复制
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 );
}

完整的源代码在下面。问题区域/

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2013-12-31 13:32:43

在lambda中使用捕获来接受要排序的列号。

代码语言:javascript
复制
int columnToSort = 2;
sort(vec2d.begin(), vec2d.end(), [columnToSort](const vector< int >& a, const vector< int >& b)
{
    return a[columnToSort] < b[columnToSort];
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20852114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档