帮助矢量 [英] Help with vectors

查看:53
本文介绍了帮助矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在使用向量还有Borland C ++编译器。我

写了以下代码。


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

#include< stdio。 h>

#include< iostream.h>

#include< vector>

#include< algorithm>

#include< numeric>


class Moving_Average {

private:// Moving_Average类将包含矢量预测

vector< double>预测;


public:


void Moving_Average(int size)

{

forecast.reserve(size); //我指定了向量的大小

这里

}


void calculate(double * da,int time){

/ *此函数将计算移动平均线(

预测的一种方法)

然后将值存储在向量中

* /

double sum = 0;

for(int i = 1; i< = time; i ++){

sum + = *(da + i);

}

cout<<""第一个tge的总和<<< sum<< endl;

forecast.pushback(sum / time);


for(int i = time + 1; i< = size; i ++){

forecast.pushback(预测[时间+ i-1] +((*(da +时间+ i-1) - *(da + i-1))/时间));

}

for(int i = 1; i< = forecast.size(); i ++){

cout<<"""&time; << I<<" - >

"<< forecast [i]<< endl;

}

}

};

void main()

{

Moving_Average MA;

cout<< 输入历史数据的no.of期间<< endl;

cin>>期间;

MA(期间);

向量< double>数据(句点); //这个向量保存数据


for(int i = 1; i< = periods; i ++){

cout<<""输入数据期间 - > "<< I<<" " ;;

cin>> data [i];

}

MA.calculate(data.begin(),2);


}


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


我正在追踪错误。


1)VECTOR.h(984,2)无法创建预编译的头:头中的代码

2)tria.cpp(9,14):预期的类型名称

3)tria.cpp(9,14):宣言缺失;


等等..


请邮寄我有什么问题我的代码。


提前致谢..


Chandrashekar

解决方案

Chandrashekar写道:

大家好,

我正在使用向量还有Borland C ++编译器。我写了以下代码。

/ ******************************* ******************* ********* /
#include< stdio.h>
#include< iostream.h> ;


< iostream.h>是一个非标准的标题。使用< iostream>相反,和

< stdio.h>已弃用,应替换为< cstdio>。但它无论如何你都不会使用该标题中的任何东西。

#include< vector>
#include< algorithm>
#include< numeric>

类Moving_Average {
private:// Moving_Average类将包含向量预测

向量< double>预测;


vector在命名空间std中。所以写:


std :: vector< double>预测;

public:

void Moving_Average(int size)
{
forecast.reserve(size); //我指的是矢量的大小
这里


void calculate(double * da,int time){
/ *这个函数会计算移动平均值(
预测的一种方法)
然后将值存储在矢量中
* /
double sum = 0;
for(int i = 1; i< ; =时间; i ++){
sum + = *(da + i);


这不是一个错误,但如果你写的话它看起来会更清晰:


sum + = da [i];


我想知道:da真的指向时间+ 1值,为什么你不使用第一个值来获得
?请记住,索引从0开始。

}
cout<<<""第一个tge的总和<<< sum<< endl;


cout和endl也在命名空间std。

forecast.pushback(sum / time);

for(int i =时间+ 1; i< =尺寸; i ++){


大小定义在哪里?


forecast.pushback(预测[时间+ I-1] +((*(DA +时间+ I-1) - *(DA + I-1))/时间));对于(int i = 1; i< = forecast.size(); i ++){
cout<<<"""<<<<<<<<<<<<<<<<<<< - >
"<< forecast [i]<< endl;
}
}
};

void main ()


main()必须在C ++中返回int。有些编译器让你逃脱它,

但它仍然是一个错误。

{
Moving_Average MA;


这里使用默认构造函数创建一个Moving_Average对象。

不幸的是,那个类没有默认的构造函数,所以你

不能这样做。

cout<<"输入历史数据的no.of期间"<<< endl;
cin>>期间;
MA(期间);


是否应该构建MA对象?你不能这样做。这个

将尝试调用Moving_Average类的operator()。由于

没有定义operator(),所以这不行。你不能两次构造一个对象
。要么将构造推迟到这个地方,即

删除main()的第一行并写在这里:


Moving_Average MA(句点);


或创建自己的成员函数来指定大小。无论如何,

reserve()在向量中的空间只是一个优化。它不是强制性的b $ b。如果你不这样做,push_back()将自动分配所需的空间。

vector< double> data(句点); //此向量保存数据

(int i = 1; i< = periods; i ++){
cout<<"输入句点数据 - > "<< I<<" " ;;
cin>> data [i];


在这里,您再次遇到索引问题(以及其他各个地方)。

您不能在向量的第一个元素中放置任何内容你写了

结束一个元素。

}
MA.calculate(data.begin(),2);


为什么2?难道不是时期吗?此外,你不能在这里指定data.begin()

。此函数返回一个迭代器到

向量的第一个元素。虽然向量迭代器可以是一个指针,但它不是必需的,

和一些编译器(如gcc)有一个自己的迭代器类。使用& data [0]

代替。

}

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

我正在追踪错误。
1)VECTOR.h(984,2)无法创建预编译的头:头中的代码
2)tria.cpp(9,14):预期的类型名称
3)tria.cpp(9 ,14):声明丢失;

等等..


您应该列出所有这些并标记编译器的行

指的是。这样可以更容易地找到错误。

请邮寄我的代码有什么问题。




否。这不是只写新闻组。发布在这里 - 请阅读此处。




" Chandrashekar" < CH ****** @ bhelhyd.co.in>在消息中写道

新闻:94 ************************* @ posting.google.co m ... < blockquote class =post_quotes>大家好,

我正在使用向量还有Borland C ++编译器。我写了以下代码。

/ ******************************* ******************* ********* /
#include< stdio.h>
#include< iostream.h> ;
#include< vector>
#include< algorithm>
#include< numeric>

class Moving_Average {
private:// Moving_Average类将包含向量预测

向量< double>预测;

公开:

void Moving_Average(int size)
{
forecast.reserve(size); //我在这里指定矢量的大小
这里




不,你不是,你为矢量保留记忆,它的大小仍然是

零。


如果你想调整矢量大小,请使用调整大小。


forecast.resize(size); //我指定了矢量的大小


john


John Harrison写道:


Chandrashekar < CH ****** @ bhelhyd.co.in>在消息中写道
新闻:94 ************************* @ posting.google.co m ...

大家好,

我正在使用向量还有Borland C ++编译器。我写了以下代码。

/ ******************************* ******************* ********* /
#include< stdio.h>
#include< iostream.h> ;
#include< vector>
#include< algorithm>
#include< numeric>

class Moving_Average {
private:// Moving_Average类将包含向量


向量< double>预测;

公开:

void Moving_Average(int size)
{
forecast.reserve(size); //我指的是矢量的大小
这里
不,你不是,你为矢量保留记忆,它的大小仍然是零。




这就是OP的意思,我会说。

如果你想调整矢量大小,请使用调整大小。
forecast.resize(size); //我指定了向量的大小




他后来使用push_back将值添加到向量中,其中

使用调整大小而不是保留的情况是致命的。


Hi all,

I am using "vectors" and also Borland C++ compiler for fist time. I
wrote the following code.

/************************************************** *********/
#include<stdio.h>
#include<iostream.h>
#include <vector>
#include <algorithm>
#include <numeric>

class Moving_Average{
private: // Moving_Average class will contain the vector forecast

vector<double> forecast;

public:

void Moving_Average(int size)
{
forecast.reserve(size); //I am specifying the size of vector
here
}

void calculate(double *da, int time){
/* This function will calculate the moving averages (one method of
forecasting)
and then store the values in the vector
*/
double sum=0;
for(int i=1;i<=time;i++) {
sum+=*(da+i);
}
cout<<"sum of the first tge "<<sum<<endl;
forecast.pushback(sum/time);

for(int i=time+1;i<=size;i++) {
forecast.pushback(forecast[time+i-1]+((*(da+time+i-1)-*(da+i-1))/time));
}
for(int i=1;i<=forecast.size();i++) {
cout<<"moving average for period "<<i<<" -->
"<<forecast[i]<<endl;
}
}
};
void main()
{
Moving_Average MA;
cout<<"Enter the no.of periods of historic data"<<endl;
cin>>periods;
MA(periods);
vector<double>data(periods); // This vector holds the data

for(int i=1;i<=periods;i++) {
cout<<"Enter data for period --> "<<i<<" ";
cin>>data[i];
}
MA.calculate(data.begin(),2);

}

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

I am getting following erros.

1)VECTOR.h(984,2) Cannot create pre-compiled header:code in header
2)tria.cpp(9,14):Type name expected
3)tria.cpp(9,14):Declaration missing;

and so on..

Pls mail me what is wrong with my code.

Thanks in advance..

Chandrashekar

解决方案

Chandrashekar wrote:

Hi all,

I am using "vectors" and also Borland C++ compiler for fist time. I
wrote the following code.

/************************************************** *********/
#include<stdio.h>
#include<iostream.h>
<iostream.h> is a non-standard header. Use <iostream> instead, and
<stdio.h> is deprecated and should be replaced by <cstdio>. But it
doesn''t seem that you use anything from that header anyway.
#include <vector>
#include <algorithm>
#include <numeric>

class Moving_Average{
private: // Moving_Average class will contain the vector forecast

vector<double> forecast;
vector is in namespace std. So write:

std::vector<double> forecast;
public:

void Moving_Average(int size)
{
forecast.reserve(size); //I am specifying the size of vector
here
}

void calculate(double *da, int time){
/* This function will calculate the moving averages (one method of
forecasting)
and then store the values in the vector
*/
double sum=0;
for(int i=1;i<=time;i++) {
sum+=*(da+i);
That''s not an error, but it would look cleaner if you wrote:

sum+=da[i];

I wonder: does da really point to time+1 values, and why aren''t you
using the first value? Remember, indexes start at 0.
}
cout<<"sum of the first tge "<<sum<<endl;
cout and endl are also in namespace std.
forecast.pushback(sum/time);

for(int i=time+1;i<=size;i++) {
Where is size defined?
forecast.pushback(forecast[time+i-1]+((*(da+time+i-1)-*(da+i-1))/time)); }
for(int i=1;i<=forecast.size();i++) {
cout<<"moving average for period "<<i<<" -->
"<<forecast[i]<<endl;
}
}
};
void main()
main() must return int in C++. Some compilers let you get away with it,
but it''s still an error.
{
Moving_Average MA;
Here you create a Moving_Average object using the default constructor.
Unfortunately, that class doesn''t have a default constructor, so you
can''t do that.
cout<<"Enter the no.of periods of historic data"<<endl;
cin>>periods;
MA(periods);
Is that supposed to construct the MA object? You can''t do that. This
will instead try to call operator() of the Moving_Average class. Since
there is no operator() defined, this can''t work. You cannot construct
an object twice. Either defer the construction to this place, i.e.
remove the first line of main() and write here:

Moving_Average MA(periods);

or create an own member function to specify the size. Anyway,
reserve()ing the space in a vector is only an optimization. It''s not
mandatory. If you don''t do that, push_back() will automatically
allocate the needed space.
vector<double>data(periods); // This vector holds the data

for(int i=1;i<=periods;i++) {
cout<<"Enter data for period --> "<<i<<" ";
cin>>data[i];
Here, you have the index problem again (and at various other places).
You don''t put anything in the first element of the vector and you write
one element past its end.
}
MA.calculate(data.begin(),2);
Why 2? Shouldn''t it be periods? Also, you can''t specify data.begin()
here. This function returns an iterator to the first element of the
vector. While a vector iterator can be a pointer, it''s not required to,
and some compilers (like gcc) have an own iterator class. Use &data[0]
instead.

}

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

I am getting following erros.

1)VECTOR.h(984,2) Cannot create pre-compiled header:code in header
2)tria.cpp(9,14):Type name expected
3)tria.cpp(9,14):Declaration missing;

and so on..
You should have listed all of them and marked the lines your compiler is
referring to. It makes it a lot easier to find the errors.

Pls mail me what is wrong with my code.



No. This is not a write-only newsgroup. Post here - read here.



"Chandrashekar" <ch******@bhelhyd.co.in> wrote in message
news:94*************************@posting.google.co m...

Hi all,

I am using "vectors" and also Borland C++ compiler for fist time. I
wrote the following code.

/************************************************** *********/
#include<stdio.h>
#include<iostream.h>
#include <vector>
#include <algorithm>
#include <numeric>

class Moving_Average{
private: // Moving_Average class will contain the vector forecast

vector<double> forecast;

public:

void Moving_Average(int size)
{
forecast.reserve(size); //I am specifying the size of vector
here



No you aren''t, you are reserving memory for the vector, its size is still
zero.

If you want to resize a vector use resize.

forecast.resize(size); //I am specifying the size of vector

john


John Harrison wrote:


"Chandrashekar" <ch******@bhelhyd.co.in> wrote in message
news:94*************************@posting.google.co m...

Hi all,

I am using "vectors" and also Borland C++ compiler for fist time. I
wrote the following code.

/************************************************** *********/
#include<stdio.h>
#include<iostream.h>
#include <vector>
#include <algorithm>
#include <numeric>

class Moving_Average{
private: // Moving_Average class will contain the vector
forecast

vector<double> forecast;

public:

void Moving_Average(int size)
{
forecast.reserve(size); //I am specifying the size of vector
here
No you aren''t, you are reserving memory for the vector, its size is
still zero.



That''s actually what the OP meant, I''d say.

If you want to resize a vector use resize.

forecast.resize(size); //I am specifying the size of vector



He''s later using push_back to add the values to the vector, in which
case using resize instead of reserve is fatal.


这篇关于帮助矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆