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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:43:46+00:00 2026-05-16T11:43:46+00:00

I know that STL has a HashMap API, but I cannot find any good

  • 0

I know that STL has a HashMap API, but I cannot find any good and thorough documentation with good examples regarding this.

Any good examples will be appreciated.

  • 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-16T11:43:46+00:00Added an answer on May 16, 2026 at 11:43 am

    The standard library includes the ordered and the unordered map (std::map and std::unordered_map) containers. In an ordered map (std::map) the elements are sorted by the key, insert and access is in O(log n). Usually the standard library internally uses red black trees for ordered maps. But this is just an implementation detail. In an unordered map (std::unordered_map) insert and access is in O(1). It is just another name for a hashtable.

    An example with (ordered) std::map:

    #include <map>
    #include <iostream>
    #include <cassert>
    
    int main(int argc, char **argv)
    {
      std::map<std::string, int> m;
      m["hello"] = 23;
      // check if key is present
      if (m.find("world") != m.end())
        std::cout << "map contains key world!\n";
      // retrieve
      std::cout << m["hello"] << '\n';
      std::map<std::string, int>::iterator i = m.find("hello");
      assert(i != m.end());
      std::cout << "Key: " << i->first << " Value: " << i->second << '\n';
      return 0;
    }
    

    Output:

    23
    Key: hello Value: 23
    

    If you need ordering in your container and are fine with the O(log n) runtime then just use std::map.

    Otherwise, if you really need a hash-table (O(1) insert/access), check out std::unordered_map, which has a similar to std::map API (e.g. in the above example you just have to search and replace map with unordered_map).

    The unordered_map container was introduced with the C++11 standard revision. Thus, depending on your compiler, you have to enable C++11 features (e.g. when using GCC 4.8 you have to add -std=c++11 to the CXXFLAGS).

    Even before the C++11 release GCC supported unordered_map – in the namespace std::tr1. Thus, for old GCC compilers you can try to use it like this:

    #include <tr1/unordered_map>
    
    std::tr1::unordered_map<std::string, int> m;
    

    It is also part of boost, i.e. you can use the corresponding boost-header for better portability.

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

Sidebar

Related Questions

I know that Phonegap has an event for back button, but it's only available
I know that this sort of question has been asked here before, but still
I know that Java have its own garbage collection, but sometimes I want to
I know that many threads has been created here & on the internet about
I have used unsorted_map from TR1. I never know any data structure from STL
Simple question: I know that the STL library is not provided inside the Android
I know that concurrent adds to an stl queue in c++ can cause issues,
I have an application that has several objects (about 50 so far, but growing).
I know that in STL vector represents the implementation of a dynamic array. So
I just has a vector like that vector<User*> users; I know it is not

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.