Here's another clunky experiment that don't work right:
It doesn't work because within fibvec.digit values for "series" are not kept -- everything is always 0. How can I render this "static" or something? Eg, in perl, a constructor returns a special reference to a hash that contains any number and type of elements (it's special in the sense that the class methods can then be used on it). But I cannot "return" anything with a C++ constructor -- do I have to make fibvec derived from vector (I would assume not, since apparently deriving things from the standard containers is a bad practice).Code:#include <iostream>
#include <vector>
using namespace std;
class fibvec {
vector<int> series;
unsigned int len;
public:
fibvec() {
series.reserve(2);
series[0] = series[1] = 1;
len = 2;
}
int digit (int pos) {
int i, j = 0;
if (pos >= len) {
int nlen = pos+1;
series.reserve(nlen);
for (i=len;i<nlen;i++)
series[i] = series[i-1] + series[i-2];
len = nlen;
}
return series[pos];
}
};
int main() {
int i;
fibvec eg;
for (i=5;i<10;i++) cout << eg.digit(i) << '\n';
return 0;
}