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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:04:39+00:00 2026-06-12T07:04:39+00:00

I have a question on how the objects stored in set are compared. class

  • 0

I have a question on how the objects stored in set are compared.

class A
{
public:  
char * name;
};

I am storing objects of A in a set. I am provide a comparator class which provides implementation of operator() ( A &ob1, A & ob2 ). Here I compare ob1.name & ob2.name and return true when ob1.name is lesser than ob2.name.

Will I be able to search for an object using the find() of set ? I have provided implementation only for operator(). Will this be sufficient ? Could someone explain how find() works in this case ?

Thanks in advance

  • 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-12T07:04:40+00:00Added an answer on June 12, 2026 at 7:04 am

    The function that will be used in std::set::find() is an instance of the Comparator class that you declared as a template parameter of your set.

    template < class Key, class Compare = less<Key>,
               class Allocator = allocator<Key> > class set;
    

    Concretely, the comparator instance will be passed to Key objects and should return true if the first is located before the second. So yes, your implementation is fine.

    Now, to dig further: if you want to convince yourself, you can dig into the source code of gcc’s implementation of the standard library, and you’ll find:

      template<typename _Key, typename _Val, typename _KeyOfValue,
               typename _Compare, typename _Alloc>
        typename _Rb_tree<_Key, _Val, _KeyOfValue,
                  _Compare, _Alloc>::iterator
        _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::
        find(const _Key& __k)
        {
          iterator __j = _M_lower_bound(_M_begin(), _M_end(), __k);
          return (__j == end()
              || _M_impl._M_key_compare(__k, _S_key(__j._M_node))) ? end() : __j;
        }
    

    You can see that _M_key_compare (which is an instance of the _Compare class you provided), is called with __k as a first parameter (one of your Key) and the return value of _S_key(...) which is a key. The return value of _M_key_compare is used in a ternary expression, so it should be a boolean.

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

Sidebar

Related Questions

I have simple set of objects stored in RavenDB: public class Question { public
I have a question on constant objects. In the following program: class const_check{ int
I have the following code: char stats[109]; /* !LINE UNDER QUESTION! */ sprintf(stats, OBJECTS:\n%u/256\n
I have a very basic question in saving objects which I get from a
This is a Hibernate/JPA question. I have a set of Schedule objects, each including
I have a question regarding the integration of business objects developed using Linq To
I have a simple question. Can a grid automatically resize the objects in it
I have a figure below: And my question is that the objects in Array
The question is this. I have a number of persisted objects that I'll pull
I have a question. I first created an object which extends NSObject, I provided

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.