std :: set :: equal_range用于std :: pair的容器 [英] std::set::equal_range for a container of std::pair

查看:135
本文介绍了std :: set :: equal_range用于std :: pair的容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到包含int值i的所有范围[a,b],其中a <= i <= b。我对范围集使用 set< std:pair< int,int>>



在下面,在向量< int> 上使用相等范围产生范围的开始和结束。



当我对 set< pair< int,int>> 执行相同操作时,

  #include< set> ; 
#include< iostream>
#include< algorithm>
using namespace std;

int main()
{
int ia [] = {1,2,3,4,5,6,7,8,9,10};
set< int> s1(begin(ia),end(ia));
auto range1 = s1.equal_range(5);

cout<< * range1.first<< < * range1.second<< endl; // prints 5 6

pair< int,int> p [] = {make_pair(1,10),
make_pair(11,20),
make_pair(21,30),
make_pair
set< pair< int,int>> s(begin(p),end(p));

auto range = s.equal_range(make_pair(12,12));

cout<< range.first->第一<< <范围第一 - >第二< endl; // prints 21 30,为什么?
cout<< range.second-> first<< < range。second-> second<< endl; // prints 21 30

}


5 6

21 30

21 30

为什么在设置< pair< int,int>> 不包括包含值(12)的范围,即[11.20]

解决方案

equal_range 的行为完全正确: / p>

  assert(std :: make_pair(11,20)< std :: make_pair(12,12) 
assert(std :: make_pair(12,12)< std :: make_pair(21,30));

[11,20]不是范围,它是一对。



[12,12]不是在内部[11,12] ,20],它比它大。 std :: pair 的小于运算符首先比较第一个元素,只有当它们相等时,才会查看第二个元素,因此 x ,make_pair(11,x)小于 code>和 y



c $ c>告诉你在[11,20]之后和[21,30]之前插入[12,12],这是正确的。



想将对作为值范围,你需要编写代码来做到这一点,不要假设对的内在比较。你实际上试图在一对int对的范围内找到一个int 12,但是写了代码在一对int对中寻找一对[12,12]。这不一样。


I'm trying to find all the ranges [a,b] enclosing int values i, where a <= i <= b. I'm using set<std:pair<int,int>> for the set of ranges.

In the following, using equal range on a vector<int> yields the start and one past the end of the range.

When I do the same for a set<pair<int,int>>, the result starts and ends at one past the end of the range and therefore doesn't include the range enclosing the value.

#include <set>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int ia[] = {1,2,3,4,5,6,7,8,9,10};
    set<int> s1(begin(ia),end(ia));
    auto range1 = s1.equal_range(5);

    cout << *range1.first << " " << *range1.second << endl; //prints 5 6

    pair<int,int> p[] = {make_pair(1,10), 
                         make_pair(11,20),  
                         make_pair(21,30), 
                         make_pair(31,40)}; 
    set<pair<int,int>> s(begin(p), end(p));    

    auto range = s.equal_range(make_pair(12,12));

    cout << range.first->first << " " << range.first->second << endl; //prints 21 30, why?
    cout << range.second->first << " " << range.second->second << endl; //prints 21 30

}

prints
5 6
21 30
21 30
Why does equal_range on the set<pair<int,int>> not include the range that encloses the value (12), namely [11.20]

解决方案

equal_range is behaving completely correctly:

assert( std::make_pair(11, 20) < std::make_pair(12, 12) );
assert( std::make_pair(12, 12) < std::make_pair(21, 30) );

[11,20] is not a range, it's a pair. The pair [12,12] is not "within" another pair, that makes no sense to even say.

[12,12] is not "within" [11,20], it's greater than it. The less-than operator for std::pair compares the first elements first, and only if they're equal does it look at the second elements, so make_pair(11,x) is less than make_pair(12, y) for any x and y

So equal_range tells you that [12,12] would be inserted after [11,20] and before [21,30], which is correct.

If you want to treat pairs as ranges of values you need to write code to do that, not assume the built-in comparisons for pairs does that. You're actually trying to find an int 12 in a range of pairs of ints, but have written code to find a pair [12,12] in a range of pairs of ints. That's not the same thing.

这篇关于std :: set :: equal_range用于std :: pair的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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