1

I am sure I am making some really silly mistake but haven't been able to point it out. The make_heap function in the algorithm STL of C++ has the syntax

void make_heap(v.begin(), v.end(),Compare comp ); where v is declared as an int vector. Now the comp input defaults to < but I want to change it so that I can use it to make both min and max heaps. Looking at other examples what makes sense to me is doing something like,

void make_heap(v.begin(), v.end(),std::less<int> );

OR

void make_heap(v.begin(), v.end(),std::greater<int> );

but I keep on getting the compiler error expected primary expression before ')' token

I can't figure out what am I doing wrong? Any help?

2
  • 1
    You're passing a type for the third parameter. It needs to be an object instance of a type fulfilling the comparator requirements. Put some () on the end of that thing. And the void make no sense at all. Commented Apr 8, 2017 at 3:12
  • You are missing a pair of parentheses after std::less<int>() Commented Apr 8, 2017 at 3:12

1 Answer 1

1

Put () at the end

void make_heap(v.begin(), v.end(), std::greater<int>()); 

Reason

priority_queue signature -

template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue; 

So, it takes a Compare class (not Compare class object) as third argument. So priority_queue<int, vector<int>, greater<int>()> Q; will yield compilation error because you need to pass a Compare class (a binary predicate). Correct syntax is -

priority_queue<int, vector<int>, greater<int>> Q; 

make_heap signature is -

template <class RandomAccessIterator, class Compare> void make_heap (RandomAccessIterator first, RandomAccessIterator last, Compare comp ); 

which take Compare class object. So make_heap(v.begin(), v.end(), std::greater<int>()); will give compilation error. You need to a Compare class object (a binary function) here.

Hope it helps!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.