A nonconst vector whose elements cannot change - alternatives
<p>I have this obsession with making things <code>const</code> that really should be <code>const</code>, even when it seems a bit painful to do so.</p>

<p>In this case, I want a <code>vector&lt;int&gt;</code> into which I can add elements, but whose existing elements I cannot change. The problem is that <code>vector&lt;const int&gt;</code> is not allowed.</p>

<p>The purpose is not only to prevent the users of my classes to modify things they shouldn't modify (this can be easily done by <code>const</code> iterators in member functions). It is also, just as importantly, so <strong>I myself don't later do things in my own code that I didn't intend on doing</strong>.</p>

<p>I have found two alternatives:</p>

<ul>
<li><p>Use <code>deque&lt;const int&gt;</code> instead.</p></li>
<li><p>Create my own vector class that wraps the actual <code>vector</code>, like this (I do this for <code>int</code> here, but in reality I would use templates for generality)</p>

<pre><code>class my_vector {
private:
  std::vector&lt;int&gt; vec;    
public:
  std::vector&lt;int&gt;::const_iterator cbegin() { return vec.cbegin(); }
  std::vector&lt;int&gt;::const_iterator cend() { return vec.cend(); }
  void push_back(int i) { vec.push_back(i); }
};
</code></pre></li>
</ul>

<p>Which of these ideas (or others) would you recommend? Will the compiler turn the second alternative into code that is just as fast as using the vector directly?</p>
