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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:48:36+00:00 2026-05-22T21:48:36+00:00

I’m looking to replace a vector<string> and a boost::unordered_map<string, size_t> mapping string to indices

  • 0

I’m looking to replace a vector<string> and a boost::unordered_map<string, size_t> mapping string to indices in the former with a boost::bimap.

What instantiation of bimap should I use? So far, I’ve come up with

typedef bimap<
    unordered_set_of<size_t>,
    vector_of<string>
> StringMap;

but I’m not sure if I’ve reversed the collection types now. Also, I wonder if I should change the collection of relations type. Would a vector_of_relation be my best choice, or a set_of_relation, or just go with the default?

  • 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-22T21:48:37+00:00Added an answer on May 22, 2026 at 9:48 pm

    To get a bimap between size_t and std::string where you have ~constant (up to the cost of hashing and any potential clashes) you need to use unordered_set_of:

    #include <boost/bimap.hpp>
    #include <boost/bimap/unordered_set_of.hpp>
    #include <string>
    #include <iostream>
    #include <typeinfo>
    
    int main(int argc, char* argv[]) {
    
      typedef boost::bimap< boost::bimaps::unordered_set_of<size_t>, boost::bimaps::unordered_set_of<std::string> > StringMap;
      StringMap map;
      map.insert(StringMap::value_type(1,std::string("Cheese")));
      map.insert(StringMap::value_type(2,std::string("Cheese2")));
    
      typedef StringMap::left_map::const_iterator const_iter_type;
      const const_iter_type end = map.left.end();
    
      for ( const_iter_type iter = map.left.begin(); iter != end; iter++ ) {
        std::cout << iter->first << " " << map.left.at(iter->first) << "\n";
      }
    
    }
    

    returns:

    1 Cheese
    2 Cheese2
    

    The unordered_set is a boost version of set which uses hash tables instead of trees to store the elements, see Boost Unordered docs.

    Looking at the comments from one of the bimap examples at Bimap example, we have:

    The left map view works like a std::unordered_map< std::string, long >,
    given the name of the country we can use it to search for the population in constant time

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

Sidebar

Related Questions

No related questions found

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.