Boost logo

Boost :

From: Victor A. Wagner, Jr. (vawjr_at_[hidden])
Date: 2002-05-16 02:43:05


At Wednesday 2002/05/15 07:18, you wrote:
>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();
         return !cont.empty() && boost::next(cont.begin()) == cont.end();
>}
Isn't that necessary to prevent an invalid memory de-reference?

>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().

the "problem" I'm having is attempting to generalize how to write an
iterator that will increment by more than one element AND still be able to
be used in the standard algorithms (withOUT a huge performance
"hit"). For example a "guarded_advance(n, some_ending_iterator);" solves
my problem (if I can figure out how to wrap it in an extender class for
arbitrary iterators) and your is_single_element would become return
!cont.empty() && cont.begin().guarded_advance(1, cont.end()) == cont.end();

>Comments?
>
>_______________________________________________
>Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Victor A. Wagner Jr. http://rudbek.com
PGP RSA fingerprint = 4D20 EBF6 0101 B069 3817 8DBF C846 E47A
PGP D-H fingerprint = 98BC 65E3 1A19 43EC 3908 65B9 F755 E6F4 63BB 9D93
The five most dangerous words in the English language:
               "There oughta be a law"


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