What iterator category are you talking about ? Random access iterators (e.g. for std::vector or std::string), bidirectional iterators (e.g. std::list, std::map, std::set) or forward iterators (e.g. istream or ostream) ?
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
Naughty, Yves - that's a trick question, dropping forward iterators into it, when he's asked for the previous.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
Originally posted by Yves M
What iterator category are you talking about ? Random access iterators (e.g. for std::vector or std::string), bidirectional iterators (e.g. std::list, std::map, std::set) or forward iterators (e.g. istream or ostream) ?
Aha I see that random access iterators have that thing (iter-1) which is what I thought you could do. However since I'm using a map, it's not possible. Oh dear. Why isn't (iter-1) defined when you've got an iterator over a map?
It is (sort of) - you just express it as "--iter". std::map::iterator is a bidirectional iterator, so has operator++ and operator-- (both forms of each) defined. Only random access iterators have operator+ and operator- defined.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
Originally posted by Graham
It is (sort of) - you just express it as "--iter". std::map::iterator is a bidirectional iterator, so has operator++ and operator-- (both forms of each) defined. Only random access iterators have operator+ and operator- defined.
No, "--iter" and "iter-1" are significantly different because the former alters iter, but iter-1 does not.
Originally posted by c94wjpn
Aha I see that random access iterators have that thing (iter-1) which is what I thought you could do. However since I'm using a map, it's not possible. Oh dear. Why isn't (iter-1) defined when you've got an iterator over a map?
Well, because making a map's iterator random access would make you incurr big performance penalties. The ++ and -- operators for map's iterators are already amortized constant time, so it's a little slower than constant time. Getting a random access iterator over a map would be amortized linear time (so something like O(N log(log(N))) ).
It just doesn't make sense. Anyways, I don't see the problem in having a temporary iterator which points at the previous element.
Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
Supports C++ and VB out of the box, but can be configured for other languages.
Originally posted by c94wjpn
No, "--iter" and "iter-1" are significantly different because the former alters iter, but iter-1 does not.
--iter is, like iter = iter-1;
Which is why I said "sort of": you can get the previous iterator (assuming that iter != cont.begin() ), but that's as far as it goes.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there. -- Gordon Bell
Originally posted by Graham
Which is why I said "sort of": you can get the previous iterator (assuming that iter != cont.begin() ), but that's as far as it goes.
Bookmarks