stl :: sort()中的比较函数

0 c++ stl

class node{
public:
  unsigned long long int value;
  int index;
};


bool comp2(node& a,node& b){
  if(a.value < b.value) { return true; }
  return false;
}

vector <node*>llist,rlist;
sort(llist.begin(),llist.end(),comp2);
Run Code Online (Sandbox Code Playgroud)

上面的代码给了我一些在某些其他行(后面的代码中)的错误,但是当我改变comp2函数跟随所有错误消失时.

bool comp2(node* a,node* b){
  assert(a && b && "comp2 - null argument");
  if(a->value < b->value){ return true; }
  return false;
}
Run Code Online (Sandbox Code Playgroud)

这有什么理由吗?

错误:/usr/include/c++/4.4/bits/stl_algo.h|124|error: invalid initialization of reference of type ‘node&’ from expression of type ‘node* const’|

如果这个(波纹管)工作,那么上面也应该工作

using namespace std;

void rep(int& a,int& b){

int c;
c=a;
a=b;
b=c;

}

int main(void){

int a=3,b=4;

rep(a,b);
cout<<a<<" "<<b;
return 0;
}
Run Code Online (Sandbox Code Playgroud)

Oli*_*rth 5

您已经定义了std::vectornode *.因此,所有元素都是node *,并且矢量执行的所有操作都将打开node *.您不能给出sort不同类型的比较功能.