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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:51:32+00:00 2026-06-14T00:51:32+00:00

I have tried searching for this but couldn’t really find a proper answer. I

  • 0

I have tried searching for this but couldn’t really find a proper answer. I have a stl::set of a custom class I made.

The class looks like

class myObject{

public:
  int a;
  int b;
  int c;

// constructor and operator < ()
}

The < comparison is based on b and c but I want to find elements in the set by a. Is there a way to do that other than performing a linear search by iterating over the set and checking for myObject.a? I am trying to get a sorted container of myObject but I need to be able to find elements in that container by the identifier a which is not really involved in the < comparison.

  • 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-14T00:51:33+00:00Added an answer on June 14, 2026 at 12:51 am

    You can do it using boost::multi_index_container

    class myObject
    {
    public:
        int a;
        int b;
        int c;
        bool operator < (const myObject& obj) const
        { return b < obj.b; }
    };
    
    using namespace boost::multi_index;
    typedef multi_index_container<
        myObject,
        indexed_by<
            ordered_unique<identity<myObject> >, // index by operator <
            ordered_unique<member<myObject, int, &myObject::a> > // index by member a
      > 
    > SetOfmyObjects;
    
    typedef SetOfmyObjects::nth_index<0>::type SetIndex_b;
    typedef SetOfmyObjects::nth_index<1>::type SetIndex_a;
    

    …

        SetOfmyObjects s;
        const SetIndex_b& index_b = s.get<0>();
        SetIndex_b::iterator it_b;
        for (it_b = index_b.begin(); it_b != index_b.end(); ++it_b)
            // ordered by b
        const SetIndex_a& index_a = s.get<1>();
        SetIndex_a::iterator it_a;
        for (it_a = index_a.begin(); it_a != index_a.end(); ++it_a)
            // ordered by a
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried searching for this but couldn't find a similar question. I have a
Ok, I have tried searching around for this answer, but with no luck. I
I've tried searching for this but couldn't find examples that suited my situation. I
I have been searching a solution for this since 2 days but couldn't find
Tried to do some searching on this, but couldn't find exactly what I needed.
First post here, I have tried searching but couldn't find what I'm looking for
I've tried searching for an answer to my question but I couldn't find one
I have tried searching over the internet about this problem but not able to
I've tried Googling and searching stackoverflow, but have yet to find a solution too
Have tried to find solutions for this and can't really come up with anything.

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.