Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://www.velocityreviews.com/forums/t638232-priority_queue-predicate.html" rel="nofollow">Derived from here</a>.</p> <p>priority_queue accepts a function pointer for the compare argument, but you'd have to pass the function pointer to the constructor, not the template. The template would just accept the type of the function pointer.</p> <p>The easiest way to implement this would be to use a functor (function object, predicate class or however you name it).</p> <p>From that functor, you could also call the function pointer, provided it is known globally at compile time. But I assume it isn't so you might have to go the 'pass function pointer in constructor' route after all.</p> <p>If the function address is known at compile time:</p> <pre><code>typedef bool (*MyFuncPtr)(const unsigned&amp;, const unsigned&amp;); MyFuncPtr globalPtr = myCompareFunction; struct MyCompare { bool operator()(const unsigned &amp;a, const unsigned &amp;b) const { return globalPtr(a, b); } }; std::vector&lt;unsigned&gt; Traverse ( unsigned start_node, std::priority_queue &lt; std::pair&lt; unsigned, unsigned &gt;, std::vector&lt; std::pair&lt;unsigned, unsigned&gt; &gt;, MyCompare &gt; &amp;pQueue ) const; </code></pre> <p>If it isn't:</p> <pre><code>typedef bool (*MyFuncPtr)(const unsigned&amp;, const unsigned&amp;); typedef std::priority_queue &lt; std::pair&lt; unsigned, unsigned &gt;, std::vector&lt; std::pair&lt;unsigned, unsigned&gt; &gt;, MyFuncPtr &gt; MyQueue; std::vector&lt;unsigned&gt; Traverse ( unsigned start_node, MyQueue &amp;pQueue ) const; int main() { MyQueue queue(myCompareFunction); Traverse(..., queue); } </code></pre> <p>In short, the template is pretty much useless here, because a function is not a type, and the type of the compare function is 100% predetermined (it's MyQueue). The compiler wouldn't have any information to infer a specific type from the arguments passed to Traverse().</p> <p>If you don't actually need to change the comparison function dynamically during runtime, the other answers here are the way to go. Just pass T as the third template argument to your priority_queue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload