0

I found this example on another site:

bool fncomp (Node lhs, Node rhs) {return lhs.val < rhs.val;}
bool(*fn_pt)(Node,Node) = fncomp;
std::set < Node, bool(*)(const Node &, const Node&) >  example(ft_pt);

but what I really want is a typedef so I can write

SetOfNodes example;

without having to repeat the long std::set instantiation and without having to repeat what compare function to use.

I'm getting the impression from researching this and other venues that I might have to use "using" instead of "typedef", but still can't get my head around how to instantiate and/or specialize either way to the specific compare function I have.

1 Answers1

0

You can make a comparator class, which could be default-constructed and then used as the comparison function object. e.g.

struct node_comparator {
    bool operator() (Node lhs, Node rhs) const {return lhs.val < rhs.val;}
};

then typedef the type as

typedef std::set<Node, node_comparator> SetOfNodes;
// or 
using SetOfNodes = std::set<Node, node_comparator>;

then use it as

SetOfNodes example;
songyuanyao
  • 169,198
  • 16
  • 310
  • 405