c++ - How to instantiate an comparison function (functor) when defining a map/set? -


i'm using funciton object specify comparison function map/set:

struct compareobject {     bool operator()(node* const n1, node* const n2) const; };  

as far understand defining set not create instance of compareobject , pretend static:

std::multiset<node*, compareobject> set; 

but in problem need pass instance of tree i'm using in actual comparision function:

bool compareobject::operator()(node* const n1, node* const n2) const {   if (tree->getnoofgood(n1) > tree->getnoofgood(n2)) return false;   if (tree->getnoofgood(n2) > tree->getnoofgood(n1)) return true;   return false; } 

so, i'm adding fields compareobject definition:

struct compareobject {    tree& tree;              // added   compareobject(tree& t);  // added    bool operator()(node* const n1, node* const n2) const; };  

the issue i'm having don't know how instatiate object definition of set.

the first thing comes mind is:

std::multiset<node*, compareobjects(*this)> shapesmap; // not valid code 

but not suprisingly gives me error: ‘this’ cannot appear in constant-expression

do have ideas how go around problem?

you can pass in instance of functor parameter set constructor. multiset<node*, compareobject> shapesset(compareobject(mytree));


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -