Click here to Skip to main content
6,629,885 members and growing! (24,126 online)
Email Password   helpLost your password?
Web Development » Trace and Logs » General     Beginner

Quick and Dirty Collection Class Notes

By Joe Harleman

An article describing MFC Collection Classes
VC6, Visual Studio, MFC, Dev
Posted:8 Apr 2000
Updated:28 Nov 2000
Views:70,334
Bookmarked:29 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
30 votes for this article.
Popularity: 4.97 Rating: 3.37 out of 5
1 vote, 10.0%
1
1 vote, 10.0%
2

3
5 votes, 50.0%
4
3 votes, 30.0%
5

Introduction

If you're tired of wading through documentation, looking for examples of how to use collection classes, then the text files that accompany this article may help. I put this together in one afternoon after a couple of exhausting days trying to use collections. Since I am on a beginner's level, advanced user's may not find this useful, but for the rest of us neophytes may make the light bulb come on sooner.

Collection classes covered are Collection classes not covered are
Arrays  

CArray

CTypedPtrArray

CObArray

CPtrArray

CByteArray

CDWordArray

CStringArray

CUintArray

CWordArray

Lists  

CTypedPtrList

CObList

CPtrList

CStringList
Maps  

CMap

CTypedPtrMap

CMapWordToPtr

CMapPtrToWord

CMapPtrToPtr

CMapStringToPtr

CMapWordToOb

CMapStringToOb

CMapStringToString

Below is an example of one of the files. Most of it was plagiarized from the MSDN library, however I can't remember where I got it all. Apologies to those who may see their code show up here. The code is in fragments with just enough to give you the idea of how it should work. If there is enough interest, I will try to correct the typos, mistakes and omissions. And I welcome all corrections.

Expand the Header Files folder, and open file StdAfx.h. At the end of the file, type:

#include <afxtempl.h>    // MFC templates
/////////////////////////////////////////////////////////////////////////

CTypedPtrList < class BASE_CLASS, class TYPE >

    BASE_CLASS  Base class of the typed pointer list class;
                must be a pointer list class (CObList or CPtrList).

    TYPE        Type of the elements stored in the base-class list.

    The CTypedPtrList class provides a type-safe 
    �wrapper� for objects of class
    CPtrList. When you use CTypedPtrList 
    rather than CObList or CPtrList, the
    C++ type-checking facility helps 
    eliminate errors caused by mismatched
    pointer types.

    In addition, the CTypedPtrList wrapper 
    performs much of the casting that
    would be required if you used CObList or CPtrList.

    ==================================================
    Class Members
    ==================================================
    AddHead
        POSITION AddHead( TYPE newElement );
        void AddHead( CTypedPtrList<BASE_CLASS, TYPE> *pNewList );
    AddTail
        POSITION AddTail( TYPE newElement );
        void AddTail( CTypedPtrList<BASE_CLASS, TYPE> *pNewList );
    GetAt
        TYPE& GetAt( POSITION position );
        TYPE GetAt( POSITION position ) const;
    GetHead
        TYPE& GetHead( );
        TYPE GetHead( ) const;
    GetNext
        TYPE& GetNext( POSITION& rPosition );
        TYPE GetNext( POSITION& rPosition ) const;
    GetPrev
        TYPE& GetPrev(POSITION& rPosition );
        TYPE GetPrev( POSITION& rPosition ) const;
    GetTail
        TYPE& GetTail( );
        TYPE GetTail( ) const;
    RemoveHead
        TYPE RemoveHead( );
    RemoveTail
        TYPE RemoveTail( );
    SetAt
        void SetAt( POSITION pos, TYPE newElement );

    ==================================================
    Create
    ==================================================
    typedef CTypedPtrList<CPtrList, CMyStruct*> CMyStructList;
    CMyStructList m_myPtrList;

    typedef CTypedPtrList<CObList, CMyObject*>  CMyObList;
    CMyObList m_myObList;

    ==================================================
    Insert
    ==================================================
    CMyStruct* pMyStruct = new CMyStruct();
    pMyStruct->m_int = 1234;
    pMyStruct->m_float = 12.34f;
    pMyStruct->m_str.LoadString(IDS_INITIAL_STRING);
    m_myPtrList.AddTail(pMyStruct);

    m_myPtrList.InsertBefore(pos, pMyStruct);

    CMyObject* pMyObject = new CMyObject();
    m_myObList.AddTail(pMyObject);

    ==================================================
    Iterate
    ==================================================
    POSITION pos = m_myPtrList.GetHeadPosition();
    while( pos != NULL )
    {
        CMyStruct* pMyStruct = m_myPtrList.GetNext( pos );
    }

    ==================================================
    Find
    ==================================================
    pos = m_myPtrList.Find(pMyStruct);

    ==================================================
    Update
    ==================================================
    m_myPtrList.SetAt(pos, pMyStruct);

    ==================================================
    Delete
    ==================================================
    m_myPtrList.RemoveAt(pos);

    POSITION pos = m_myPtrList.GetHeadPosition();
    while (pos != NULL)
    {
        delete m_myPtrList.GetNext(pos);
    }
    m_myPtrList.RemoveAll();

    while (!m_myObList.IsEmpty())
    {
        delete m_myObList.GetHead();
        m_myObList.RemoveHead();
    }

    ==================================================
    Serialize
    ==================================================
    nCount = (WORD)m_myPtrList.GetCount();
    if (ar.IsStoring())
    {
        ar << nCount;
        pos = m_myPtrList.GetHeadPosition();
        while (pos != NULL)
        {
            CMyStruct* pMyStruct = m_myPtrList.GetNext(pos);
            w = (WORD)pMyStruct->m_int;
            ar << w;
            ar << pMyStruct->m_float;
            ar << pMyStruct->m_str;
            nCount--;
        }
        ASSERT(nCount == 0);
    }
    else
    {
        ar >> nCount;
        while (nCount-- > 0)
        {
            CMyStruct* pMyStruct = new CMyStruct;
            ar >> w;
            pMyStruct->m_int = w;
            ar >> pMyStruct->m_float;
            ar >> pMyStruct->m_str;
            m_myPtrList.AddTail(pMyStruct);
        }
    }

    m_myObList.Serialize(ar);
    // Note: CMyObject serializes itself

    void CMyObject::Serialize(CArchive& ar)
    {
        CObject::Serialize( ar );
        if( ar.IsStoring() )
            ar << i;
        else
            ar >> i;
    }

Send all flaming replies to: jaharl@hotmail.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Joe Harleman


Member

Location: United States United States

Other popular Trace and Logs articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralInteresting idea PinsussAnonymous4:38 28 Aug '03  
GeneralCarray of CStringArray Pinmembermimosa4:25 11 Aug '03  
GeneralRe: Carray of CStringArray Pinmembermimosa4:28 11 Aug '03  
GeneralA single correction... PinmemberMasoud Samimi0:51 29 Nov '00  
GeneralRe: Oh Oh, Even with me!!?? :-( PinmemberMasoud Samimi0:56 29 Nov '00  
GeneralRe: Oh Oh, Even with me!!?? :-( PinmemberAndy Hassall11:00 29 Nov '00  
GeneralRe: Oh Oh, Even with me!!?? :-( PinmemberAndy Hassall11:08 29 Nov '00  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Nov 2000
Editor: Smitha Vijayan
Copyright 2000 by Joe Harleman
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project