Reputation: 181
Hi I was wondering if I have a map with int as element and list of int as value and I try to access to a key which is not in the map. The operator return a default value. But what is suppose to be a default value for a list ? the default constructor ?
Upvotes: 2
Views: 75
Reputation: 6260
Yes, it will default construct a list. See here.
If this is not the behaviour you want, you can use std::map::at
(see here) instead of std::map::operator[]
to access elements, which will throw an exception if the key doesn't exist, rather than inserting a new element.
Note: my answer assumes that you were referring to the std
namespace in your question.
Upvotes: 3
Reputation: 158479
If the key
is not in the map than operator []
inserts a new element with that key
and uses the default constructor
, which you can read more about here
The default constructor for std::list
constructs an empty
container.
Upvotes: 1