Strange STL vector behavior -- request help

Discussion in 'C++' started by dakupoto@gmail.com, Apr 4, 2012.

  1. Guest

    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.
     
    , Apr 4, 2012
    #1
    1. Advertising

  2. On 04.04.2012 09:31, 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<<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
     
    Alf P. Steinbach, Apr 4, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dennis
    Replies:
    1
    Views:
    2,586
    Dennis
    Jun 7, 2004
  2. CD
    Replies:
    2
    Views:
    806
    Victor Bazarov
    Oct 5, 2004
  3. Replies:
    2
    Views:
    418
  4. Replies:
    8
    Views:
    1,929
    Csaba
    Feb 18, 2006
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    375
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page