Tech Blogs
  • Vivek bhargav
  • Books
    • Seven Databases In Seven Weeks
      • Factors to consider
      • Genres of databases
      • Important questions
      • PostGreSQL
  • Tech
    • C++
      • Type Conversions
      • String
      • Vector
      • Set
      • Unordered Set
      • Map
      • Unordered Map
      • Queue
      • Priority Queue
      • Union find
      • Algorithms
      • Matrix to Graph
      • Trie
      • Dijkstra
      • Math
      • Utils
    • Database Transactions
      • A Simple Transaction Analysis
      • Implementation of Isolation Levels
      • Isolation Levels
      • Isolation
      • Storage Types
      • Transaction Atomicity and Durability
    • Java
      • Important Questions
      • Spring MVC
    • Program execution
      • Node.js
      • Runtimes
    • System Design
      • Basic Terminologies
      • CAP Theorem
      • Normalization of Database
      • Useful Reads
  • Personal Finance
    • Asset Classes
      • Equity instruments
      • Debt instruments
Powered by GitBook
On this page
  1. Tech
  2. C++

Unordered Map

#include <unordered_map>  

unordered_map<string, int> umap;                            // create a map of string: {integer}

// create a map with few initial elements 
unordered_map<string, int> umap = {
                                     {"batman", 100}, 
                                     {"superman", 500}, 
                                     {"shaktiman", 200}
                                  };     


cout << umap.size() << endl;                                // number of enteries in myMap
umap["spiderman"] = 150;                                    // add entry to the map

int bmMarks = umap.at("batman");                            // get a value in map corresponding to a key
int smMarks = umap["superman"];

umap.erase("superman");                                     // erase an entry from the map
umap.clear();                                               // remove all the enteries from the map

// check whether map is empty
if(umap.empty()){
    ...
}

// search for an element
if (umap.find("shaktiman") != umap.end()) {
    // It means key: "shaktiman" exists in the map
}

// iterate over all elements
for(unordered_map<string, int>::iterator it = umap.begin(); it != umap.end(); it++) {
    cout << *it << endl;
    cout << it->first << endl;
    cout << it->second << endl;
}
  
PreviousMapNextQueue

Last updated 2 years ago