## jshoot -1 Junior Poster in Training

Hi everyone,

My question is:

I have two vectors, for example vectorA and vectorB, vectorA is filled with randomics values, like 23, 43, 54, etc, and its have 1000 members, like vectorA, vectorA, etc.
My vectorB descending of vectorA, I use the "push_back" function to take the values of vectorA, but I specificate wich ones values I want, like this:

``````std::vector< double > vectorA ;
std::vector< double > vectorB;
double c2 = 100;
while( c2 < 500){
vectorB.push_back(vectorA[c2]);
}``````

But when generate the vectorB, its have 1000 members, the vectorB is equal to the vectorA(what I wanna in this case), and the vectorB[501 ~ 1000] is equal 0.

Can anyone help me?

Thanks!

## jonsca 1,059

Where do you modify c2 in the loop? Also, why is it a double?

## jshoot -1

oh, i forget the incrementor of c2, but in my code its exist, and its a double because in my code, i dont use int values, because i manage a lot of data.

## Lerner 582

I don't believe C++ allows type double to be the index of an array/vector, so I'm surprised that the posted code would compile.

If c2 were of type int and you were to increment it's value by 1 starting at 100 and stopping at 499, then the code as posted should put items with indexes of 100 to 499 in vectorA into vectorB at indexes 0 to 399.

The STL library, which is where the vector class is located, has to meet certain specifications, but the exact implementation is left to the developer(s). It sounds like your version of the vector class may default the capacity (how many doubles could be held in the vector before more memory is needed) of a vector to 1000 (at least when it's handling doubles)and automatically assigns each element to a default value of zero (instead of leaving them unassigned). One of the things a vector class must do is keep track of the number of elements actually in the vector. That value is retrievable by calling the size() method. The only time the size and capacity are equal is when the vector is "full".

You can check out whether this is indeede the case with your version of the vector class by declaring a vector of doubles and calling the size() and capacity() and printing out say 10 elements of the "empty()" vector.

To ignore the default values beyond the "size" of the vector, only print out the elements that have indexes less than size().

## jonsca 1,059

in my code, i dont use int values, because i manage a lot of data.

That doesn't make a lick of sense. Sorry to be so blunt, but I don't understand that at all.

Give Lerner's suggestions a try and see what happens.