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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:26:43+00:00 2026-06-17T18:26:43+00:00

How would I implement the comparison operator in the example below so that ObjectPair(

  • 0

How would I implement the comparison operator in the example below so that ObjectPair( &a, &b ) is equal to ObjectPair( &b, &a )? In addition, how would I implement this using stdext::hash_map instead of std::map?

struct ObjectPair
{
    public:

        ObjectPair( Object* objA, Object* objB )
        {
            A = objA;
            B = objB;
        }

        bool operator<( const ObjectPair& pair ) const
        {
            // ???
        }

        Object* A;
        Object* B;
};

int main()
{
    std::map< ObjectPair, int > pairMap;

    Object a;
    Object b;

    pairMap[ ObjectPair(&a, &b) ] = 1;
    pairMap[ ObjectPair(&b, &a) ]++;    

    /// should output 2
    std::cout<< pairMap[ ObjectPair( &a, &b ) ] << std::endl;

    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-17T18:26:45+00:00Added an answer on June 17, 2026 at 6:26 pm

    Your fundamental problem is you need to implement operator< such that it doesn’t distinguish between a and b, and yet returns consistent results for all nonequal objects.

    The simplest thing to do is probably to sort the pointers and then compare them. Something like

    bool operator<(const ObjectPair& pair) const {
        // Technically < is unspecified on most object pointers
        // but std::less<T> is guaranteed to have a total ordering
        std::less<Object*> comp;
        Object *ourlow = std::min(a, b, comp);
        Object *ourhigh = std::max(a, b, comp);
        Object *theirlow = std::min(pair->a, pair->b, comp);
        Object *theirhigh = std::max(pair->a, pair->b, comp);
        if (comp(ourlow, theirlow)) return true;
        if (comp(theirlow, ourlow)) return false;
        return comp(ourhigh, theirhigh);
        }
        return false;
    }
    

    This is assuming, of course, that Object is not sortable, and therefore we only care about the pointer value being the same. If Object itself has an ordering, then you should probably call Object::operator<() instead of just using < on the pointer, i.e. if (*ourlow < *theirlow)


    In order to make this work in a std::unordered_map (which is a C++11 thing that I’m assuming stdext::hash_map is the equivalent of) then you’ll need to implement operator== as well as specialize std::hash<> for your object. For your specialization you may just want to hash the two pointers and combine the values (using something like bitwise-xor).


    The gist of the really long comment thread attached to this question has to do with what the C++ standard says about comparisons on pointers. Namely, that comparing two object pointers of the same type that are not members of the same object/array invokes unspecified behavior. In general, this isn’t going to matter on any architecture with a single unified memory system (i.e. any architecture you’re likely to use), but it’s still nice to be standards-compliant. To that end, the comparisons have all been changed to use std::less<Object*>, because the C++ standard guarantees that std::less<T> has a total ordering.

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

Sidebar

Related Questions

How would one implement a ternary comparison operator to determine, for example, the boolean
How would you implement a Listener that is queuing events that are executed at
How would you implement a generic stack without using nsmutablearrays?
Not infrequently, one wants to implement the <=> (comparison, or spaceship) operator on a
I think that Entities should implement equality by primary key comparison as default, but
I want to implement a String comparison function that doesn't take a different amount
I have a design for a GC algorithm that I would like to implement
I've started using googletest to implement tests and stumbled across this quote in the
This is my source code. I'trying to implement a simple program that asks a
looking at dribbble, I have started to ask myself, how I would implement a

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.