我试图找到满足:i
的向量元素的索引,其中是给定的任意值。我正在尝试使用该函数,但似乎从迭代器而不是迭代器传递值,因此我无法找到执行比较的方法。有没有办法与一元谓词进行比较,如下所示: v
v[i] <= x < v[i + 1]
x
find_if
find_if
x < v[i + 1]
#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));