Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8866149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:42:51+00:00 2026-06-14T16:42:51+00:00

I am implementing Dijkstra’s algorithm and I would like to use STL’s ‘priority_queue’ to

  • 0

I am implementing Dijkstra’s algorithm and I would like to use STL’s ‘priority_queue’ to speed up the coding process, but as so often is the case with my attempts at coding in C++, my lack of understanding of the language is slowing me down. I found this example at http://www.cplusplus.com/reference/stl/priority_queue/priority_queue/, but do not understand what the following is doing:

// constructing priority queues
#include <iostream>
#include <queue>
using namespace std;

class mycomparison
{
  bool reverse;
public:
  mycomparison(const bool& revparam=false)
    {reverse=revparam;}
  bool operator() (const int& lhs, const int&rhs) const
  {
    if (reverse) return (lhs>rhs);
    else return (lhs<rhs);
  }
};

int main ()
{
  int myints[]= {10,60,50,20};

  priority_queue<int> first;
  priority_queue<int> second (myints,myints+4);
  priority_queue< int, vector<int>, greater<int> > third (myints,myints+4);

  // using mycomparison:
  priority_queue< int, vector<int>, mycomparison > fourth;

  typedef priority_queue<int,vector<int>,mycomparison> mypq_type;
  mypq_type fifth (mycomparison());
  mypq_type sixth (mycomparison(true));

  return 0;
}

To be more specific, ‘bool operator() (const int& lhs, const int&rhs) const’ is what is tripping me up. The rest I am fine with. I think its overloading an operator, but ‘operator()’ makes no sense to me. Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:42:53+00:00Added an answer on June 14, 2026 at 4:42 pm

    Essentially, operator() is “just” another operator that can be overloaded, so everything you know about operator overloading still applies – it’s useful because it can be applied to an object with the same syntax that you might use to call a function, e.g.

    MyClass f;
    f(); // i.e. f.operator()();
    

    That said, there’s a lot that could be said about functors etc. beyond that – you might want to do a Google search for functors / function objects for more information. Here’s one link:

    http://en.wikipedia.org/wiki/Function_object

    In the above code, priority_queue takes a comparison functor that it uses to order the things you put in the queue. The first queue (fifth) uses normal ordering; the second queue (sixth) uses reverse ordering.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

implementing service something similar with tinyurl or bit.ly, I'm would like to expose service
When implementing move constructors and move assignment operators, one often writes code like this:
This is my first time implementing Dijkstra's algorithm. Okay so here I have a
I have a project where I'm implementing Dijkstra's shortest path algorithm using C++ classes.
I'm implementing the Dijkstra search algorithm in Python. At the end of the search,
I have been fighting all day in understanding Dijkstra's algorithm and implementing with no
when implementing a recursive function during development, i will use a counter and exit
Implementing a custom VM and I've come to use registers (these will store pointers
Implementing a low pass FIR filter, when should one use FFT and IFFT instead
Implementing the basic algorithm using last array as a pivot in Java, is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.