If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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.