accessing M[0] in the if condition ? (because there's no such a key priorly)
std::map<int, int> M;
if(M[0])
{
++M[0];
}
else
{
M[0] = 0;
}
You didn't do any initialization here, right? So isn't M just filled with junk? I don't really know. I'm not a C++ guy
You're right I think, cause uninitialized objects can cause UB
And, if there is a method for testing if a key exists you probably want to use it
I thought it inserts the requested key without causing any ub.
Yeah. I can initially insert pairs of "key : 0" into my map, but I'd like to know if there's a better way too.
So is this defined too ?
This does not cause UB. The use of operator[] with a non existent key inserts that key and a value constructed value of value_type
Обсуждают сегодня