【发布时间】:2014-07-12 18:28:17
【问题描述】:
我正在编写一个预测校正数值求解器。我已经编写了一个有效的循环数组来跟踪我的函数以前的值。
#include <cmath>
// Circular array
// this is fixed sized container to hold the last n update of a function
// written by Keivan Moradi 2014
template <typename T>
class carray
{
public:
carray(int s)
{
size = exp2(ceil(log2(s)));
array = new T[size];
SizeNegOne = size-1;
head = SizeNegOne;
}
void initialize(T n)
{
for (head=0; head<size; head++)
array[head]=n;
head = SizeNegOne;
}
void update(T h)
{
// bitwise modulus:
// if "size" is in power of 2:
//(head+1) & SizeNegOne = (head+1) % size
// in contrast to modulus, this method is guaranteed to get positive values
head = (head+1) & SizeNegOne;
array[head]=h;
}
T operator[](int index)
{
// bitwise modulus:
// if "size" is in power of 2:
// (head + index) & SizeNegOne = (head + index) % size
// in contrast to modulus, this method is guaranteed to get positive values
return array[(head + index) & SizeNegOne];
}
~carray()
{
delete [] array;
}
protected:
private:
T *array;
int size, SizeNegOne, head;
};
以下代码显示了该代码应该如何工作:
int main()
{
carray<float> phi(3);
phi.initialize(-64);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(6.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(7.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(8.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(9.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
phi.update(10.1);
std::cout<<phi[0]<<" "<<phi[-1]<<" "<<phi[-2]<<" "<<phi[-3]<<" "<<phi[-4]<<" "<<phi[-5]<<" "<<phi[-6]<<" "<<phi[-7]<<" "<<phi[-8]<<std::endl<<std::endl;
return 0;
}
现在我想将这个类嵌套在一个预测器类中,这样我就可以像这样使用它:
int main()
{
predictor<float> phi(4);
phi.update(10);
phi.update(11);
phi.update(12);
phi.update(13);
std::cout<<phi.predict2ndOrder()<<std::endl;
}
这段代码显示了我失败的最佳尝试:
#include <cmath>
template <typename T>
class predictor
{
public:
predictor(int s)
{
size = s;
}
void update(T a)
{
f.update(a);
}
T predict2ndOrder()
{
return f[0] + (3/2*(f[0]-f[-1])-1/2*(f[-1]-f[-2]));
}
private:
int size;
carray<T> f(size);
class carray
{
public:
carray(int s)
{
size = exp2(ceil(log2(s)));
array = new T[size];
SizeNegOne = size-1;
head = SizeNegOne;
}
~carray()
{
delete [] array;
}
void initialize(T n)
{
for (head=0; head<size; head++)
array[head]=n;
head = SizeNegOne;
}
void update(T h)
{
head = (head+1) & SizeNegOne;
array[head]=h;
}
T operator[](int index)
{
return array[(head + index) & SizeNegOne];
}
private:
T *array;
int size, SizeNegOne, head;
};
};
请告诉我如何解决这个问题?我是一个新的 C++ 程序员,所以请放轻松。 ;)
【问题讨论】:
-
我注意到你没有关注Rule of Three。
-
Rule of Zero 相同
-
所以使用
std::vector。 -
我确实理解了三法则,我可以根据这些规则修复我的代码。但是,我不明白你对零规则的解释。据我所知,std::vector 不是一个循环数组。
标签: c++ class templates constructor nested