|
|
|
|
|
by OskarS
495 days ago
|
|
I kinda like it on occasion. Works like pythons defaultdict. Like, if you wanna count something: for (const auto &thing: collection) {
counts[thing]++;
}
Works nicely, you don't have to check if it's already there before ++ing it. As long as you know that's what operator[] does, it comes in handy more than I would've expected. |
|
Another popular use case is something along the lines of:
That said, I don't know what behavior I'd want if maps didn't automatically insert an element when the key was absent. UB (as with vector)? Throw an exception? Report the incident to Stroustrop? All these options feel differently bad.