Pushback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chandrashekar

    Pushback

    hi,

    I am facing problem with pushback() method in Vectors. I am getting
    compilation errors when I am using pushabck() in the following
    program. I am pasting the code here

    /*************** *************** **********/

    #include<iostre am>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    using namespace std;

    class Moving_Average{
    public:
    std::vector<dou ble> forecast;
    // vector<double>f orecast;

    public:
    Moving_Average( int size) {
    cout<<" IN size "<<endl;
    forecast.resize (size);
    cout<<" Out sized "<<endl;
    }

    void calculate(doubl e *da,int time) {

    /* This function is merely copying the elements of vector "data" into
    vector "forecast"
    */
    for(int i=0;i<time;i++) {
    forecast.pushba ck(*(da+i)); // I am facing problem here
    // ^^^^^^^^
    }
    }
    void prasint(int time) {
    for(int i=0;i<time;i++) {
    cout<<"asd "<<forecast[i]<<endl;
    }
    }
    };


    int main()
    {
    int periods;
    cout<<"Enter the no.of periods of historic data"<<endl;
    cin>>periods;
    vector<double>d ata(periods);
    for(int i=0;i<periods;i ++) {
    cout<<"Enter data for period --> "<<i<<" ";
    cin>>data[i];
    }
    Moving_Average MA(periods);
    MA.calculate(da ta.begin(),peri ods);
    MA.prasint(peri ods);
    }

    /*************** *************** *************** **************/
    When I used pish_back() in place of pushback(), i didnt get any
    compilation errors.But, I am not getting proper output. In the above
    program I am copying
    the elements of vector 'data' (i.e da) to vector forecast.Here is the
    sample out put
    /*************** ***************/

    Enter the no.of periods of historic data
    3
    Enter data for period --> 0 1
    Enter data for period --> 1 2
    Enter data for period --> 2 3

    IN calc value is -->1
    Out of cal value is -->2
    Out of cal value is -->3
    Out of cal
    in print
    asd 1.88323e-307
    asd 1.88323e-307
    asd 1.88323e-307

    /*************** *************** *************** ***/

    Can somebody tell me what is the difference between pushback() and
    push_back()? Is there any better method to store values in vectors
    other than this?

    Thanks,

    Chandrashekar
  • Victor Bazarov

    #2
    Re: Pushback

    "Chandrashe kar" <chandrak@bhelh yd.co.in> wrote...[color=blue]
    > I am facing problem with pushback() method in Vectors. I am getting
    > compilation errors when I am using pushabck() in the following
    > program. I am pasting the code here[/color]

    There is not member function 'pushback' in 'vector'. There
    is, however, the member function 'push_back'. RTFM.

    And if you're not getting "correct output", you have to post
    your code and explain what output you expect as "correct"
    and why.

    Victor


    Comment

    • Chandrashekar

      #3
      Re: Pushback

      "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:<6MoQa.552 29$N7.7349@sccr nsc03>...[color=blue]
      > "Chandrashe kar" <chandrak@bhelh yd.co.in> wrote...[color=green]
      > > I am facing problem with pushback() method in Vectors. I am getting
      > > compilation errors when I am using pushabck() in the following
      > > program. I am pasting the code here[/color]
      >
      > There is not member function 'pushback' in 'vector'. There
      > is, however, the member function 'push_back'. RTFM.
      >
      > And if you're not getting "correct output", you have to post
      > your code and explain what output you expect as "correct"
      > and why.
      >
      > Victor[/color]


      Thanks for your reply.
      My code is here
      /*************** *************** **********/

      #include<iostre am>
      #include <vector>
      #include <algorithm>
      #include <numeric>
      using namespace std;

      class Moving_Average{
      public:
      std::vector<dou ble> forecast;
      // vector<double>f orecast;

      public:
      Moving_Average( int size) {
      cout<<" IN size "<<endl;
      forecast.resize (size);
      cout<<" Out sized "<<endl;
      }

      void calculate(doubl e *da,int time) {

      /* This function is merely copying the elements of vector "data" into
      vector "forecast"
      */
      for(int i=0;i<time;i++) {
      forecast.push_b ack(*(da+i)); // I am copying the elements of da
      to forecast
      }
      }
      void prasint(int time) {
      for(int i=0;i<time;i++) {
      cout<<"asd "<<forecast[i]<<endl;
      }
      }
      };


      int main()
      {
      int periods;
      cout<<"Enter the no.of periods of historic data"<<endl;
      cin>>periods;
      vector<double>d ata(periods);
      for(int i=0;i<periods;i ++) {
      cout<<"Enter data for period --> "<<i<<" ";
      cin>>data[i];
      }
      Moving_Average MA(periods);
      MA.calculate(da ta.begin(),peri ods);
      MA.prasint(peri ods);
      }

      /*************** *************** *************** **************/

      I am getting following output
      /*************** ***************/

      Enter the no.of periods of historic data
      3
      Enter data for period --> 0 1
      Enter data for period --> 1 2
      Enter data for period --> 2 3

      IN calc value is -->1
      Out of cal value is -->2
      Out of cal value is -->3
      Out of cal
      in print
      asd 1.88323e-307
      asd 1.88323e-307
      asd 1.88323e-307

      /*************** *************** *************** ***/
      And I expect following output since I am copying the elements of one
      vector to the other. (I know that there are some other ways to do it,
      but I want it this way)

      IN calc value is -->1
      Out of cal value is -->2
      Out of cal value is -->3
      Out of cal
      in print
      asd 1
      asd 2
      asd 3
      /*************** **********/

      Chandrashekar

      Comment

      • Ron Natalie

        #4
        Re: Pushback


        "Chandrashe kar" <chandrak@bhelh yd.co.in> wrote in message news:94ac6a9.03 07140017.39bef4 b3@posting.goog le.com...
        [color=blue]
        > forecast.resize (size);[/color]

        push_back resizes the array to hold the additional object. You don't need to do it both.
        I think you'll find your vector forecast ends up being 6 long.
        [color=blue]
        > forecast.push_b ack(*(da+i)); // I am copying the elements of da
        > to forecast[/color]

        You can either do forcast[i] = da[i] here or avoid resizing the array.


        Comment

        • Victor Bazarov

          #5
          Re: Pushback

          "Chandrashe kar" <chandrak@bhelh yd.co.in> wrote...[color=blue]
          > "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message[/color]
          news:<6MoQa.552 29$N7.7349@sccr nsc03>...[color=blue][color=green]
          > > "Chandrashe kar" <chandrak@bhelh yd.co.in> wrote...[color=darkred]
          > > > I am facing problem with pushback() method in Vectors. I am getting[/color][/color]
          > [...]
          > class Moving_Average{
          > public:
          > std::vector<dou ble> forecast;
          > // vector<double>f orecast;
          >
          > public:
          > Moving_Average( int size) {
          > cout<<" IN size "<<endl;
          > forecast.resize (size);[/color]

          I think John already told you to use 'reserve' here.
          [color=blue]
          > cout<<" Out sized "<<endl;
          > }
          > [...][/color]

          Victor


          Comment

          Working...