# Quantlib interpolation question

I am trying to use QuantLib to create some curves, but I am finding this error I do not really know how to get around. Say for simplicity this is the example:

#include<iostream>
#include <ql/quantlib.hpp>
using namespace QuantLib;
LinearInterpolation e() {
std::vector<Time> x = { 0,1 };
std::vector<Real> y = { 0,2 };
return LinearInterpolation(x.begin(), x.end(), y.begin());
}

int main()
{
LinearInterpolation c = e();
std::cout << c(0.5) << std::endl;
system("pause");
return 0;
}


But it gives me an error: "Debug assertion failed. Vector iterator + offset out of range."

I can get around it if I change the code to:

Real e(Real t) {
std::vector<Time> x = { 0,1 };
std::vector<Real> y = { 0,2 };
LinearInterpolation c(x.begin(), x.end(), y.begin());
return c(t);
}

int main()
{
std::cout << e(0.5) << std::endl;
system("pause");
return 0;
}


but I would like to use the function multiple times, so that would perform the interpolation multiple times (and hence not so efficient). I was wondering if there is a way to return a Real operator I can use throughout only doing the interpolation once.

The LinearInterpolation class doesn't copy the x and y ranges. You have to make sure that the vectors stay alive as long as you're using them. Instead of a function, you can code a small function object that stores the vectors.