I want to write a template function to interpolate missing values within a std::map like this :
template<typename T1, typename T2>
T2 interpolate(const std::map<T1, T2> &data, const T1 at_x);
When I write the function with real type for the map, like std::map <unsigned short, double>
, all is fine.
But when I want to template the function, after I create an iterator for the map, I cannot access the ->first and ->second membres anymore from the iterator ? why ?
Here's the code of my templated function:
template<typename T1, typename T2>
T2 interpolate(const std::map<T1, T2> &data, const T1 at_x){
typedef std::map<T1, T2>::const_iterator i_t;
i_t right = data.upper_bound(at_x);
i_t left;
double delta;
data.begin()->first; // this is ok for the compiler and the auto-completion of eclipse
right->first; // this is rejected: "Field 'first' could not be resolved"
// after range of keys in map:
if (right == data.end()) {
--right;
left = right;
--left;
delta = ((double)at_x - left->first) / (right->first - left->first);
return right->second + (delta - 1.0) * (right->second - left->second);
}
// before range of keys in map:
if (right == data.begin()) {
left = right;
++left;
delta = ((double)at_x - left->first) / (right->first - left->first);
return right->second + (delta - 1.0) * (right->second - left->second);
}
// within range of keys in map:
left = right;
--left;
delta = ((double)at_x - left->first) / (right->first - left->first);
return delta * right->second + (1.0 - delta) * left->second;
}
Many thanks for any help.
Samuel