Reputation: 71
set<pair<int,int> >s;
pair<int,int>a0 = make_pair(1,10); // pair is kind of range of integer
air<int,int>a1 = make_pair(11,50);
s.insert(a0);
s.insert(a1);
Now I need a function which returns true when I search any integer which lies between range of any pair in the set.
Upvotes: 3
Views: 1252
Reputation: 66230
Now I need a function which returns true when I search any integer which lies between range of any pair in the set.
It seems a work for std::any_of()
#include <set>
#include <utility>
#include <iostream>
#include <algorithm>
bool anyRange (std::set<std::pair<int, int>> const & s, int v)
{ return std::any_of(s.cbegin(), s.cend(),
[&](std::pair<int, int> const & p)
{ return (p.first <= v) && (v <= p.second); }); }
int main()
{
std::set<std::pair<int, int>> s { {1, 10}, {11, 50} };
std::cout << anyRange(s, -5) << std::endl; // print 0
std::cout << anyRange(s, 5) << std::endl; // print 1
std::cout << anyRange(s, 25) << std::endl; // print 1
std::cout << anyRange(s, 75) << std::endl; // print 0
}
Upvotes: 2
Reputation: 1789
Iterate over the set and check if the integer is in each pair inside the set
bool bet(set<pair<int,int> > s, int integer)
{
for (auto it : s)
{
if (it.first <= integer && it.second >= integer)
return true;
}
return false;
}
Upvotes: 2
Reputation: 1649
If you want fast search (i.e. better than linear) then you should use interval tree. std::set is not suitable for this problem
Upvotes: 1