标签:fill tor 指针 static 工作量 request cer 位数组 int
void prog1_static(void)
{
int pos = 9;
// elem will hold the element‘s value
vector<int> *elem;
elem = fibon_seq2(pos);
cout << elem->size() << endl;
for (int in = 0; in < elem->size(); in++)
{
cout << elem[0][in] <<" ";
}
cout << endl;
pos = 18;
elem = fibon_seq2(pos);
cout << elem->size() << endl;
for (int in = 0; in < elem->size(); in++)
{
cout << elem[0][in] <<" "; //error
}
cout << endl;
}
下面的函数返回elems的地址。可以返回静态对象的地址
通过去读取vector的返回值可以实现减少计算工作量的方法。
cout << elem[0][in] <<" ";
这个用法一定要注意,试了很就才发现。
elem是一个向量指针,这个指针指向某一个向量的,
因此类似于c语言中一个指针指向二位数组。
vector<int> *fibon_seq2( int size )
{
const int max_size = 1024;
static vector< int > elems;
if ( size <= 0 || size > max_size )
{
cerr << "fibon_seq(): oops: invalid size: "
<< size << " -- can锟絫 fulfill request.\n";
return 0;
}
// if size is equal to or greater than elems.size(),
// no calculations are necessary ...
for ( int ix = elems.size(); ix < size; ++ix )
{
if ( ix == 0 || ix == 1 )
{
elems.push_back( 1 );
}
else
{
elems.push_back( elems[ix - 1] + elems[ix - 2] );
}
}
return &elems;
}
标签:fill tor 指针 static 工作量 request cer 位数组 int
原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/11221428.html