Answers for "unordered map in c++"

C++
17

map vs unordered_map in C++

| map             | unordered_map
---------------------------------------------------------
Ordering        | increasing  order   | no ordering
                | (by default)        |

Implementation  | Self balancing BST  | Hash Table
                | like Red-Black Tree |  

search time     | log(n)              | O(1) -> Average 
                |                     | O(n) -> Worst Case

Insertion time  | log(n) + Rebalance  | Same as search
                      
Deletion time   | log(n) + Rebalance  | Same as search


::-> Use std::map when
1. You need ordered data.
2. You would have to print/access the data (in sorted order).
3. You need predecessor/successor of elements.
  
::-> Use std::unordered_map when
1. You need to keep count of some data (Example – strings) and no ordering is required.
2. You need single element access i.e. no traversal.
Posted by: Guest on June-08-2020
8

hashmap in cpp

#include <unordered_map>
#include <iostream>

int main()
{
    std::unordered_map<std::string, int> age;
    // Insert
    age["Michael"] = 16;
    age.insert(std::pair<std::string, int>{"Bill", 25});
    age.insert({"Chris", 30});

    // Search and change
    age["Michael"] = 18;
    age.at("Chris") = 27;

    // Check if key exists
    std::string query;
    query = "Eric";
    if (age.find(query) == age.end())
    {
        std::cout << query << " is not in the dictionary!" << std::endl;
    }

    // Delete
    query = "Michael";
    if (age.find(query) == age.end())
    {
        std::cout << query << " is not in the dictionary!" << std::endl;
    }
    age.erase(query);
    if (age.find(query) == age.end())
    {
        std::cout << query << " is not in the dictionary!" << std::endl;
    }

    // Iterate
    for (const std::pair<std::string, int>& tup : age)
    {
        std::cout << "Name: " << tup.first << std::endl;
        std::cout << "Age: " << tup.second << std::endl;
    }
}
Posted by: Guest on November-10-2020
2

header file for unordered_map in c++

#include<unordered_map> //is the stl for unordered map
Posted by: Guest on July-08-2020
0

unordered_map c++

#include <bits/stdc++.h>
#include <iostream>
#include <map>
#include <unordered_map>

using namespace std;

int main() {
  	map<char, int> M; //based on balanced binary tree takes O(logn) access time
	unordered_map<char, int> U; //uses hashing and accessing elements takes O(1)
	//U.add(key,value);
  	//U.erase(key,value);
  	
  	//map each letter to their occurance
  	string s = "Sumant Tirkey";
  	for (char c : s) {
  		M[c]++;
	  }
	for (char c : s){
		U[c]++;
	}
  
  return 0;
}
Posted by: Guest on December-03-2020
0

unordered map in c++

#include <unordered_map>
#include <iostream>

int main()
{
    std::unordered_map<std::string, int> age;
    // Insert
    age["Michael"] = 16;
    age.insert(std::pair<std::string, int>{"Bill", 25});
    age.insert({"Chris", 30});

    // Search and change
    age["Michael"] = 18;
    age.at("Chris") = 27;

    // Check if key exists
    std::string query;
    query = "Eric";
    if (age.find(query) == age.end())
    {
        std::cout << query << " is not in the dictionary!" << std::endl;
    }

    // Delete
    query = "Michael";
    if (age.find(query) == age.end())
    {
        std::cout << query << " is not in the dictionary!" << std::endl;
    }
    age.erase(query);
    if (age.find(query) == age.end())
    {
        std::cout << query << " is not in the dictionary!" << std::endl;
    }

    // Iterate
    for (const std::pair<std::string, int>& tup : age)
    {
        std::cout << "Name: " << tup.first << std::endl;
        std::cout << "Age: " << tup.second << std::endl;
    }
}
Posted by: Guest on October-13-2021

Browse Popular Code Answers by Language