在set< pair>中找到具有特定第一坐标的任何元素。 > [英] Finding any element with specific first coordinate in set<pair> >
问题描述
我想弄清楚以下问题。
假设我在C ++中有以下容器:
std: :set< std :: pair< int,int> > my_container;
此集合(字典)按照< / code>在
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());
pre>
这将给你一个迭代器到
e
的第一个元素; std :: pair(x,-LIMIT)
不成立。
这样的元素有第一个组件>
x
(在这种情况下,集合中没有x
),或者第一个组件等于x
并且是第一个这样的(注意所有第二个组件大于或等于std :: numeric_limits< int> :: min()
定义)。I'm trying to figure out the following problem.
Suppose I have the following container in C++:
std::set<std::pair<int, int> > my_container;
This set (dictionary) is sorted with respect to the order
<
onstd::pair<int, int>
, which is the lexicographic order. My task is to find any element inmy_container
that has the first coordinate equal to, sayx
, and return the iterator to it. Obviously, I don't want to usefind_if
, because I need to solve this in logarithmic time.I would appreciate any advice on how this can be done
解决方案You can use
lower_bound
for this:auto it = my_container.lower_bound(std::make_pair(x, std::numeric_limits<int>::min());
This will give you an iterator to the first element
e
for whiche < std::pair(x, -LIMIT)
does not hold.Such an element either has its first component >
x
(in which case there's nox
in the set), or has the first component equal tox
and is the first such. (Note that all second components are greater than or equal tostd::numeric_limits<int>::min()
by definition).这篇关于在set< pair>中找到具有特定第一坐标的任何元素。 >的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!