tokenizer: finding the last element. is it possible?

Hello, I'm using boost::tokenizer and want to goto the last token in the sequence, so I tried Tok::iterator last = tok.end(); last--; // error decrement not defined last = tok.begin(); advance(last,tok.size()-1); // error size() not defined. Is it possible to add decrement or a back() reference to tokenizer? Thanks Vince

--- At Fri, 25 Apr 2003 17:01:56 -0400, Vincent LaBella wrote:
Hello, I'm using boost::tokenizer and want to goto the last token in the sequence, so I tried
Tok::iterator last = tok.end(); last--; // error decrement not defined
last = tok.begin(); advance(last,tok.size()-1); // error size() not defined.
Is it possible to add decrement or a back() reference to tokenizer?
Probably not. The iterator is a likely a forward iterator (I havent looked at the code). Forward iterators typically have valid assignment operators (I think that's a requirement). If that's true then you can use a look-ahead technique copying the current iterator until the next is what you are looking for. Implementation left as an excercise. (Is there a standard algorithm that might fit well besides for_each()?) ...Duane

--- At Fri, 25 Apr 2003 17:01:56 -0400, Vincent LaBella wrote:
sequence, so I tried <snip> last = tok.begin(); advance(last,tok.size()-1); // error size() not defined. <snip> Implementation left as an excercise. (Is there a standard algorithm
--- In Boost-Users@yahoogroups.com, "Duane Murphy"
might fit well besides for_each()?)
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. jh

--- At Tue, 29 Apr 2003 10:56:03 +0000, John Harris wrote:
--- At Fri, 25 Apr 2003 17:01:56 -0400, Vincent LaBella wrote:
sequence, so I tried <snip> last = tok.begin(); advance(last,tok.size()-1); // error size() not defined. <snip> Implementation left as an excercise. (Is there a standard algorithm
--- In Boost-Users@yahoogroups.com, "Duane Murphy"
wrote: that might fit well besides for_each()?)
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. ...Duane

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::vectorstd::string v; std::copy(begin, end, std::back_inserter(v)); // use v[v.size()-1] here
participants (3)
-
Duane Murphy
-
John Harris
-
Vincent LaBella