Reputation: 1559
I am trying to find the index, i
, of an element of a vector, v
, that satisfies: v[i] <= x < v[i + 1]
, where x
is a given arbitrary value. I am trying to use the find_if
function, but it appears that find_if
passes the value from the iterator and not the iterator, because of this I cannot figure out a way to perform the x < v[i + 1]
comparison. Is there a way to do this comparison with a unary predicate set up the way that I have it below:
#include <vector>
#include <iostream>
#include <algorithm>
//Create predicate for find_if
template<typename T>
struct eq {
eq(const T _x) : x(x) { };
//Does not work
bool operator()(typedef std::vector<T>::iterator it) const { //
return *it <= x && x < *(++it);
}
private:
T x;
};
//Make vector
std::vector<double> vDouble;
vDouble.push_back(1.5);
vDouble.push_back(3.1);
vDouble.push_back(12.88);
vDouble.push_back(32.4);
double elemVal = *std::find_if(vNumeric.begin(), vNumeric.end(), eq<double>(13.0));
Upvotes: 1
Views: 1117