Boost logo

Boost Users :

From: John Harris (john.harris_at_[hidden])
Date: 2003-04-29 16:35:57


> >how about: std::advance(first, std::distance(first,last)-1) ?
> >
> >The only requirement for distance() is that iterators be input
> >iterators, which tokenizer's are.
>
> This is an interesting solution. The downside is that the iterator
would
> get advanced twice. I believe the implementation for distance for a
> forward iterator is to walk the iterator and count the distance.
Then
> advance is going to do the same. This is efficient to type, but
> inefficient to run.

Of course. But with an iterator modelling only the forward_iterator
concept, there isn't much choice. In fact the end() iterator can be
just instantiated defaultly, as shown in a comment in the online
docs. This indicates that it contains no information that could be
used for traversal. You either have to copy the data out using the
iterator or pay for the walking of it twice, like you said.

So something like this would work, too:

std::vector<std::string> v;
std::copy(begin, end, std::back_inserter(v));
// use v[v.size()-1] here


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net