473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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<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[i]<<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[i];
}
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
Jul 19 '05 #1
4 8358
"Chandrashekar" <ch******@bhelhyd.co.in> wrote...
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
Jul 19 '05 #2
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<6MoQa.55229$N7.7349@sccrnsc03>...
"Chandrashekar" <ch******@bhelhyd.co.in> wrote...
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

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[i]<<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[i];
}
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
Jul 19 '05 #3

"Chandrashekar" <ch******@bhelhyd.co.in> wrote in message news:94*************************@posting.google.co m...
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[i] = da[i] here or avoid resizing the array.
Jul 19 '05 #4
"Chandrashekar" <ch******@bhelhyd.co.in> wrote...
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<6MoQa.55229$N7.7349@sccrnsc03>...
"Chandrashekar" <ch******@bhelhyd.co.in> wrote...
I am facing problem with pushback() method in Vectors. I am getting

[...]
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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: David Morgenthaler | last post by:
How does one overide the iterator implied by the construct "for line in file:"? For example, suppose I have a file containing row,col pairs on each line, and I wish to write a subclass of file...
4
by: Jan Burgy | last post by:
Hi all y'all, Consider the class down below. I've implemented it just because I needed the pushback method. Now of course the third line in __init__ doesn't work and I even understand why. My...
7
by: Roy Smith | last post by:
In the old days, if I wanted to return a sequence of items, I'd return a list, and loop over it like this: for thing in getListOfThings (): do something With iterators, I'm doing: for...
3
by: Daniele | last post by:
Hi, My problem is the manage of a dynamic vector in a list of structures. In the .hpp file I define the struct and the vector v. typedef struct Word { char* Name; vector<int> v; Word* Next;...
11
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
6
by: Rob Thorpe | last post by:
Given the code:- r = sscanf (s, "%lf", x); What is the correct output if the string s is simply "-" ? If "-" is considered the beginning of a number, that has been cut-short then the...
4
by: john isaac | last post by:
this statement causes a compilation error: memberlist.name.push_back(newname); cannot convert std::string to char. memberlist is a vector...i is an integer that's interating in a...
27
by: Simon Biber | last post by:
The following Example 3 is given in the 1999 C standard for the function fscanf: I have tested several implementations and none of them get the last case right. In no case does fscanf return 0...
6
by: hadad.yaniv | last post by:
Hello, i am new to c++, i hav a vector of typed object: vector<Man*People; When i do a second pushback, even for the same object the program crash say: "An Access Violation (Segmentation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.