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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:25:43+00:00 2026-06-14T08:25:43+00:00

I have elements a and b of two sets A and B. Now these

  • 0

I have elements a and b of two sets A and B. Now these are related to each other (0..1:n cardinality) so each a has at most one partner in B and each b can have several (at least one) associations to items in A.
A is a set of integer pairs and B are integers.

Is there efficient way to store such a “bi-directional” map?
A simple approach would be to use two maps:

map<pair<unsigned int, unsigned int>, unsigned int> AtoB
map<unsigned int, vector<pair<unsigned int, unsigned int> > > BtoA

But perhaps there is good way to deal with this more efficiently.

Thanks for your help

  • 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-14T08:25:44+00:00Added an answer on June 14, 2026 at 8:25 am

    Boost contains two libraries to deal with this: Boost.Bimap and Boost.MultiIndex. The former is specific to the problem of bijective (“bidirectional”) maps, while the second is more general and implements something akin to an in-memory database with arbitrary indexes.

    Given that your unsigned int keys don’t uniquely map to your pairs, I think MultiIndex is more in order. It’s a long time since I’ve last used this library, but looking at the tutorial, you would need something like

    struct YourData {
         unsigned key;
         std::pair<unsigned, unsigned> value;
    };
    
    typedef multi_index_container<
        YourData,
        indexed_by<
            ordered_non_unique<member<YourData, unsigned, &YourData::key> >,
            ordered_unique<member<YourData, std::pair<unsigned, unsigned>,
                                  &YourData::value> >
        >
    > YourContainer;
    

    If you don’t want to use Boost, then you can at least simplify your current setup by replacing the

    map<unsigned int, vector<pair<unsigned int, unsigned int> > >
    

    by an std::multimap<unsigned, std::pair<unsigned, unsigned>>.

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

Sidebar

Related Questions

Assume we have three sets of strings in Scala. One has elements A,B,C. Two
I have two div -elements, the top one has the height of 40% and
How can I add elements from two sets? If there's a set one (1,
Say I have two sets of XML elements: <One/> <Two/> <Three/> And <A/> <B/>
I have two sets of elements with (sometimes) corresponding rel and id attributes: <a
I'm trying to do the following: I have two sets of DOM elements on
I a template, I have five DIV elements underneath each other on a HTML
I have two sets of elements and I want an optimal algorithm to find
I have two set of draggable and two sets of dropable elements. What I
I have two sets of elements A:[1,2,3,4] and B:[5,6,7,8] and I would like to

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.