Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #15217 > unrolled thread

Strange STL vector behavior -- request help

Started bydakupoto@gmail.com
First post2012-04-04 00:31 -0700
Last post2012-04-04 22:38 +0200
Articles 2 — 2 participants

Back to article view | Back to comp.lang.c++


Contents

  Strange STL vector behavior -- request help dakupoto@gmail.com - 2012-04-04 00:31 -0700
    Re: Strange STL vector behavior -- request help "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-04-04 22:38 +0200

#15217 — Strange STL vector behavior -- request help

Fromdakupoto@gmail.com
Date2012-04-04 00:31 -0700
SubjectStrange STL vector behavior -- request help
Message-ID<3258921.1909.1333524687267.JavaMail.geo-discussion-forums@pbcwe9>
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[i]<<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.

[toc] | [next] | [standalone]


#15221

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2012-04-04 22:38 +0200
Message-ID<jlibgu$mra$1@dont-email.me>
In reply to#15217
On 04.04.2012 09:31, dakupoto@gmail.com wrote:
> 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[i]<<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 '[i]' (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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c++


csiph-web