Question

given a container like std::vector<T>, where T is a generic type for numerical quantities ( like int, float, etc ... ), I would like to have a predicate that takes as arguments :

  • the container itself
  • a range, a type expressing a numerical set of values, or simply 2 extreme numerical values, that should verify the predicate
  • a tolerance, a type expressing a possible ( optional ) extension to the range, in relative terms ( like a percentage ) or in absolute terms ( with a numeric )

I would like to have as an output:

  • the result of the predicate
  • if the range is not enough for some quantities so the tolerance is "used" by some values or not ( but the predicate is still true )

In the C++ standard library looks like there is not even the concept of range and this is strange to me, simply because there are a lot of interesting algorithms and types, but nothing that could possibly serve my purposes in this case.

Before continuing on my own path, I ask, there is something like this in terms of types and algorithms in the C++ library ?

Was it helpful?

Solution

Rather than evaluate the condition over each item in the collection, I think I'd start by using std::minmax_element to find the smallest and largest elements in the collection.

From there, it's a simple matter of testing whether result.first < minimum and/or result.second > maximum.

Dealing with the tolerance is basically a matter of repeating the test with the expanded range.

As to whether this is likely to be better than std::find_if or std::all_of, it'll depend on both the number of items you expect in the collection and the likelihood of finding a value out of range that would have allowed early exit. I think you'd need some knowledge of the data to give a meaningful prediction about it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top