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
参数使用二进制搜索(std::lower_bound
)产生desiredValue
(传递给someFunction
时)。 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;
}
只使用标准函数+二分查找算法。
我该怎么做EASILY(无需编写我自己的二进制搜索功能)有无升压? int
不是迭代器,我还没有想出在这种情况下如何制作boost::make_transform_iterator
。
限制:
- C++ 03标准。
- 提升是好的,但我真的喜欢解决离不开它。
- 编辑 -
我想知道我怎么可以使用内置或已可用功能(标准:: LOWER_BOUND和类似)做我想做的。我可能写专门的二进制搜索功能,但我不认为这将是“正确”的方式来做到这一点。
http://code-generator.stackexchange.com –
@LightnessRacesinOrbit:已经解决了它自己。 – SigTerm