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);
上面的代码给了我一些奇怪的错误,这在其他一些行中也有(放在代码的后面),但是当我将 comp2 函数更改为 following 时,所有错误都消失了。
bool comp2(node* a,node* b){
assert(a && b && "comp2 - null argument");
if(a->value < b->value){ return true; }
return false;
}
有什么理由吗?
错误:/usr/include/c++/4.4/bits/STL_algo.h|124|错误:从类型为“node* const”的表达式中对类型为“node&”的引用的初始化无效|
如果这个(波纹管)有效,那么上面也应该有效
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;
}
最佳答案
您已经定义了 node *
的 std::vector
。因此,所有的元素都是node *
, vector 执行的所有操作都会在node *
上进行。您不能为 sort
提供不同类型的比较函数。
关于c++ - STL::sort() 中的比较函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4701256/