将参数传递给比较函数?
当对向量使用 STL 排序算法时,我想传入我自己的比较函数,该函数也带有一个参数。
例如,理想情况下我想做一个本地函数声明,例如:
int main() {
vector<int> v(100);
// initialize v with some random values
int paramA = 4;
bool comp(int i, int j) {
// logic uses paramA in some way...
}
sort(v.begin(), v.end(), comp);
}
但是,编译器对此有所抱怨。当我尝试类似的操作时:
int main() {
vector<int> v(100);
// initialize v with some random values
int paramA = 4;
struct Local {
static bool Compare(int i, int j) {
// logic uses paramA in some way...
}
};
sort(v.begin(), v.end(), Local::Compare);
}
编译器仍然抱怨:“错误:使用包含函数的参数”
我该怎么办?我应该使用全局比较函数创建一些全局变量吗?
谢谢。
When using the STL sort algorithm on a vector, I want to pass in my own comparison function which also takes a parameter.
For example, ideally I want to do a local function declaration like:
int main() {
vector<int> v(100);
// initialize v with some random values
int paramA = 4;
bool comp(int i, int j) {
// logic uses paramA in some way...
}
sort(v.begin(), v.end(), comp);
}
However, the compiler complains about that. When I try something like:
int main() {
vector<int> v(100);
// initialize v with some random values
int paramA = 4;
struct Local {
static bool Compare(int i, int j) {
// logic uses paramA in some way...
}
};
sort(v.begin(), v.end(), Local::Compare);
}
The compiler still complains: "error: use of parameter from containing function"
What should I do? Should I make some global variables with a global comparison function..?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 C++ 中,不能在另一个函数中定义自由函数。所以你的第一个代码片段格式不正确。
第三个参数必须是函数对象。在类中重载
()
运算符,然后创建函数对象。在 C++0x 中,您可以使用 lambda 表达式。
In C++ you cannot define a free function inside another function. So your first code snippet is ill formed.
The 3rd argument must be a function object. Overload
()
operator inside the class and then create the function object.In C++0x you can use lambda expressions.
一种可能性是在构造比较器对象时传递参数:
One possibility is to pass the parameter when you construct your comparator object:
//使用std::bind
//示例
//Using std::bind
//Example
您无法从本地定义的函数中访问函数的本地变量 - 当前形式的 C++ 不允许 关闭。该语言的下一个版本 C++0x 将支持这一点,但该语言标准尚未最终确定,目前对当前草案标准的支持很少。
要实现此功能,您应该将
std::sort
的第三个参数更改为对象实例而不是函数。std::sort
的第三个参数可以是任何可调用的参数(即任何x
,其中添加像x(y, z)
这样的括号使得句法意义)。最好的方法是定义一个实现operator()
函数的结构,然后传递该对象的实例:请注意,我们必须将
paramA
存储在结构体,因为我们无法从operator()
内部访问它。You cannot access the local variables of a function from within a locally defined function -- C++ in its current form does not allow closures. The next version of the language, C++0x, will support this, but the language standard has not been finalized and there is little support for the current draft standard at the moment.
To make this work, you should change the third parameter of
std::sort
to be an object instance instead of a function. The third parameter ofstd::sort
can be anything that is callable (i.e. anyx
where adding parentheses likex(y, z)
makes syntactic sense). The best way to do this is to define a struct that implements theoperator()
function, and then pass an instance of that object:Note that we have to store
paramA
in the structure, since we can't access it otherwise from withinoperator()
.