Boost logo

Boost :

From: Damien Fisher (damien_at_[hidden])
Date: 2002-05-15 09:18:41


I would like to see something like this in Boost:

template<typename Container>
bool is_single_element(const Container &cont)
{
    return boost::next(cont.begin()) == cont.end();
}

In a current project, I am finding the need to continually check for
single element std::lists, and I have to keep restraining myself from
using size(). I think the above would fit nicely into
boost/utility.hpp (or maybe some sort of algorithm header file?).

The following is a more general, possibly useful extension of the above
(although I have no need for it ATM):

template<typename Container>
bool is_n_elements(const Container &cont, unsigned int n)
{
    // ...
}

Of course, this should be specialized for containers where calling
size() isn't an issue, to just call size().

Comments?


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