在set< pair>中找到具有特定第一坐标的任何元素。 > [英] Finding any element with specific first coordinate in set<pair> >

查看:520
本文介绍了在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 < on std::pair<int, int>, which is the lexicographic order. My task is to find any element in my_container that has the first coordinate equal to, say x, and return the iterator to it. Obviously, I don't want to use find_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 which e < std::pair(x, -LIMIT) does not hold.

Such an element either has its first component > x (in which case there's no x in the set), or has the first component equal to x and is the first such. (Note that all second components are greater than or equal to std::numeric_limits<int>::min() by definition).

这篇关于在set&lt; pair&gt;中找到具有特定第一坐标的任何元素。 &gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆