我有一个unordered_map向量,它根据我定义的比较器函数进行排序.我想使用二进制搜索来使用比较器函数查找其中一个值.但是,二进制搜索只返回bool,我需要结果的索引/迭代器.我能做什么?
T33*_*33C 22
#include <algorithm>
using namespace std;
//!!!!! a must be sorted using cmp. Question indicates that it is.
it = lower_bound(a.begin, a.end(), value, cmp);
//Check that we have actually found the value.
//If the requested value is missing
//then we will have the value before where the requested value
//would be inserted.
if(it == a.end() || !cmp(*it, value))
{
//element not found
}
else
{
//element found
}
Run Code Online (Sandbox Code Playgroud)
Teo*_*oae 15
#include <algorithm>
using namespace std;
it = lower_bound(a.begin, a.end(), value, cmp);
Run Code Online (Sandbox Code Playgroud)