【发布时间】:2019-09-22 14:24:15
【问题描述】:
我在一个类函数中工作:
void Planner::CellSort()
{
vector<vector<int>> *pointer_to_open = &openlist;
sort(pointer_to_open->begin(), pointer_to_open->end(), Compare);
}
当我尝试编译它时,我收到以下错误:
error: must use '.*' or '->*' to call pointer-to-member function
但是当我尝试下面的编译就好了:
sort((&openlist)->begin(), (&openlist)->end(), Compare);
两者实际上不是在做同样的事情吗?或者这两种方法有什么区别。
这是我的比较功能:
bool Planner::Compare(const vector<int> a, const vector<int> b)
{
int f1 = a[2] + a[3]; // f1 = g1 + h1
int f2 = b[2] + b[3]; // f2 = g2 + h2
return f1 > f2;
}
谢谢。
【问题讨论】:
-
可以显示比较功能吗?如果比较函数的参数类似于 const std::vector
& a, const std::vector & b,它应该可以工作。 -
ideone.com/Rw93A8 演示如何使用 sort(pointer_to_open->begin(), pointer_to_open->end(), Compare);
-
另外,不明白为什么这个问题标记为重复并带有指向潜在答案的链接。 0.0'。手头的问题似乎与函数 () 是否具有比绑定运算符更高的优先级...
-
@Yucel_K 我添加了比较功能,我检查了 URL 的代码,似乎我的仍然无法弄清楚为什么它无法编译。
-
当您调用比较函数时,向量作为副本传递。相反,它们可以通过引用传递。这样你就不会在每次调用比较函数时都复制向量。所以你获得了速度优势。您所要做的就是更改比较功能。因此; 比较(const vector
& a, const vector 它应该可以工作& b)
标签: c++