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

  • Home
  • SEARCH
  • 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 8973741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:31:07+00:00 2026-06-15T18:31:07+00:00

I am writing a program in C++ and I would like to define priority

  • 0

I am writing a program in C++ and I would like to define priority queue of one of my class. I need it to compare objects by one of the class member variable. I have used an operator< overload, but I know there is a second way to achieve this goal – special function defined with queue definition. Which way is better, more esthetic and more efficient? And how to write such a function?

I have done it as follows:

#include <iostream>
#include <queue>
using namespace std;

class Human {

    public:
        string name;
        int age;
        Human(string name, int age);
};

Human::Human(string name, int age) : name(name), age(age) {}

bool operator<(Human a, Human b) {return a.age < b.age ? true : false;}

int main() {

    Human p1("Child",5);
    Human p2("Grandfather",70);
    Human p3("Older son",20);
    Human p4("Father",40);
    Human p5("Younger son",10); 

    priority_queue<Human> Q;

    Q.push(p1);
    Q.push(p2);
    Q.push(p3);
    Q.push(p4);
    Q.push(p5);

    while(!Q.empty()) {

        cout << "Name: " << Q.top().name << ", age: " << Q.top().age << endl;
        Q.pop();
    }
    return 0;
}
  • 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-15T18:31:08+00:00Added an answer on June 15, 2026 at 6:31 pm

    Style

    If your type has an intrinsic, “natural” ordering, it is reasonable to express that with a built-in operator.

    If the ordering varies by how you’re using the type (eg. you have one collection of Human sorted by age, one by height, one by IQ), then it seems sensible to say the ordering is a property of the container instead of the type.


    Implementation

    You can write a free function, or use a functor if you need state.

    Note that these are both exactly as amenable to inlining as a built-in operator, so there is no inherent difference in speed (it’s probably harder for the compiler to prove a function pointer is inline-able though, so functors are generally preferred).

    struct OrderByAge
    {
        bool operator() (Human const &a, Human const &b) { return a.age < b.age; }
    };
    typedef std::priority_queue<Human, std::vector<Human>, OrderByAge> age_queue;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a blackjack program and would like to be able to write the
I'm currently writing a program in Python, and I would like to determine the
For the python program I am writing I would like to give the opportunity
I'm writing a physics simulating program, and found after() useful. I once would like
I am currently trying to make a Writing program. I would like the functionality
I'm writing a program & I want to have 2 SQL Server databases. One
I am writing a program in Java, in which I define a class class
I am writing a flashcard program and would like to see the question on
I am thinking of writing a desktop HTA program. However, at startup I would
Im writing a program that should read input via stdin, so I have the

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.