When I want get certain element from a std::map, as far as I know at() will do a bounds check. Does that mean it's slower than []?
1 Answer
Both std::map::at and std::map::operator[] check for the presence of a key. The former throws an exception when not found, the latter creates a default value.
Their complexity is mandatory to be log(n) (look-up). Performance by itself is not mandatory to be identical, but should be similar in practice.
So choose the one to use from their behavior (after the look-up). Similar alternatives might be map::find or in some cases map::insert/insert_or_assign/emplace.
[]support it is slower because of the bounds check, butmaphas very different behaviour.