reading object from const unordered_map

The expression z[5] calls a non-const member function of the map.

This is because a map’s operator[] will insert a new element if the key isn’t found, so obviously it has to be non-const.

For a vector nothing is inserted by operator[], the element must exist already (or you get undefined behaviour, so the equivalent code would access the 6th element of an empty vector, which is not fine!).

To lookup a key without adding it use:

int val = 0;
auto it = z.find(5);
if (it != z.end())
  val = it->second;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)