Possible Duplicate:
Why does stack<const string> not compile in g++?
We know that vector<const int> is not allowed.
But is map<const int, int>, map<int, const int>, or map<const int, const int> allowed?
Why (not)?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Items in a vector must be copy-constructible or (as of C++11) move-constructible.
The key in a map only needs to be destructible, not copy/move constructible, so the requirements are much looser in this respect.
As to why that is, it’s pretty simple: when/if a vector gets resized, the data from the existing buffer must be copied or moved into the new buffer. By contrast, a map normally stores its contents as nodes in a tree. Once a node is created, it will simply exist until it is destroyed. While the tree does need balancing at times, that only requires manipulating pointers between the nodes — the key in the node is never modified after the node is created.