By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

Is STL iterator singleton ?

P: n/a
Is iterators singleton?

I am accessing a map through wrapper functions to it; that allow me to
access the contents of the map as

CUnit &unit = hash.getFirstunit();
while(hash.hasMoreunits())
{
unit = hash.getNextUnit();
}

Thats all about it. A call to getFirstUnit will initialize the
iterator. hasMoreUnits will move the iterator one unit forward and
hasNextUnit will access the next one and so on.

[just a side note, i was trying to implement a hastable with map and
set, so that each branch of the hash is a set protruding from a map
element.]

All fine unitl now. I have another function that is a straight forward
call.

hash.getUnitByName();

I initialise another set of iterators on the stack inside the function
(iterator objects scoped inside that function) which is local to the
function so that i don't disturb the original master iterators that i
keep intact in my class scope. Once I call this function, that will
throw the master iterator off the edge. What is happening here.

~raan

Aug 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 2007-08-08 06:00, raan wrote:
Is iterators singleton?

I am accessing a map through wrapper functions to it; that allow me to
access the contents of the map as

CUnit &unit = hash.getFirstunit();
while(hash.hasMoreunits())
{
unit = hash.getNextUnit();
}

Thats all about it. A call to getFirstUnit will initialize the
iterator. hasMoreUnits will move the iterator one unit forward and
hasNextUnit will access the next one and so on.
To answer the question in the subject, no.

What I really want to know is how you implemented that wrapper to get
that kind of functionality, using global variables? My advice, if you
insist on keeping the code like that, is to make hasMoreUnits() and
getNextUnit() take a CUnit as parameter so they know which iterator to
test for end / increment.

If the problem you are trying to solve is to iterate over your home-made
hashmap then perhaps you should instead you should try to create a STL-
compatible iterator (i.e. one that behaves like other iterators). I've
done it, so it's quite possible.

--
Erik Wikström
Aug 8 '07 #2

P: n/a
On Aug 8, 1:00 pm, raan <palaka...@gmail.comwrote:
[just a side note, i was trying to implement a hastable with map and
set, so that each branch of the hash is a set protruding from a map
element.]
The point of hash tables is that their lookup is constant time (O(1))
- faster than binary maps which are O(ln n). Using C++/STL terms, a
set is actually a binary map where the keys are important and there is
no value associated with the node. So the idea of using a map and a
set to implement a hash table involves using two lower performance
mechanisms in an attempt to create a higher performance mechanism.
>From a performance perspective: impossible. It's also impossible from
a functional perspective: consider what you're asking...

A hash table finds data associated with a particular key by generating
a numerical hash value from that key, using that value (probably
wrapped by the container size) to directly index to an entry related
to that value, then using the full key to see whether the desired data
exists at that spot. One reason it might not is because some other
key's hash value happened to yield the same index, and the other value
is here. The hash map implementation must then handle the collision
by searching elsewhere, or making the "entry" itself some kind of
associative container. That means that the original key must still be
used to find the desired value amongst the key/value pairs associated
with the hash value.

So, given your "map of sets", you presumably look up the hash value in
the map, then you have a set of values, but they're no longer
associated with a particular key and you can't work out which one you
want.

FWIW, it _would_ make sense to implement a hash table as a vector of
some associative container (e.g. (binary) map). Why vector? Because
it's fast to randomly index. And using a binary map as this secondary
resolution step is ok because you've presumably reduced the number of
elements you're searching through. If your vector has M elements, and
your hash map has N, then your binary map lookup will typically only
be - loosely speaking - O(max(ln (N/M),1)).

( Actually, it's generally faster to keep your vector much larger than
the number of entries in the hash map, and successively try a list of
offsets (e.g. 1, 3, 6, 13 whatever) from the position to which the
hash value resolved. But if you want to start learning about hash
tables, start with the simple std::map<based approach above. )

Cheers,

Tony

Aug 8 '07 #3

P: n/a
On Aug 8, 11:05 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-08-08 06:00, raan wrote:
Is iterators singleton?
I am accessing a map through wrapper functions to it; that
allow me to access the contents of the map as
CUnit &unit = hash.getFirstunit();
while(hash.hasMoreunits())
{
unit = hash.getNextUnit();
}
Thats all about it. A call to getFirstUnit will initialize
the iterator. hasMoreUnits will move the iterator one unit
forward and hasNextUnit will access the next one and so on.
To answer the question in the subject, no.
What I really want to know is how you implemented that wrapper
to get that kind of functionality, using global variables? My
advice, if you insist on keeping the code like that, is to
make hasMoreUnits() and getNextUnit() take a CUnit as
parameter so they know which iterator to test for end /
increment.
My advice is to not use that interface. It's probably the
poorest solution to the question of how to iterate over all of
the elements.
If the problem you are trying to solve is to iterate over your
home-made hashmap then perhaps you should instead you should
try to create a STL- compatible iterator (i.e. one that
behaves like other iterators). I've done it, so it's quite
possible.
Creating a classical iterator, such as described in the GoF, is
generally even easier, and it's easier to use. I try to provide
both interfaces to my iterators; I'll use the STL interface when
calling standard algorithms (or rather, the standard algorithms
will use the STL interface), and the GoF interface most of the
rest of the time (since it only requires a single iterator
object, instead of two).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 8 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.