I have a list of tuple, the list was sorted based on the first element of the tuple but the second and last elements are in random order. Now I want to find all tuples with the first element within a range, i.e. return all tuples for (tuple.first>-X and tuple.first<X). Among all these returning tuples, I need to find the maximum and minimum value in the second element of the tuples. How can a STL algorithm implement this?
I have a list of tuple, the list was sorted based on the first
Share
Since it is already sorted, you can use
equal_rangeto get a pair of iterators that delimit the range of “interesting” tuples:Then, you can simply iterate over this range, and register the minimum and maximum values that you see:
So… it’s not a single STL algorithm.
Note:
std::min_elementandstd::max_elementdo exist, but that would mean looping twice over the range, it’s certainly feasible though.Note that it gives a tuple, and not the
.secondmember.