I am just trying to produce an efficient piece of code to obtain the n-th number of a Fibonacci sequence and am using the code below to do this. But, I get the error Expression: vector subscript out of range
and don't know why it happens.
int Fib(int n)
{
vector<int> output;
output.reserve(n);
output[0] = 1; output[1] = 2;
for(int i = 2; i <=n; ++i)
{
output.push_back(output[i-1]+ output[i-2]);
}
return output[n];
}