I have created a container for generic, weak-type data which is accessible through the subscript operator.
The std::map container allows both data access and element insertion through the operator, whereas std::vector I think doesn’t.
What is the best (C++ style) way to proceed? Should I allow allocation through the subscript operator or have a separate insert method?
EDIT
I should say, I’m not asking if I should use vector or map, I just wanted to know what people thought about accessing and inserting being combined in this way.
Separate insert method, definitely. The
operator[]onstd::mapis just stupid and makes the code hard to read and debug.Also you can’t access data from a const context if you’re using a
operator[]to insert (which will lead to un-const-cancer, the even-more evil cousin of const-cancer).