Tuhin Panda
Tuhin Panda

Reputation: 21

how an unordered_map is initialized with the values of array

I have come across a code where i get confused , An unordered_map is initialised like below

std::unordered_map<std::string, int> wordMap;

// Inserting elements through an initializer_list
wordMap.insert({ {"First", 1}, {"Second", 2}, {"Third", 3} } );

But what surprise me is the below code

int arr[] = { 1, 5, 2, 1, 3, 2, 1 };
unordered_map<int, int> hash; 
    for (int i = 0; i < n; i++) 
        hash[arr[i]]++;

Here i am not getting how key and value is inserted in the map

Upvotes: 1

Views: 1819

Answers (3)

Kunal Puri
Kunal Puri

Reputation: 3427

Here, In unordered_map, hash[arr[i]]++; works in this way:

  1. It searches for a key (arr[i]). If it is found, the corresponding value is incremented by 1.

  2. If it is not found, a new element will be created with key arr[i] and because value is of type int, default value of 0 is stored for it. Because of ++ operator, it will be incremented by one. So, at the end of the operation, the value will be 1.

To be very explicit for your example, it works like this:

i = 0 => arr[i] = 1 => Not present in map => New pair added => hash: [{1, 1}]
i = 1 => arr[i] = 5 => Not present in map => New pair added => hash: [{1, 1}, {5, 1}]
i = 2 => arr[i] = 2 => Not present in map => New pair added => hash: [{1, 1}, {5, 1}, {2, 1}]
i = 3 => arr[i] = 1 => Present in map => Existing pair updated => hash: [{1, 2}, {5, 1}, {2, 1}]
i = 4 => arr[i] = 3 => Not present in map => New pair added => hash: [{1, 2}, {5, 1}, {2, 1}, {3, 1}]
i = 5 => arr[i] = 2 => Present in map => Existing pair updated => hash: [{1, 2}, {5, 1}, {2, 2}, {3, 1}]
i = 6 => arr[i] = 1 => Present in map => Existing pair updated => hash: [{1, 3}, {5, 1}, {2, 2}, {3, 1}]

The order mentioned here might be different from actual one. The above explanation is just to explain things.

Upvotes: 2

Spinkoo
Spinkoo

Reputation: 2080

operator[] checks if the element exists. If it doesn't then it creates one using default constructor and returns a reference (or const reference to it). ie :

 hash[arr[0]]++
 it creates hash[1]first 

which is

hash[1]++ => hash[1]=hash[1]+1 which is 0+1 ( since hash[1] at the begining was 0 by default.. )
when it get to the second 1 it become hash[1]=hash[1]+1 = 2 ...

..ect same for other values

basically it s creating & counting the number of the duplicates in the array

at the end it gives you

hash[1]=3
hash[2]=2
hash[3]=1
hash[5]=1

Upvotes: 0

nauman
nauman

Reputation: 151

The key of the unordered map must be unique so all 1:s will be combined. But when they do combine the loop will add 1 to the value side:

hash[arr[i]]++ will be equal to this example: hash[1] += 1;

Since there are three 1 values, hash[1] will end up with a value of 3. You will find two records of the value 2 and this will make hash[2] = 2.

#include <iostream>
#include <unordered_map>

int main()
{
    int arr[] = { 1, 5, 2, 1, 3, 2, 1 };
    std::unordered_map<int, int> hash; 
    for (int i = 0; i < 7; i++) {
        hash[arr[i]] += 1;
    }
    for (auto i : hash) {
        printf("%i:%i\n", i.first, i.second);
    }
}
# Output:
#   3:1
#   2:2
#   5:1
#   1:3

Upvotes: 0

Related Questions