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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:54:14+00:00 2026-05-28T02:54:14+00:00

I am having issues understanding how std::set (or std::map etc) identify unique keys. The

  • 0

I am having issues understanding how std::set (or std::map etc) identify unique keys. The thing I am trying to do is wrap a struct object within boost::shared_ptr and then store that shared pointer within std::set container:

Let’s say the struct is a color:

struct Color {
    float r;
    float g;
    float b;
};

Then the container and comparison function object is defined in another class:

class AnotherClass {

    typedef boost::shared_ptr<Color> ColorPtr;

    public:

        struct ColorCompare {
            bool operator()(const ColorPtr &a, const ColorPtr &b) const {
                return (a->r > b->r) && (a->g > b->g) && (a->b > b->b);
            }
        };

    private:

        // Container definition
        std::set<ColorPtr, ColorCompare> colors;
};

The code above is failing to uniquely identify shared_ptr objects based on their wrapped Color struct. I always thought that the std::set container would run a comparison function on two objects, and if none of them is greater than or less than the other – it will assume they are equal. Note, that I cannot use default shared_ptr::operator<() and less<...> as that implementation is based on pointer address.

What am I missing?

p.s. I am wrapping colors within shared_ptr since I need to know their reference count at some point (and remove colors that are at ref count 1 – that is, only referenced by std::set container itself). Is there a better way to get the same result?

  • 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-05-28T02:54:15+00:00Added an answer on May 28, 2026 at 2:54 am

    The comparison needs to be a strict weak ordering, which yours isn’t. (For example, how are (1,0,0) and (0,1,0) ordered?) Do this:

    return (a->r > b->r)                                   ||
           ((a->r == b->r) && (a->g > b->g))               ||
           ((a->r == b->r) && (a->g == b->g) && (a->b > b->b) );
    

    This is the standard lexicographic ordering on a tuple of elements.

    Note that you would write !(b->r > a->r) instead of a->r == b->r in general so that you don’t introduce a dependence on a compatible equality operator, though in this simple case of floats we’re fine.

    By the way, you don’t need a struct, you can simply declare a static bool ColorCompare(...); function. Another option is to define an operator< directly in the struct Color to make everything self-contained (so you’ll just need a generic dereferencing-comparator for (smart) pointers).

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

Sidebar

Related Questions

Trying to get some basic understanding of console functionalities. I am having issues so
I'm having some issues understanding how to reference new browser windows after opening them.
I'm trying to familiarize myself with the STL library, but I'm having trouble understanding
I am having some difficulty understanding callbacks and program flow, synchronization issues. Lets say
I'm just getting started with M-V-VM and WPF and having issues understanding some binding
Im reading c++ primer plus and having some issues understanding how implicit instantiation works.
I'm having issues understanding how to configure TFS according to best practices as provided
I'm having issues understanding exactly what array_splice and array_slice do. From what I can
I'm having issues with understanding how I should organize my user interface in Android.
I'm having issues understanding the concept of outlets how the iPhone deals with events.

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.