Strange STL vector behavior -- request help

D

dakupoto

Could some C++ guru please provide some hints to this
odd C++ STL vector issue ?
I have in a class:

private:
std::vector<double> invect;
std::vector<double> invect_copy;
std::vector<double> filtvect;
'invect' gets filled correctly with 64 distinct double
values, as checked with a simple print statement.

Then I have:
invect_copy = invect;
invect.clear();
/*for(i = 0; i < MAXELEM; i++) std::cout<<invect_copy<<std::endl; */
Here again, the print statement correctly shows the elements added
to 'invect_copy'

Now I wish to read out chunks of data from 'invect_copy' as:
if(invect_copy.size() > 0)
{
while(clim + MAXFILTELEM < MAXELEM)
{
for(j = 0; j < MAXFILTELEM; j++)
{
cind = j + clim;
tmp = invect_copy.at(cind);
std::cout<<name()<<" "<<j<<" "<<cind<<" "<<clim<<" "<<tmp<<std::endl;
filtvect.push_back(tmp);
}

This time however, I get the same value being for all values
of the index 'cind' as:
mavg 0 0 0 1.80429e+09
mavg 1 1 0 1.80429e+09
mavg 2 2 0 1.80429e+09
mavg 3 3 0 1.80429e+09
mavg 4 4 0 1.80429e+09
mavg 5 5 0 1.80429e+09
mavg 6 6 0 1.80429e+09
mavg 7 7 0 1.80429e+09
mavg 8 8 0 1.80429e+09
mavg 9 9 0 1.80429e+09
mavg 0 1 1 1.80429e+09
mavg 1 2 1 1.80429e+09
mavg 2 3 1 1.80429e+09
mavg 3 4 1 1.80429e+09
mavg 4 5 1 1.80429e+09
mavg 5 6 1 1.80429e+09
mavg 6 7 1 1.80429e+09
mavg 7 8 1 1.80429e+09
mavg 8 9 1 1.80429e+09
mavg 9 10 1 1.80429e+09
mavg 0 2 2 1.80429e+09
mavg 1 3 2 1.80429e+09

and so on ...
Could someone please point out what I might be doing
wrong ? Any hints, suggestions would be gratefully
accepted.
 
A

Alf P. Steinbach

Could some C++ guru please provide some hints to this
odd C++ STL vector issue ?
I have in a class:

private:
std::vector<double> invect;
std::vector<double> invect_copy;
std::vector<double> filtvect;
'invect' gets filled correctly with 64 distinct double
values, as checked with a simple print statement.

Then I have:
invect_copy = invect;
invect.clear();

Don't clear the vector.

/*for(i = 0; i< MAXELEM; i++) std::cout<<invect_copy<<std::endl; */
Here again, the print statement correctly shows the elements added
to 'invect_copy'


Use the vector size rather than MAXELEM.

Use '.at( i )' instead of '' (because of current problems).

Generally, do not user ALL UPPERCASE except for macro names.

For C++ constants, use `const` instead of macro definitions.

[snip]


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top