
On Jan 22, 2010, at 2:02 PM, Grant Erickson wrote:
For a recent project, I needed to inquire about a sequence of objects and make a determination as to whether the sequences were:
* Increasing * Strictly Increasing * Decreasing * Strictly Decreasing
Looking through STL, Boost documentation, Boost sources and headers and the Boost mailing lists I was able to find neither an existing algorithm or stateful unary predicate functor for accomplishing this for a pair of input iterators. However, I was a bit surprised considering that this seems like a sequence property query that would tend to come up fairly often and serve a general utility. Did I just fail to form the proper search/query or am I overestimating the general utility?
I've got this floating around from a while back (as well as the other three - increasing, decreasing, and strictly_decreasing ). Maybe these should go in the algorithms library: template<typename T> bool is_strictly_increasing ( T* begin, T* end ) { // Empty sequences are strictly increasing if ( begin == end ) return true; T* iter = begin; T val = *iter++; while ( iter != end ) { if ( ! ( *iter > val )) return false; val = *iter++; } return true; } template<typename I> bool is_strictly_increasing ( I begin, I end ) { // Empty sequences are strictly increasing if ( begin == end ) return true; I iter = begin; typename I::value_type val = *iter++; while ( iter != end ) { if ( ! ( *iter > val )) // if ( val >= *iter ) return false; val = *iter++; } return true; } -- Marshall