samedi 8 mai 2021

I am getting unexpected behavour of count() in map C++

#include<bits/stdc++.h>
using namespace std;
int main() {

    map<int, int> nums_map;
    cout << nums_map.count(0) << endl;
    int a = nums_map[0];
    cout << nums_map.count(0) << endl;
    cout << nums_map[0];
    return 0;
}

OUTPUT: 0 1 0

It makes no sense to me at least, why the line: int a = nums_map[0]; is increasing the value of count by 1, also the nums_map.empty() = 0 at the same time.

Aucun commentaire:

Enregistrer un commentaire