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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:23:47+00:00 2026-05-10T16:23:47+00:00

What is the best way (in C++) to set up a container allowing for

  • 0

What is the best way (in C++) to set up a container allowing for double-indexing? Specifically, I have a list of objects, each indexed by a key (possibly multiple per key). This implies a multimap. The problem with this, however, is that it means a possibly worse-than-linear lookup to find the location of an object. I’d rather avoid duplication of data, so having each object maintain it’s own coordinate and have to move itself in the map would be bad (not to mention that moving your own object may indirectly call your destructor whilst in a member function!). I would rather some container that maintains an index both by object pointer and coordinate, and that the objects themselves guarantee stable references/pointers. Then each object could store an iterator to the index (including the coordinate), sufficiently abstracted, and know where it is. Boost.MultiIndex seems like the best idea, but it’s very scary and I don’t wany my actual objects to need to be const.

What would you recommend?

EDIT: Boost Bimap seems nice, but does it provide stable indexing? That is, if I change the coordinate, references to other elements must remain valid. The reason I want to use pointers for indexing is because objects have otherwise no intrinsic ordering, and a pointer can remain constant while the object changes (allowing its use in a Boost MultiIndex, which, IIRC, does provide stable indexing).

  • 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. 2026-05-10T16:23:48+00:00Added an answer on May 10, 2026 at 4:23 pm

    I’m making several assumptions based on your writeup:

    • Keys are cheap to copy and compare
    • There should be only one copy of the object in the system
    • The same key may refer to many objects, but only one object corresponds to a given key (one-to-many)
    • You want to be able to efficiently look up which objects correspond to a given key, and which key corresponds to a given object

    I’d suggest:

    • Use a linked list or some other container to maintain a global list of all objects in the system. The objects are allocated on the linked list.
    • Create one std::multimap<Key, Object *> that maps keys to object pointers, pointing to the single canonical location in the linked list.
    • Do one of:
      • Create one std::map<Object *, Key> that allows looking up the key attached to a particular object. Make sure your code updates this map when the key is changed. (This could also be a std::multimap if you need a many-to-many relationship.)
      • Add a member variable to the Object that contains the current Key (allowing O(1) lookups). Make sure your code updates this variable when the key is changed.

    Since your writeup mentioned ‘coordinates’ as the keys, you might also be interested in reading the suggestions at Fastest way to find if a 3D coordinate is already used.

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

Sidebar

Ask A Question

Stats

  • Questions 62k
  • Answers 62k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Look in the configuration.php file. There's a couple of variables… May 11, 2026 at 10:09 am
  • added an answer You don't need to create numpy arrays to call numpy.std().… May 11, 2026 at 10:09 am
  • added an answer I have a Python script to make moving around a… May 11, 2026 at 10:09 am

Related Questions

What is the best way (in C++) to set up a container allowing for
What is the best way to graph scatter plots in C++? Do you write
What is the best way to initialize a private, static data member in C++?
What is the best way of doing case-insensitive string comparison in C++ without transforming
In general, what is the best way of storing binary data in C++? The
What is the best way in Perl to copy files to a yet-to-be-created destination
What is the best way to store international addresses in a database? Answer in
What is the best way to display Flash content in a C# WinForms application?
What is the best way to find something in a list? I know LINQ
What is the best way to track changes in a database table? Imagine you

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.