C ++ vectors are not like arrays

Iโ€™ve been going to C ++ for several months now, google has directed the stack overflow most of the time for C ++ requests. I often noted exhortations like โ€œWhy don't you use a vector,โ€ and was inspired to do just that.

So, first of all, to get the small advantages of automatically freeing memory, and the ability to write typed comparison functions for sorting. I switched an array of pointers to objects in a vector. Now I thought (incorrectly) that vectors could be used by more or less similar arrays, and therefore I was initialized this way:

cluster clusters[LOTS]; vector<cluster *> pclust; pclust.reserve(numClust); for (int i=0; i<numClust; ++i) pclust[i] = clusters + i; 

No complaints from the compiler. Then after some time I need to sort the vector by some attribute of the cluster object. So:

 std::sort(pclust.begin(), pclust.end(), ClusterCompareNumSegs); 

Again there is no compilation of problems. Except that the vector is not sortable. It turns out that vector.size () is zero, and of course my initialization should have been

 pclust.push_back(clusters + i); 

Now this is easy to fix, but I'm confused because the initial assignment was incorrect. I successfully went through the vector - using array syntax, for example:

 for (clustind=0; clustind < numClust; ++clustind) {<br> cluster *cl = pclust[clustind]; ...happily access *cl... 

And everything turned out fine. So Iโ€™m just wondering whatโ€™s going on. Presumably, in my original assignments, I tried to access the elements were not yet in the vector (I tried to insert them), and the vector throws exceptions, which I ignored. But, nevertheless, when indicating the locations, the signs were there. Can anyone give enlightenment?

+7
source share
4 answers

vector::reserve does not resize your vector, it still contains only the 0 elements with which it was created. What it does is make sure that the vector can contain numClust without redistributing it. See here .

Do you want to either declare a vector that size

 vector<cluster *> pclust(numClust); 

or resize vector

 pclust.resize(numClust); 
+11
source

std::vector::reserve asks that the capacity of the allocated space for storing the elements of the vector container be at least sufficient to store n elements. It does not resize the vector, which makes std::vector::resize .

Replace pclust.reserve(numClust); on pclust.resize(numClust); .

Alternatively, you can remove the call to pclust.reserve(numClust); and change the construction of this vector to: vector<cluster *> pclust(numClust); which gives the same result.

I also suggest you take a look at this question: std :: vector reserve () and push_back () are faster than resize () and array index, why? :)

+5
source

operator [] used with returning a vector returns a reference to an element at the index position. BUT, you did not initialize the vector with any values, so it was empty.

Instead of making pclust.reserve(numClust) , but it only reports that the size of the vector will change soon, and it allocates storage space without resizing the vector.

0
source
 cluster clusters[LOTS]; vector<cluster *> pclust(numClust); for (int i = 0; i < numClust; ++i) pclust[i] = clusters + i; 

But that means you are still using an array to store clusters. Can't you make clusters vector?

 vector<cluster> clusters(LOTS); 
0
source

All Articles