map iterators -- Circulators?

J

John

I would like to make the map iterators cyclic. i.e.
If I I do a it-- when it = map.begin() then it gives
it.end()-1 and when I do it++ when it= it.end()-1, I would
like to get it.begin().

Any comments on how to do this?

Thanks,
--j
 
L

Larry I Smith

John said:
I would like to make the map iterators cyclic. i.e.
If I I do a it-- when it = map.begin() then it gives
it.end()-1 and when I do it++ when it= it.end()-1, I would
like to get it.begin().

Any comments on how to do this?

Thanks,
--j

What happens when the map has just one entry, or is empty? :)

Larry
 
J

Jonathan Mcdougall

I would like to make the map iterators cyclic. i.e.
If I I do a it-- when it = map.begin() then it gives
it.end()-1 and when I do it++ when it= it.end()-1, I would
like to get it.begin().

The problem is that usually, iterators do not know about their
containers, so you'll have to wrap the standard iterator along with the
container in a new class, which will implement all the operators. These
should simply forward the call to the iterator, except for ++, -- and
the like which will do the checking and wrap around.

Jonathan
 
S

Stephen Howe

What happens when the map has just one entry..

It increments or decrements to the same entry :)
..., or is empty? :)

It increments or decrements forever searching for the next entry.
Infinite loop.
Big Red Switch time :)

Stephen Howe
 
M

msalters

Jonathan said:
The problem is that usually, iterators do not know about their
containers, so you'll have to wrap the standard iterator along with the
container in a new class, which will implement all the operators. These
should simply forward the call to the iterator, except for ++, -- and
the like which will do the checking and wrap around.

In fact, that's not a problem at all IF the iterator is
constructed from a pair of iterators (begin,end). You
don't have to know where the iterator points to, just
when to wrap. In fact, the advantage of using an iterator
range is that it will also work if you're interested in
a subset of map values (e.g. only positive keys).

The only wrapper class is then the iterator wrapper. In
fact, there are libraries that do this already (VTL).

HTH,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top