Pushback

C

Chandrashekar

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<iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

class Moving_Average{
public:
std::vector<double> forecast;
// vector<double>forecast;

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

void calculate(double *da,int time) {

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


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

/***********************************************************/
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
 
V

Victor Bazarov

Chandrashekar said:
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

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
 
C

Chandrashekar

Victor Bazarov said:
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


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

#include<iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

class Moving_Average{
public:
std::vector<double> forecast;
// vector<double>forecast;

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

void calculate(double *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_back(*(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<<endl;
}
}
};


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

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

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
 
R

Ron Natalie

Chandrashekar said:
forecast.resize(size);

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.
forecast.push_back(*(da+i)); // I am copying the elements of da
to forecast

You can either do forcast = da here or avoid resizing the array.
 
V

Victor Bazarov

Chandrashekar said:
"Victor Bazarov" <[email protected]> wrote in message [...]
class Moving_Average{
public:
std::vector<double> forecast;
// vector<double>forecast;

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

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

Victor
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top