STL vs. MFC CMap, CArray, CList 
Author Message
 STL vs. MFC CMap, CArray, CList

I am about to start a MFC project. What are the advantages/disadvantages of
using the STL vs. MFC built-in collection classes. At first thought, i would
prefer to use the STL, but something in my gut tells me it would be a big

I will be using the collection class to store objects or pointers to objects
derived from CObject.  These objects may in turn contain other CObjects or
pointers to CObjects.

I think (i may be wrong) that the only potential issue is serialization.
Could I not just cycle through a STL class (that is a member of CDoc) and
serialize its contents? would it's content upon reading contain valid

Thanks, Mark



Sun, 22 Jun 2003 03:28:37 GMT  
 STL vs. MFC CMap, CArray, CList

I was using CTypedList<CObList, CMyClasses*> alot in my project.
Here's what I noticed:

Serializing The List:

- The CTypedPtrList writes header? information about each class that's
serialized (I would open one of my files up using a hex editor and
visibly see the names of all my classes scattered throughout the

- It was also writing an extra 2 bytes for every object in the list(s)
(I traced though the serialization code, it seems to be apart of
object/class schema?).  

- When you have a list with a small # of elements, it uses a WORD
value to store the # of elements in the list when it could be using a
BYTE.  This comes in to play when you have a large # of lists with a
small # of elements (In some cases, I would have up to 1500 lists with
< 256 elements, thus wasting up to 1.5KB in the file size).

I ended up replacing all of my serialized list classes with the vector
STL template.  You have to write your own serialize code for the
vector, but that's a breeze:

CMyDoc::SerializeMyVector(CArchive& ar)
        // Storing pseudocode
        ar << (BYTE)myVector.size();      // Write # of elements

        for each element in vector

        // Loading pseudocode
        BYTE bySize;
        ar >> bySize;

        for 1 to bySize
                CVectorClass* pNew = new CVectorClass;          


It turns out that my file size was about 20-25% larger than what I
expected it to be due to the above.  (In one case, a 200KB file became
a 130KB file)  If you're using a large # of lists with a small # of
elements, or a small # of lists with a large # of elements, it may be
something to look into.  You can easily trace through the serialize
code to see what's going on.

- I don't think either is that hard to use (after you get used to it)
- the STL code seems to be more compact than collection code (at least
for the stuff I'm doing)
- according to sizeof(), an STL vector is 16 bytes, a CObList is
28bytes, and a CObArray is 20 bytes.

As far as performance (according to my profiling):
- Iterating an STL vector was slightly faster than iterating a CList
- Adding elements to a CList was faster than adding to a vector
(supposedly you can optimize this operation for a vector)

In the end, I didn't strictly choose one over the other, I ended up
using a hybrid environment.  I'd try doing some tests with some mockup
classes and see which works best for your situation.

Sun, 22 Jun 2003 10:43:06 GMT  
 [ 2 post ] 

 Relevant Pages 

1. STL vs MFC CMap, CArray, CList

2. Sorting a CArray or another class that combines CMap and CArray functionality

3. STL Equiv to MFC CMap?

4. Looking for classes CArray , CList ,CMap,CString - -- with no MFC support -- anybody know ?

5. Looking for classes CArray , CList ,CMap,CString - -- with no MFC support -- anybody know ?

6. CList and CMap destruction crash

7. DCOM Arrays of C++ structs to Automation Clients using CList and CMap

8. CList and CMap

9. Serializing CArray and CMap

10. Qn on CArray / CList?

11. CList and CArray

12. CFile, Carray, CList, CString, C........


Powered by phpBB® Forum Software