c++ vectors not like arrays
<p>I've been getting my head round c++ for a few months, and have
been directed by google to stack overflow most of the time
for c++ queries. I noted frequently exhortations of the type
"why don't you use a vector", and was inspired to do just that.</p>

<p>So, primarily to get the minor benefits of automatic memory deallocation,
and being able to write typed comparison functions for sorting.
I switched an array of pointers to objects to being a vector.
Now I thought (incorrectly it seems) that vectors could be used 
more or less like arrays, and hence I initialized thusly:</p>

<pre><code>cluster  clusters[LOTS];
vector&lt;cluster *&gt; pclust;
pclust.reserve(numClust);
for (int i=0; i&lt;numClust; ++i)
    pclust[i] = clusters + i;
</code></pre>

<p>No complaints from the compiler.
Then some time later I need to sort the vector on some
attribute of the cluster object. So:</p>

<pre><code>std::sort(pclust.begin(), pclust.end(), ClusterCompareNumSegs);
</code></pre>

<p>Again no problems compiling. Except the vector doesn't
get sorted. It turns out that vector.size() is zero,
and of course my initialization should have been </p>

<pre><code>pclust.push_back(clusters + i);
</code></pre>

<p>Now that's easy to fix, but I am confused, because the initial
incorrect assignment was working. I successfully iterated through
the vector - using the array syntax, like so:</p>

<pre><code>for (clustind=0; clustind &lt; numClust; ++clustind) {&lt;br&gt;
    cluster *cl = pclust[clustind];
    ...happily access *cl...
</code></pre>

<p>And it all worked fine. So I'm just wondering what's going on.
Presumably in my initial assignments, I was trying to access
elements not yet in the vector (I was trying to put them in),
and the vector was throwing exceptions that I was ignoring.
But nonetheless, when referencing the locations, the pointers
were there. Can anyone provide enlightenment?</p>
