-1

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)?

Community
  • 1
  • 1
Dennis Ritchie
  • 1,281
  • 1
  • 10
  • 15
  • 1
    Gaming SO is fun, isn't it? – Nikos C. Dec 07 '12 at 18:03
  • 1
    'Dennis Ritchie' is a daring nick ;) ... – πάντα ῥεῖ Dec 07 '12 at 18:06
  • What are you trying to do that you need these things? – Martin York Dec 07 '12 at 18:08
  • See my answer to you previous question, I edited it to cover this one as well. – prazuber Dec 07 '12 at 18:11
  • @LokiAstari I’m trying to make sure that I won't later change objects I didn't plan on changing. According to prazuber and others, `map` allows this. However, `vector` doesn't, which is why I'm thinking about moving to `deque` instead. – Dennis Ritchie Dec 07 '12 at 18:42
  • 1
    You have the correct thought pattern. But that is the wrong way to achieve it. The reasons for using map/vector are different (the choice should be based on access characteristics). If you don't want to change any of the members of a vector in a function that pass the vector by const reference. If you give more details about your exact problem maybe we can suggest a better alternative to achieve your exact goals while still giving you the appropriate access characteristics that are provided by vector. – Martin York Dec 07 '12 at 20:30
  • @LokiAstari The thing is that I want to guarantee *in my own code* (in my own member functions of my own class) that I don't modify the elements of a `vector` (a private member). Sure, const references help to avoid outsiders to modify it, but they don't help myself, in my own class. Any ideas on how to make vector elements non-modifiable, even by myself? – Dennis Ritchie Dec 08 '12 at 10:36
  • @LokiAstari I created a new question dedicated to this topic: http://stackoverflow.com/q/13777773/1859852 – Dennis Ritchie Dec 08 '12 at 13:48

2 Answers2

4

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

You'll need to be able to insert copies for the items stored in STL containers, that's why it's not allowed.

You may still use const references to export containers for read only access via a const_iteratorfrom a class method (getter).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190