shubham vashishtha shubham vashishtha - 1 month ago 6x
C++ Question

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);

It's compare function definition:

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;

Now, at some point of the code I want iterate through it. However following code isn't working.

for(set <Node*, bool (*)(Node*, Node*)>::iterator itt = visited.begin(); itt != visited.end(); itt++) {
delete (*it);

Error is :
error: no match for 'operator<' (operand types are 'std::set::iterator {aka std::_Rb_tree_const_iterator}' and 'std::set::iterator {aka std::_Rb_tree_const_iterator}')|


You have a bunch of erroneous parentheses in your template declaration:

set<Node*, bool(*)(Node*, Node*)())>::iterator

Remove those and the error will go away.

Aside from that, I would strongly recommend using auto instead:

for (auto it = visited.begin(); it != visited.end(); ++it) { ...

Or even better, if possible, use range-based for loops:

for (const auto& node : visited) { ...