Jaewoo Song
Jaewoo Song


  • Tech


In this post, I checked that if we want to update the value for a certain key into another, we simply can do it with map[key] = value, while map.insert(value) does not update the value properly.

I got curious: “If a certain key in the map is not initialized, what is the default value of this key?”

#include <iostream>
#include <map>

using namespace std;

int main() {

    map<int, int> int_map;
    map<int, char> char_map;
    map<int, bool> bool_map;
    map<int, string> str_map;
    map<int, double> double_map;


    return 0;

With the above test, I got results like below.

The result of checking default values of map above.

At least there is no error or exception, so we can think that even if the value is not initialized, default values are returned if we access them.

And we can see that in the case of char and string, empty values were printed.

To see specifically, I transformed char into ASCII decimal integer and changed the code to print the length of string.

Both results are $0$.

Therefore, the former is Null character and the latter is a string with 0 size.

type default value
int 0
double 0.0
char NULL
bool false
string ””

Actually, there is nothing special because it is the same as the default value of global arrays.