map::operator[] should just be [[deprecated]] - even if you want to mutate the map it's probably not the best way to do what you want.
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:
std::unique_ptr<T>& ptr = ptrs[key];
if (ptr != nullptr) {
ptr = std::make_unique<T>(...);
}
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.