Boost logo

Boost :

Subject: Re: [boost] RFC: Sequence Properties and Boost Algorithm/Functional
From: Jeffrey Hellrung (jhellrung_at_[hidden])
Date: 2010-01-22 17:13:31


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?
>
> Regardless, I forged ahead with the rather naïve implementation (see below),
> which can be used as a predicate with for_each as shown:
<snip>
> Regards,
>
> Grant

Perhaps std::adjacent_find and std::less is all you need, e.g., to
determine if a sequence is decreasing?

I.e., a sequence is decreasing iff all adjacent pairs of elements xi,
x[i+1] satisfies xi >= x[i+1] iff no adjacent pair of elements xi,
x[i+1] satisfies xi < x[i+1] (assuming that (!>=) == (<)).

- Jeff


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk