CMap iteration
Nayab Khan -- Nayab_Khan@amrcorp.com
Wednesday, April 02, 1997
Environment: VC++ 4.2b, Win95, WinNT 4.0
I'm trying to iterate between elements of a CMap
object. The key type is long. I am using
GetStartPosition and GetNextAssoc() and not
Lookup(). I was assuming that it would return
elements in the order they were inserted into the
mapped list. Apparently, they are not. Can someone
tell me what is the order they are returned? It
certainly didn't look like the hashed value
either. What is it?
Mario Contestabile -- Mario_Contestabile.UOS__MTL@UOSMTL2.universal.com
Thursday, April 03, 1997
> Can someone
>tell me what is the order they are returned? It
>certainly didn't look like the hashed value
>either. What is it?
The order is indeterminate.
Mario Contestabile
mcontest@universal.com
Mike Blaszczak -- mikeblas@nwlink.com
Thursday, April 03, 1997
[Mini-digest: 3 responses]
At 11:01 4/2/97 -0600, Nayab Khan wrote:
>Environment: VC++ 4.2b, Win95, WinNT 4.0
>
>I'm trying to iterate between elements of a CMap
>object. The key type is long. I am using
>GetStartPosition and GetNextAssoc() and not
>Lookup(). I was assuming that it would return
>elements in the order they were inserted into the
>mapped list.
That's a bad assumption. Maps are, by definition, not well-ordered.
There's no list--there's just a map.
>Apparently, they are not.
Actually, it's by by design that they are not.
>Can someone
>tell me what is the order they are returned? It
>certainly didn't look like the hashed value
>either. What is it?
If you need a deterministic order, you need to use a different
data structure. Right now, the map enumeration works loosely by
hash key value, but that order is folded over different buckets
of hash keys. Maybe that implementation will change in a future
version.
Please don't rely on behaviour that isn't guaranteed.
.B ekiM
Crotch Rocket / Full-Body Rocket / Trip Report Central!
95 Honda VFR-750F / 94 Mazda RX-7 / http://www.nwlink.com/~mikeblas/
Less work, more hockey!
These words are my own - I do not speak on behalf of Microsoft.
-----From: Shane Triem
This is implementation dependent. According to the collection class
documentation:
Table 1 Collection Shape Features
Shape Ordered? Indexed? Insert an element Search for specified
element Duplicate elements?
List Yes No Fast Slow Yes
Array Yes By int Slow Slow Yes
Map No By key Fast Fast No (keys)
Yes (values)
As shown above, a map is not "ordered" so there is no guarantee that the
order in which elements are added to the map is the actual order in which
elements are stored.
If you want an ordered collection, use a list or an array.
Shane
-----Original Message-----
From: Nayab Khan
Sent: Thursday, April 03, 1997 12:11 PM
To: SHANE; 'MFC-L@SMTP '
Subject: CMap iteration
Environment: VC++ 4.2b, Win95, WinNT 4.0
I'm trying to iterate between elements of a CMap
object. The key type is long. I am using
GetStartPosition and GetNextAssoc() and not
Lookup(). I was assuming that it would return
elements in the order they were inserted into the
mapped list. Apparently, they are not. Can someone
tell me what is the order they are returned? It
certainly didn't look like the hashed value
either. What is it?
-----From: "Chris W. Rea [UL]"
The documentation for GetStartPosition states that the iteration sequence
is not predictable. Sounds like you need something other than a CMap. If
you need to do iteration in a particular order, a hash table won't do that
for you (that is, not without you augmenting it somehow.)
On Wed, 2 Apr 1997, Nayab Khan wrote:
> Environment: VC++ 4.2b, Win95, WinNT 4.0
>
> I'm trying to iterate between elements of a CMap
> object. The key type is long. I am using
> GetStartPosition and GetNextAssoc() and not
> Lookup(). I was assuming that it would return
> elements in the order they were inserted into the
> mapped list. Apparently, they are not. Can someone
> tell me what is the order they are returned? It
> certainly didn't look like the hashed value
> either. What is it?
>
Haridev T.V. -- haridev@stanford.com
Thursday, April 03, 1997
[Mini-digest: 3 responses]
Nayab Khan wrote:
>=20
> Environment: VC++ 4.2b, Win95, WinNT 4.0
>=20
> I'm trying to iterate between elements of a CMap
> object. The key type is long. I am using
> GetStartPosition and GetNextAssoc() and not
> Lookup(). I was assuming that it would return
> elements in the order they were inserted into the
> mapped list. Apparently, they are not. Can someone
> tell me what is the order they are returned? It
> certainly didn't look like the hashed value
> either. What is it?
Please read the documentation for CMap in the CMap Overview.
Remarks
CMap is a dictionary collection class that maps unique keys to values.
Once you have inserted a key-value pair (element) into the map, you can
efficiently retrieve or delete the pair using the key to access it. You
can also iterate over all the elements in the map.
A variable of type POSITION is used for alternate access to entries. You
can use a POSITION to =93remember=94 an entry and to iterate through the
map.=20
****You might think that this iteration is sequential by key value;=20
it is not. The sequence of retrieved elements is indeterminate.****
-----From: Francisco =?iso-8859-1?Q?Jos=E9?= Sen
At 11:01 AM 4/2/97 -0600, you wrote:
>Environment: VC++ 4.2b, Win95, WinNT 4.0
>
>I'm trying to iterate between elements of a CMap
>object. The key type is long. I am using
>GetStartPosition and GetNextAssoc() and not
>Lookup(). I was assuming that it would return
>elements in the order they were inserted into the
>mapped list. Apparently, they are not. Can someone
>tell me what is the order they are returned? It
>certainly didn't look like the hashed value
>either. What is it?
>
>
CMap is not an ordered collection. If you want an ordered one use a CList
and if you want speed too use a CArray.
------------------------------------------------
______ ______ _
/ ____/ / ____/ /\ / /
/ /___ / /___ / \ / /
/____ / / ____/ / /\ \/ /
_____/ / / /___ / / \ /
/______/ /_____/ /_/ \/
------------------------------------------------
Francisco Jos=E9 Sen del Prado
------------------------------------------------
e-mail: josesen@dinamicmultimedia.es
http: www.geocities.com/CapeCanaveral/9268
------------------------------------------------
Dinamic Multimedia Madrid, Spain
-----From: DFPav@aol.com
In a message dated 97-04-03 20:12:06 EST, you write:
> >I'm trying to iterate between elements of a CMap
> >object. The key type is long. I am using
> >GetStartPosition and GetNextAssoc() and not
> >Lookup(). I was assuming that it would return
> >elements in the order they were inserted into the
> >mapped list.
>
>From the MFC online help (VC 1.52c),
file: mfc.hlp
topic class CMapStringToOb : public CObject
---
You can use a POSITION to "remember" an entry and to iterate through the map.
You might think that this iteration is sequential by key value; it is not.
The sequence of retrieved elements is indeterminate.
--
I thought this was an advanced list. RTFM, already.
Dan
Become an MFC-L member
| Вернуться в корень Архива
|