鉴于:
typedef .../*some type*/ SomeValue;
SomeValue someFunction(int arg){
return /*some calculation involving arg that produces SomeValue*/
}
int firstCandidate = 0, lastCandidate = 101;
SomeValue desiredValue = SomeValue();
我想找到使用二进制搜索( )int
产生desiredValue
(当传递给)的参数。
,是要赋予 的参数。对于搜索,候选人应调用并将结果与 . 因为是真的。someFunction
std::lower_bound
firstCandidate
lastCandidate
someFunction
std::lower_bound
someFunction(currentArgument)
desiredValue
SomeValue
someFunction(x) < someFunction(x + 1)
即它应该产生与此相同的结果:
int findArgLowerbound(int first, int last, SomeValue refVal){
for (int i = first; i < last; i++){
if (someFunction(i) >= refVal)
return i;
}
return last;
}
仅使用标准函数+二分查找算法。
在有和没有提升的情况下
,我怎样才能轻松地做到这一点(无需编写我自己的二进制搜索函数)?不是迭代器,在这种情况下int
我还没有弄清楚如何制作。boost::make_transform_iterator
限制:
- c++03标准。
- boost 没问题,但我真的更喜欢没有它的解决方案。
- 编辑 -
我想知道如何使用内置函数或已经可用的函数(std::lower_bound 和类似函数)来做我想做的事。我可以编写专门的二进制搜索函数,但我认为这不是“正确”的方法。