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[0], vectorA[1], 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[0] is equal to the vectorA[100](what I wanna in this case), and the vectorB[501 ~ 1000] is equal 0.

Can anyone help me?

Thanks!

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

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.

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().

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.

Hi, thanks for reply.

Yes, the code compile, i think that you have talk about is this warnig "[Warning] passing `double' for converting 1 of `typename _Alloc::reference std::vector<_Tp, _Alloc>::operator[](size_t) [with _Tp = double, _Alloc = std::allocator<double>]'".

My code, is doing exactly what you talking about, mak de vectorA[100] and put int vectorB[0], and after all they sets to 0 all others vectorB[449 + x], i tryed to erase,clear,empty this anothers values, but i didn't make.

So now, i just ignore this others values, because for me the important is the text file that I output from the program, and in this text file, i limitate the rows to the last value of vectorB, in this case vectorB[499].

Thanks for the help, I see you are a great programmer, and i wanna to be like this one day. My code have about 5000 lines + wxwidgets code(i make a gui interface), but i still feel like a dummie.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.