我正在尝试C++
map
通过使用upper_bound
函数来优化我的搜索: The map
is the variable table
:
Foo& search(uint64 id, uint64 hash) {
std::map<std::pair<uint64, uint64>, Foo>::const_iterator iter;
std::pair<uint64, uint64> key(id, hash);
iter = table.upper_bound(key);
// for completeness I search for those elements which may be a match
// they may fall into a different hash range
for( ; iter != table.end() || iter != table.begin(); --iter ) {
const Foo foo = iter->second;
if(foo.id() == foo.first_hash() <= hash &&
hash <= foo.second_hash()) {
if( foo.state() == NORMAL) {
return foo;
}
else {
// do something else
}
}
}
但是,当我执行程序时它只是挂起......看起来搜索根本不起作用,而且我没有日志告诉我错误在哪里......我在这里做错了什么?当我进行线性搜索时,它工作正常,但现在当我尝试改进算法时它失败了......