nathan
nathan

Reputation: 844

default value in C++ container library

I saw some code

    unordered_map<int, int> table;
    for (int i = 0; i < nums1.size(); i++) {
        table[nums1[i]]++;
    }

nums1 is input vector array, I know the value of pair is the occurrence of entry in nums1 My question is default value of table[nums[i]] is alway 0 ?? just like default value of a local int variable ?

Upvotes: 0

Views: 257

Answers (1)

Telokis
Telokis

Reputation: 3389

Yes.

From cppreference :

operator[] is non-const because it inserts the key if it doesn't exist.

It inserts it by default-constructing it which, for an int, sets it to 0.

Upvotes: 1

Related Questions