Let's say you have 26 vectors, and you want them to be associated with keys in the range [A, Z]. Suppose, by accident, you write "C" as "c". This would be an entirely incorrect key. Unfortunately, there is little you can do about it other than to manually check. (That said, you could write an automated test to check, but the process of writing the test is um, manual labour.)
Yes, but there is the risk of using an existing mapped value when you want to be inserting a new mapped value with a new key.
Then you're good to go
Given this prototype for the member operator[] of std::map:
Code:
T& operator[](const key_type& x);
This is its canonical implementation:
Code:
return (*((insert(make_pair(x, T()))).first)).second;
Observe that insert is used, and the members of the struct returned is accessed. An implementation is not strictly required to implement operator[] in this way, but whatever implementation used must essentially do the same thing.