I'm trying to declare a priority_queue of nodes, using bool Compare(Node a, Node b) as the comparator function (which is outside the node class).
What I currently have is:
priority_queue<Node, vector<Node>, Compare> openSet;
For some reason, I'm getting Error: "Compare" is not a type name
Changing the declaration to priority_queue <Node, vector<Node>, bool Compare>
gives me Error: expected a '>'
I've also tried:
priority_queue<Node, vector<Node>, Compare()> openSet;
priority_queue<Node, vector<Node>, bool Compare()> openSet;
priority_queue<Node, vector<Node>, Compare<Node, Node>> openSet;
How should I correctly declare my priority_queue?
Note - You may also want to check other answers, especially the one with decltype and lambda
You should declare a class Compare and overload operator() for it like this:
class Foo
{
};
class Compare
{
public:
bool operator() (Foo, Foo)
{
return true;
}
};
int main()
{
std::priority_queue<Foo, std::vector<Foo>, Compare> pq;
return 0;
}
Or, if you for some reasons can't make it as class, you could use std::function for it:
class Foo
{
};
bool Compare(Foo, Foo)
{
return true;
}
int main()
{
std::priority_queue<Foo, std::vector<Foo>, std::function<bool(Foo, Foo)>> pq(Compare);
return 0;
}
The accepted answer shows how to use a class or a std::function as comparator. We can also pass a function pointer, as cute_ptr's answer already showed. However, the syntax to do so is much simpler than shown there:
class Node;
bool Compare(Node a, Node b);
std::priority_queue<Node, std::vector<Node>, decltype(&Compare)> openSet(Compare);
That is, there is no need to explicitly encode the function's type, you can let the compiler do that for you using decltype.
This is very useful if the comparator is a lambda. You cannot specify the type of a lambda in any other way than using decltype. For example:
auto compare = [](Node a, Node b) { return a.foo < b.foo; }
std::priority_queue<Node, std::vector<Node>, decltype(compare)> openSet(compare);
The third template parameter must be a class who has operator()(Node,Node) overloaded.
So you will have to create a class this way:
class ComparisonClass {
public:
bool operator() (Node, Node) {
//comparison code here
}
};
And then you will use this class as the third template parameter like this:
priority_queue<Node, vector<Node>, ComparisonClass> q;
Answering your question directly:
I'm trying to declare a priority_queue of nodes, using bool Compare(Node a, Node b) as the comparator function
What I currently have is:
priority_queue<Node, vector<Node>, Compare> openSet;
For some reason, I'm getting Error:
"Compare" is not a type name
The compiler is telling you exactly what's wrong: Compare is not a type name, but an instance of a function that takes two Nodes and returns a bool.
What you need is to specify the function pointer type:
std::priority_queue<Node, std::vector<Node>, bool (*)(Node, Node)> openSet(Compare)
You have to define the compare first. There are 3 ways to do that:
use class
use struct (which is same as class)
use lambda function.
It's easy to use class/struct because easy to declare just write this line of code above your executing code
struct compare{
public:
bool operator()(Node& a,Node& b) // overloading both operators
{
return a.w < b.w: // if you want increasing order;(i.e increasing for minPQ)
return a.w > b.w // if you want reverse of default order;(i.e decreasing for minPQ)
}
};
Calling code:
priority_queue<Node,vector<Node>,compare> pq;
One can also use a lambda function.
auto Compare = [](Node &a, Node &b) { //compare };
std::priority_queue<Node, std::vector<Node>, decltype(Compare)> openset(Compare);
In case this helps anyone :
static bool myFunction(Node& p1, Node& p2) {}
priority_queue <Node, vector<Node>, function<bool(Node&, Node&)>> pq1(myFunction);
In the priority queue, there is a predefined boolean function "operator<()", try to overload this function as per your requirement.
bool operator<(const Node& x,const Node& y){
return x.data>y.data;
}
priority_queue<Node> min_heap;
With latest c++ standard, you can actually declare a lambda function for comparator which would make the code much cleaner. Here is a sample code:
#include <queue>
class Foo
{
public:
int i;
};
int main()
{
auto comparator = [](const Foo& a, const Foo& b) {
return a.i > b.i;
};
std::priority_queue<Foo, std::vector<Foo>, decltype(comparator)> pq(comparator);
return 0;
}
With the help of struct also we can do this. The code will go something like below.
struct myCompare{
bool operator()(Node &a, Node &b){
// Your own custom logic to compare the two nodes and return a boolean value.
}
}
priority_queue<Node, vector<Node>, myCompare> openSet;
prefer struct, and it's what std::greater do
struct Compare {
bool operator()(Node const&, Node &) {}
}
Related
I wonder how is the most convenient way to have a sorted set, a set of pointers to objects, like
std::set<myClass*> mySet;
I want this set to be sorted by myClass::someProperty (say, an int).
Should I overload operator < in myClass? I'm not sure if it will work, because it's not a set of myClass, but a set of pointers.
How can I define a compare function?
Thank you very much.
You need to define a type (or a function) that dereferences the pointers and compares the attributes of the objects they point at, something on this general order:
class myClass {
int value;
public:
myClass(int i = 0) : value(i) {}
struct cmp {
bool operator()(myClass *const &a, myClass *const &b) const {
return a->value < b->value;
}
};
};
We they define the set something like this:
std::set<myClass*, myClass::cmp> mySet;
My advice, however, would be to store objects instead of pointers (if possible).
You can also specialize std::less for your myClass* as given below and then no need to pass comparator while creating set:
namespace std {
template<>
struct less<myClass*>
{
bool operator()(const myClass* k1, const myClass* k2) const
{
// Some code ...
}
};
}
This is my first time using a priority queue. I'm trying to implement Dijkstra's algorithm for school and I figured I need a min heap to do this. Right now my nodes are pointers and I want to compare their weight, but I don't think I can overload > and < with pointers? Is there a way I could accomplish this?
Code this far:
priority_queue<Node*, vector<Node*>, node_comparison> minHeap;
And then I have a struct to compare the node's weights
struct node_comparison
{
bool operator<( const Node* a, const Node* b ) const
{
return a->totalWeight < b->totalWeight;
}
};
However it says there are too many parameters for this operator function. I've been trying to figure out how I could manage a min heap priority queue with my nodes for a while now and keep getting stuck. Any ideas?
If I understand your question correctly, I believe what you actually want is to make node_comparison a functor (more specifically, a binary predicate):
struct node_comparison
{
bool operator () ( const Node* a, const Node* b ) const
{
return a->totalWeight < b->totalWeight;
}
};
A functor is a class whose objects provide an overload of the call operator (operator ()) and, therefore, can be invoked with the same syntax you would use for invoking a function:
Node* p1 = ...;
Node* p2 = ...;
node_comparison comp;
bool res = comp(p1, p2) // <== Invokes your overload of operator ()
Internally, std::priority_queue will instantiate your predicate more or less like I did in the code snippet above, and invoke it that way to perform comparisons between its elements.
The advantage of functors over regular functions is that they could hold state information (something you probably won't need for the moment, but which often turns out to be desirable):
#include <cmath>
struct my_comparator
{
my_comparator(int x) : _x(x) { }
bool operator () (int n, int m) const
{
return abs(n - _x) < abs(m - _x);
}
int _x;
};
The above predicate, for instance, compares integers based on how distant they are from another integer provided at construction time. This is how it could be used:
#include <queue>
#include <iostream>
void foo(int pivot)
{
my_comparator mc(pivot);
std::priority_queue<int, std::deque<int>, my_comparator> pq(mc);
pq.push(9);
pq.push(2);
pq.push(17);
while (!pq.empty())
{
std::cout << pq.top();
pq.pop();
}
}
int main()
{
foo(7);
std::cout << std::endl;
foo(10);
}
You would need your comparison functor to implement bool operator()(....), not bool operator<(....):
struct node_comparison
{
bool operator()( const Node* a, const Node* b ) const
{
return a->totalWeight < b->totalWeight;
}
};
My priority queue declared as:
std::priority_queue<*MyClass> queue;
class MyClass {
bool operator<( const MyClass* m ) const;
}
is not sorting the items in the queue.
What is wrong? I would not like to implement a different (Compare) class.
Answer summary:
The problem is, the pointer addresses are sorted. The only way to avoid this is a class that 'compares the pointers'.
Now implemented as:
std::priority_queue<*MyClass, vector<*MyClass>, MyClass::CompStr > queue;
class MyClass {
struct CompStr {
bool operator()(MyClass* m1, MyClass* m2);
}
}
Give the que the Compare functor ptr_less.
If you want the ptr_less to be compatible with the rest of the std library (binders, composers, ... ):
template<class T>
struct ptr_less
: public binary_function<T, T, bool> {
bool operator()(const T& left, const T& right) const{
return ((*left) <( *right));
}
};
std::priority_queue<MyClass*, vector<MyClass*>, ptr_less<MyClass*> > que;
Otherwise you can get away with the simplified version:
struct ptr_less {
template<class T>
bool operator()(const T& left, const T& right) const {
return ((*left) <( *right));
}
};
std::priority_queue<MyClass*, vector<MyClass*>, ptr_less > que;
The operator <() you have provided will compare a MyClass object with a pointer to a MyClass object. But your queue contains only pointers (I think). You need a comparison function that takes two pointers as parameters.
All this is based on some suppositions - please post your actual code, using copy and paste.
Since your priority_queue contains only pointer values, it will use the default comparison operator for the pointers - this will sort them by address which is obviously not what you want. If you change the priority_queue to store the class instances by value, it will use the operator you defined. Or, you will have to provide a comparison function.
Not sure about the priority queue stuff because I've never used it but to do a straight sort, you can do this:
class A
{
friend struct ComparePtrToA;
public:
A( int v=0 ):a(v){}
private:
int a;
};
struct ComparePtrToA
{
bool operator()(A* a1, A* a2) {return a1->a < a2->a;}
};
#include <vector>
#include <algorithm>
int _tmain(int argc, _TCHAR* argv[])
{
vector<A*> someAs;
someAs.push_back(new A(1));
someAs.push_back(new A(3));
someAs.push_back(new A(2));
sort( someAs.begin(), someAs.end(), ComparePtrToA() );
}
Note the memory leaks, this is only an example...
Further note: This is not intended to be an implementation of priority queue! The vector is simply an example of using the functor I created to compare two objects via their pointers. Although I'm aware of what a priority queue is and roughly how it works, I have never used the STL features that implement them.
Update: I think TimW makes some valid points. I don't know why he was downvoted so much. I think my answer can be improved as follows:
class A
{
public:
A( int v=0 ):a(v){}
bool operator<( const A& rhs ) { return a < rhs.a; }
private:
int a;
};
struct ComparePtrToA
{
bool operator()(A* a1, A* a2) {return *a1 < *a2;}
};
which is cleaner (especially if you consider having a container of values rather than pointers - no further work would be necessary).
I want to find the first item in a sorted vector that has a field less than some value x.
I need to supply a compare function that compares 'x' with the internal value in MyClass but I can't work out the function declaration.
Can't I simply overload '<' but how do I do this when the args are '&MyClass' and 'float' ?
float x;
std::vector< MyClass >::iterator last = std::upper_bound(myClass.begin(),myClass.end(),x);
What function did you pass to the sort algorithm? You should be able to use the same one for upper_bound and lower_bound.
The easiest way to make the comparison work is to create a dummy object with the key field set to your search value. Then the comparison will always be between like objects.
Edit: If for some reason you can't obtain a dummy object with the proper comparison value, then you can create a comparison functor. The functor can provide three overloads for operator() :
struct MyClassLessThan
{
bool operator() (const MyClass & left, const MyClass & right)
{
return left.key < right.key;
}
bool operator() (const MyClass & left, float right)
{
return left.key < right;
}
bool operator() (float left, const MyClass & right)
{
return left < right.key;
}
};
As you can see, that's the long way to go about it.
You can further improve Mark's solution by creating a static instance of MyClassLessThan in MyClass
class CMyClass
{
static struct _CompareFloatField
{
bool operator() (const MyClass & left, float right) //...
// ...
} CompareFloatField;
};
This way you can call lower_bound in the following way:
std::lower_bound(coll.begin(), coll.end(), target, CMyClass::CompareFloatField);
This makes it a bit more readable
Pass a lambda function to upper_bound
float x;
MyClass target;
target.x_ = x;
std::vector< MyClass >::iterator last =
std::upper_bound(myClass.begin(),myClass.end(),target,
[](const MyClass& a, const MyClass& b){return a.x_ < b.x_;});
I think what you need is std::bind2nd(std::less<MyClass>(), x). But, of course, the operator< must be defined for MyClass.
Edit: oh and I think you will need a constructor for MyClass that accepts only a float so that it can be implicitly converted. However, there might be a better way to do this.
i have a priority queue and i have defined like this:
priority_queue<Node*,vector<Node*>,greater<Node*>> myQueue;
i have to add to queue on the basis of a parameter param and i have overloaded it like this
bool Node::operator>(const Node& right) const
{
return param>right.param;
}
since the overload function doesnt take a pointer object, how should i change it so that my overloaded function is called.
i am adding to queue this way:
Node *myNode
myQueue.add(myNode);
i cant pass the myNode without making as pointer object.
please guide ..
#Sellibitze
i have done something like this
template<typename Node, typename Cmp = std::greater<Node> >
struct deref_compare : std::binary_function<Node*,Node*,bool>
{
deref_compare(Cmp const& cmp = Cmp())
: cmp(cmp) {}
bool operator()(Node* a, Node* b) const {
return cmp(*a,*b);
}
private:
Cmp cmp;
};
typedef deref_compare<Node,std::greater<Node> > my_comparator_t;
priority_queue<Node*,vector<Node*>,my_comparator_t> open;
i am filled with errors.
You need to write your own functor for the comparison because you can't overload operator> for pointers. So, instead of greater you would be using your own dedicated class with the appropriate function call operator. This could be even done generically.
template<typename T, typename Cmp = std::less<T> >
struct deref_compare : std::binary_function<T const*,T const*,bool>
{
deref_compare(Cmp const& cmp = Cmp())
: cmp(cmp) {}
bool operator()(T const* a, T const* b) const {
return cmp(*a,*b);
}
private:
Cmp cmp;
};
typedef deref_compare<Node,std::greater<Node> > my_comparator_t;
Edit1: I just realized you could do it even more generically, with iterators instead of pointers. ;-)
Edit2: If you're not comfortable with the template and don't need this generalization you could just as well use
struct my_node_ptr_compare
{
bool operator()(Node const* a, Node const* b) const {
return *a > *b;
}
};
priority_queue<Node*,vector<Node*>,my_node_ptr_compare> foo;
Set up the operator>() as a friend-of-Nodes function taking two Nodes.
Use the friend keyword.
Some refs:
http://www.cprogramming.com/tutorial/friends.html
http://msdn.microsoft.com/en-us/library/465sdshe(VS.80).aspx
Edit: This won't work in the pointer case, but will work in the regular Nodes case.
The simplest way would be to implement a compare function that takes Node pointers as its parameters, like this:
bool nodePtrGreater(Node const *a, Node const *b) {
return *a > *b;
}
This function should use the operator> of your Node class properly.
[edit] The old version didn't define the queue correctly.
Create your priority queue like this:
priority_queue<Node*,vector<Node*>, bool(*)(Node const *, Node const *)> myQueue(nodePtrGreater);