Re: CMap in CMap
On Nov 9, 1:12 pm, Fred <f...@laposte.net> wrote:
So I guessed I could get out with it this way... but your post about the
std::map object puzzled me ;)
Could you tell me more about this std::map thing? Is it more efficient
than CMap? It seems to be less convenient to manipulate, isn't it?
I'll try a shot at that question.
Implementation-wise, difference is significant. CMap is a hash map
(http://en.wikipedia.org/wiki/Hash_table), whereas std::map is based
on key ordering. Often, std::map internally uses a data structure
called red-black tree (http://en.wikipedia.org/wiki/Red-black_tree).
As a consequence, they have wildly different performance
characteristics, but never mind that, I don't think anyone can tell
you whether one will perform better on your own data. Speed of your
hashing function when using CMap, and operator< when using std::map,
plays a part, too.
You should also bear in mind that for small data sets, a linear search
on an array will beat any associative container. In that light, for
me, using an associative container is often a matter of convenience
rather than speed (e.g. avoids handling duplicates and I don't have to
write "find" function).
As for convenience, I fail to see how is any STL container less
convenient than it's MFC counterpart. Take a map:
* to insert an element, it's MFCMap.SetAt(key, value) or MFCMap[key] =
value versus STLMap[key] = value or STLMap.insert(SLMapType(key,
value)). With STL map, you have performance-conscious insert overload,
where you can specify the position. That's useful when you use find or
lower_bound and subsequently decide to insert a new element.
* to remove an element, you use MFCMap.RemoveKey versus STLMap.erase,
where erase is overloaded to three options, one of them being
performance-conscious, and other working on a range of elements
(sweet).
* to locate an element, you use MFCMap.Lookup(key, value) or PLookup
versus STLMap.find or lower_bound, both being more performance-
conscious in STLMap ( this is getting annoying now :-) ).
* to iterate, I fail to see how is brain-dead GetStartPosition/
GetNextWhatever and POSITION convention of MFC better than a for,
while or std::for_each.
It should be noted, however, that STL induces a certain amount of
syntax gibberish. To alleviate that, it's best to typedef every
container type and use STL-provided typedefs, e.g.:
typedef std::map<my_key_type, my_value_type> CMyMap;
CMyMap map;
for (CMyMap::const_iterator pItem=map.begin(); pItem != map.end(); +
+pItem)
{
CMyMap::const_reference KeyAndValue = *pItem;
WorkWith(KeyAndValue);
}
To me personally, map is better than CMap in that I prefer writing an
ordering predicate (operator<) to writing a hashing function.
Depending on key structure, a hashing function is more complicated,
and can cause a performance hit more easily.
What you don't have with STL out of the box is serialization.
HTH,
Goran.