I have a range of numbers (In reality let's say they are 1'000'000). Each range has a lower and an upper bound. I have used a sort (in reality quick sort) function to sort them.
Now given a point 0.3
, I would like to find all the ranges which contain this number. I look for an efficient way to find these active ranges. I am not sure whether upper_bound and lower_bound are the correct solutions. Could anyone please help me completing this code?
P.S. assuming the array length is huge, I look for a method which uses the advantage of sorted vector.
P.S. The overlapped layers are in the range of 500
. Not as big as 1'000'000
.
P.S. Always, min <= max
(if important).
#include <vector>
#include <iostream>
#include <algorithm>
class Range
{
public:
double min;
double max;
};
int main()
{
std::vector<Range> range_list
{
{0.020742,0.460304},
{0.168229,0.274032},
{0.174609,0.420922},
{0.352116,0.660738},
{0.445867,0.910085},
{0.249047,0.794357},
{0.264342,0.953567},
{0.671572,0.823919},
{0.424151,0.891832},
{0.041007,0.515920}
};
std::vector<int> min_list;
std::vector<int> max_list;
min_list.resize(range_list.size());
for(int i=0;i<(int)range_list.size();i++)
min_list[i]=i;
max_list=min_list;
std::sort(
min_list.begin(),
min_list.end(),
[&range_list](int i,int j)
{
return range_list[i].min<range_list[j].min;
});
std::sort(
max_list.begin(),
max_list.end(),
[&range_list](int i,int j)
{
return range_list[i].max<range_list[j].max;
});
std::vector<int>::iterator ???,???;
???=std::lower_bound(min_list.begin(),
range_list.end(), 0.3);
???= std::upper_bound(max_list.begin(),
range_list.end(), 0.3);
????????????
std::vector<int> active_range=...
std::cout<<"Active ranges are:"<<std::endl;
for(auto x: active_range)
std::cout<<"("<<x.min<<","<<x.max<<")"<<std::endl;
return 0;
}