如何为已显式定义了比较函数的集合定义迭代器
How to define iterator for a set whose compare function I have defined explicitly?
Set I have defined:
set < Node*, bool (*)(Node *, Node *) > visited(Node::compare);
比较函数定义:
struct Node {
int config[8][8];
static int n;
Node *parent;
static bool compare(Node *, Node *);
};
int Node::n = 0;
bool Node::compare(Node *a, Node *b) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if( a->config[i][j] != b->config[i][j]) {
return true;
}
}
}
return false;
}
现在,在代码的某个点,我想遍历它。但是下面的代码不能工作。
for(set <Node*, bool (*)(Node*, Node*)>::iterator itt = visited.begin(); itt != visited.end(); itt++) {
delete (*it);
}
错误是:
error: no match for 'operator<'
(operand types are
'std::set<Node*, bool (*)(Node*, Node*)>::iterator {aka std::_Rb_tree_const_iterator<Node*>}'
and
'std::set<Node*, bool (*)(Node*, Node*)>::iterator {aka std::_Rb_tree_const_iterator<Node*>}'
)
你的模板声明中有一堆错误的括号:
set<Node*, bool(*)(Node*, Node*)())>::iterator
^^^
去掉这些,错误就会消失。
除此之外,我强烈建议使用auto
:
for (auto it = visited.begin(); it != visited.end(); ++it) { ...
或者更好,如果可能的话,使用基于范围的for循环:
for (const auto& node : visited) { ...
相关文章:
- 在命名空间中定义函数还是限定函数
- 为什么在定义函数之前先声明它
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- 这个c++代码是如何在没有定义函数的情况下运行的
- 具有外部"c"和程序集的未定义函数
- 已定义函数时出现 G++ "未定义的引用"错误
- 将自定义函数传递到基抽象类中以延迟执行
- C++使用 rand 定义函数语法
- Arduino:在 loop() 和自定义函数中运行相同的代码时出现问题
- 将具有固定签名的自定义函数名称注入 CRTP
- 使用定义函数模板别名
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 在内联程序集中定义函数和从 C++ 调用时出现问题
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- 声明和定义函数静态会产生"undefined reference to function_name()"
- 朋友定义函数的名称空间是什么
- 介子 对用户定义函数的未定义引用
- 如何使用 "using" 关键字定义函数原型/签名
- 在 C 结构中定义C++函数