vector<double> find double within tolerance level
c++, find, stl, vector
Solution
You cannot use plain `find` to search with a custom comparator - you need to use `find_if` instead. You have an answer for C++11 already, here's one for use with C++03:
struct dbl_cmp {
dbl_cmp(double v, double d) : val(v), delta(d) { }
inline bool operator()(const double &x) const {
return abs(x-val) < delta;
}
private:
double val, delta;
};
...
find_if(v.begin(), v.end(), dbl_cmp(10.5, 1E-8));
Problem
I have a ``` std::vector<double> v; ``` I'm looking to detect the presence of a real value in it, up to a error tolerance level of say ``` 1e-6; ``` The documentation indicates the `operator==` is used to find the presence of value in the container. How do I generate the behaviour I require on doubles ?