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 8664461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:14:43+00:00 2026-06-12T17:14:43+00:00

I have a class, and the pointers to the objects of this class need

  • 0

I have a class, and the pointers to the objects of this class need to be placed in a std::set. I want to define the comparator inside the class. I have seen a few solutions where either a separate class is defined (I guess it is called a functor), or a structure is defined which overloads the operator(). I want to avoid this boilerplate code, and want to define a comparator as a member of the class itself, something along the lines of Java’s compareTo() method.

Let us say, my class is something like:

class Item {
private:
    int id;
    float score;
.....
public:
// Rest of the methods and setters/getters
}

I want to define the comparator in a way that pointer to object having a higher score are placed first in the set. If the score is equal for the two, then the one with the lower id is placed first. I guess the code will be something like the following, but since I did not understand this part very well, please correct me (I would like this to be placed inside the class itself):

bool operator()(const Item* a, const Item* b) {
    if (a->score != b->score) return a->score > b->score;
    return a->id < b->id;
}

The usage would be as follows:

std::set<Item*> sortedItems;
Item* item = new Item();
sortedItems.insert(item);

I am not sure if the comparator needs to be specified at all in the std::set template if defined within the class, and if so, how? Also, how do I add this comparator in the class itself? I am new to STL, and fairly new to C++ as well. Thanks!

  • 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-12T17:14:45+00:00Added an answer on June 12, 2026 at 5:14 pm

    this solution is inspired by this answer.

    #include <set>
    
    class Item {
    private:
        int id;
        float score;
    public:
        struct compare {
            bool operator()(const Item* a, const Item* b) {
                 if (a->score != b->score) return a->score > b->score;
                 return a->id < b->id;
            }
        };
    };
    

    Because set allows you to define your own comparison method you can use it as follows.

    std::set<Item*, Item::compare> sortedItems;
    

    This should allow your class Item to work with set

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

Sidebar

Related Questions

I have a class A containing two pointers to objects of another class B.
I have a class with a vector of pointers to objects. I've introduced some
I have the template class and array of pointers to objects and overloaded logic
I have a vector of pointers to class objects. These class objects invoke new
suppose I have a class with many explicit (statically allocated) members and few pointers
I have a class that has a vector of objects. What do I need
I have some vectors of class A objects: std::vector<A> *V1; std::vector<A> *V2; etc there
Let's say I have objects which look very roughly like this: class object {
I have this code to represent bank: class Bank { friend class InvestmentMethod; std::vector<BaseBankAccount*>
I have a list of pointers to objects. What I want to do is

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.