Finding any element with specific first coordinate in set<pair> >

纵然是瞬间 提交于 2019-12-05 00:58:18

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).

You could use std::set::lower_bound to get the lower and upper limits of the range like this:

#include <set>
#include <iostream>

// for readability
typedef std::set<std::pair<int, int> > int_set;

void print_results(const int_set& s, int i)
{
    // first element not less than {i, 0}
    int_set::const_iterator lower = s.lower_bound(std::make_pair(i, 0));

    // first element not less than {i + 1, 0}
    int_set::const_iterator upper = s.lower_bound(std::make_pair(i + 1, 0));

    for(int_set::const_iterator iter = lower; iter != upper; ++iter)
        std::cout << iter->first << ", " << iter->second << '\n';
}

int main()
{
    int_set s;

    s.insert(std::make_pair(2, 0));
    s.insert(std::make_pair(1, 9));
    s.insert(std::make_pair(2, 1));
    s.insert(std::make_pair(3, 0));
    s.insert(std::make_pair(7, 6));
    s.insert(std::make_pair(5, 5));
    s.insert(std::make_pair(2, 2));
    s.insert(std::make_pair(4, 3));

    print_results(s, 2);
}

Output:

2, 0
2, 1
2, 2
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!