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

  • Home
  • SEARCH
  • 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 6740363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:35:49+00:00 2026-05-26T11:35:49+00:00

I have a class like this: struct Thing { unsigned index; // more data

  • 0

I have a class like this:

struct Thing
{
    unsigned index;
    // more data members
};

I am using a std::map<Thing> to contain my Things. Calling code looks something like this:

Thing myThing(/*...*/);
std::map<Thing> things;
things[myThing.index] = myThing;
// ...
Thing &thing3 = things[3];

I was wondering if there was an approach that could use Thing::index directly without the need implicitly to copy it into pair::first.

I guess I would need to provide some sort of Thing comparison operator, but that’s OK. std::set might work, but I would need a whole Thing object as key:

std::set<Thing> things;
Thing &thing3 = *things.find(Thing(3));

Other than refactoring Thing into a std::pair, can I do better with STL?

  • 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-26T11:35:50+00:00Added an answer on May 26, 2026 at 11:35 am

    I don’t see why

    inline bool operator<(const Thing& a,const Thing& b) {
      return (a.index<b.index);
    }
    
    std::set<Thing> things;  // Uses comparison operator above
    
    Thing &thing3 = *things.find(Thing(3));
    

    doesn’t do exactly what you want. No duplication/copying of the index field, and key comparison as efficient as the map approach; what’s not to like ?

    Update in light of comments below:

    If Thing is so heavyweight that you don’t want to copy it, then you’d probably end up with code more like:

    inline bool operator<(const shared_ptr<Thing>& a,const shared_ptr<Thing>& b) {
      return (a->index < b->index);
    }
    
    std::set<shared_ptr<Thing>> things;  // Uses comparison operator above
    
    shared_ptr<Thing> key3(new Thing(3));   
    
    Thing &thing3 = *things.find(key3);
    

    although IMHO overriding pointer value comparison is fairly evil and it’d be better to go the more verbose route of an explicit “Compare” argument to the set template args.

    One thing to bear in mind (from my own experience of big priority queues of heavyweight objects): there may be significant advantages to std::pair<trivial_key,shared_ptr<heavyweight_object>>-based containers over std::pair<trivial_key,heavyweight_object> due to the fact that traversals of the former touching only keys (e.g find) can be much more cache efficient compared with the latter which will also fetch a lot of mostly unneeded/irrelevant heavyweight_object bytes into cache (depends on the details and numbers of course, but in fact this effect could easily completely swamp the relatively minor cost of duplicating the key).

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

Sidebar

Related Questions

I have a struct like this: class Items { private: struct item { unsigned
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a class here that is defined like this: struct USERFPOINT { POINTFLOAT
I have a class like this: public class Stretcher : Panel { public static
I have a class like this: public class myClass { public List<myOtherClass> anewlist =
I have a class like this public class foo { private void getThread() {
I have a class like this: public class Contest { List<ContestTeam> Teams { get;
I have a class like this: class A { private: B* ptr; } But
I have a class like this class GUI : public QWidget, public QThread When
I have a class like this, public class person { name Name {set; get;}

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.