Jyt*_*tug 11 c++ containers stl
我想弄清楚以下问题.
假设我在C++中有以下容器:
std::set<std::pair<int, int> > my_container;
Run Code Online (Sandbox Code Playgroud)
这组(字典)被相对于所述顺序排序<
上std::pair<int, int>
,这是字典顺序.我的任务是找到第一个坐标等于,比如说的任何元素,并将迭代器返回给它.显然,我不想使用,因为我需要在对数时间内解决这个问题.my_container
x
find_if
我很感激有关如何做到这一点的任何建议
你可以使用lower_bound
这个:
auto it = my_container.lower_bound(std::make_pair(x, std::numeric_limits<int>::min());
Run Code Online (Sandbox Code Playgroud)
这会给你一个迭代器的第一个元素e
为这e < std::pair(x, -LIMIT)
不成立.
这样的元素要么具有第一个分量> x
(在这种情况下x
集合中没有),要么第一个分量等于x
并且是第一个分量.(请注意,std::numeric_limits<int>::min()
根据定义,所有第二个组件都大于或等于).
归档时间: |
|
查看次数: |
445 次 |
最近记录: |