我有一个unordered_map向量,它是根据我定义的比较器函数进行排序的。我想使用二进制搜索来查找其中一个值,使用比较器函数。然而,二进制搜索只返回bool,我需要结果的索引/迭代器。我能做些什么?
发布于 2010-11-26 18:00:28
#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
}
发布于 2010-11-26 17:35:32
#include <algorithm>
using namespace std;
it = lower_bound(a.begin, a.end(), value, cmp);
https://stackoverflow.com/questions/4284049
复制相似问题