R
Rui Maciel
When using STL's map container, if operator[] is used to access the value associated with a
given key which wasn't inserted then a new key:value pair will be created and a reference to
the value will be returned. This means that, although operator[] is a convenient way to add
new key:value pairs to a map, it may end up needlessly adding useless key:value pairs into the
map.
In order to avoid wasting resources populating a map with useless key:value pairs, is there a
clean, unencumbered way to get the value associated with a given key without being forced to
insert new elements or even resort to multiple lines of code? The closest thing I saw was the
map::find() method, but I believe that ends up forcing to write code to compare the given
iterator to map::end() and, if it matches, return a default value.
Is there a simpler way to do this?
Rui Maciel
given key which wasn't inserted then a new key:value pair will be created and a reference to
the value will be returned. This means that, although operator[] is a convenient way to add
new key:value pairs to a map, it may end up needlessly adding useless key:value pairs into the
map.
In order to avoid wasting resources populating a map with useless key:value pairs, is there a
clean, unencumbered way to get the value associated with a given key without being forced to
insert new elements or even resort to multiple lines of code? The closest thing I saw was the
map::find() method, but I believe that ends up forcing to write code to compare the given
iterator to map::end() and, if it matches, return a default value.
Is there a simpler way to do this?
Rui Maciel