在 c++ 中查找 multiset<pair<int,int> > 中的下界
Finding lower bound in multiset<pair<int,int> > in c++
我正在用 C++ 将值和索引存储在 stl 多重集中。现在我想找到给定值的下限值的最高索引。例如,如果集合由 {(2,1),(3,4),(4,2),(5,5),(5,6),(6,3)} 组成,现在给定值为 5 ,答案是6,因为5的下界是5,最高索引是6。如何将lower_bound函数修改为上面的return?
我尝试将 (5,0) 插入多重集并使用查找函数将前一个作为答案,但没有成功。提前致谢。
不要采用下限 (5,0),而是采用 (6, -inf) 的下限,然后将返回的迭代器取为前一个值。
int get_last_pos(x)
auto it = map.lower_bound(make_pair(x + 1, -INF))
assert(it != map.begin())
it--;
assert(it.first == x)
return it.second;
我正在用 C++ 将值和索引存储在 stl 多重集中。现在我想找到给定值的下限值的最高索引。例如,如果集合由 {(2,1),(3,4),(4,2),(5,5),(5,6),(6,3)} 组成,现在给定值为 5 ,答案是6,因为5的下界是5,最高索引是6。如何将lower_bound函数修改为上面的return?
我尝试将 (5,0) 插入多重集并使用查找函数将前一个作为答案,但没有成功。提前致谢。
不要采用下限 (5,0),而是采用 (6, -inf) 的下限,然后将返回的迭代器取为前一个值。
int get_last_pos(x)
auto it = map.lower_bound(make_pair(x + 1, -INF))
assert(it != map.begin())
it--;
assert(it.first == x)
return it.second;