Click here to Skip to main content
15,792,380 members
Articles / Desktop Programming / MFC

Extend collections

Rate me:
Please Sign up or sign in to vote.
3.27/5 (7 votes)
11 Dec 1999 87.1K   1.6K   24   12
Extended Collection classes to provide copy, compare and find operations with 2 dimensional arrays and maps
  • Download demo project - 24 Kb
  • Download source files - 10 Kb
  • Sample Image - ExtCol.gif

    This article describes three enhanced collection template classes, CArrayEx, CMapEx and CSortedArray. CArrayEx and CMapEx each provide an assignment operator and class copy constructor that allow us to easily craft two dimensional arrays and mappings. In addition, CMapEx provides the ability to map CStrings, by providing a template hash function. CSortedArray is a dynamic array template class that provides "sort after insertion" functionality.
    // example of 2 dimensional array
    typedef CArrayEx<int,int> CIntArray;
    CArrayEx<CIntArray, CIntArray &> a2D;
    CIntArray aInt;
    a2D.Add (aInt);

    In a lot of cases we need an array with a find function. We can use a sorted array for this.

    To work with arrays of pointers you should use the functions DestructElements, CopyElements as described in the MSDN article 'Collections: How to Make a Type-Safe Collection'], but there exists an alternative way by using auto_ptr:

    // auto_ptr example.
    CArray<auto_ptr<CValue>,auto_ptr<CValue> > a;
    CArray<auto_ptr<CValue>,auto_ptr<CValue> > b;
    a.Add(auto_ptr<CValue>(new CValue(1)));
    a.Add(auto_ptr<CValue>(new CMyValue(2)));
    b.Add(auto_ptr<CValue>(new CValue(3)));
    b.Add(auto_ptr<CValue>(new CMyValue(4)));
    // TRACE(_T("Copy\n"));
    // a.Copy(b);
    TRACE(_T("Remove all\n"));

    The classes provided with this article are as follows:

    Adds Copy constructor, assign operator and Array compare operators
    Adds Copy constructor, assign operator and a method "GetAt" by index. Also adds Hash functions for CString
    Adds Copy constructor and assign operator
    Adds Copy constructor, assign operator and Array compare operators. Also adds Sorting methods by operators or compare functions


    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

    Written By
    Technical Lead Doclogix
    Lithuania Lithuania
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

    GeneralLicense Rights Pin
    John McNeill2-Dec-10 7:31
    John McNeill2-Dec-10 7:31 
    GeneralRe: License Rights Pin
    Audrius Vasiliauskas2-Dec-10 8:15
    Audrius Vasiliauskas2-Dec-10 8:15 
    GeneralRe: License Rights Pin
    John McNeill2-Dec-10 8:17
    John McNeill2-Dec-10 8:17 
    Generallicense Pin
    yhing24-Aug-09 7:51
    yhing24-Aug-09 7:51 
    GeneralRe: license Pin
    Audrius Vasiliauskas24-Aug-09 22:56
    Audrius Vasiliauskas24-Aug-09 22:56 
    GeneralProblem with unicode Pin
    bitkidoku13-Dec-06 3:04
    bitkidoku13-Dec-06 3:04 
    GeneralVC 7.1 error C2679 Pin
    HobbitCoder25-Oct-05 14:50
    HobbitCoder25-Oct-05 14:50 
    Your auto_ptr example does not compile, it gives me:

    C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxtempl.h(64):
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const std::auto_ptr<_Ty>' (or there is no acceptable conversion)

    auto_ptr should not be used in a container. For more information, check here:

    I suggest replacing auto_ptr with boost::shared_ptr.

    #include <boost/smart_ptr.hpp>
    using boost::shared_ptr;

    Your auto_ptr example should look like this:

    CArray< shared_ptr< CValue >, shared_ptr< CValue > > a;
    CArray< shared_ptr< CValue >, shared_ptr< CValue > > b;
    a.Add(shared_ptr< CValue >(new CValue(1)));
    a.Add(shared_ptr< CValue >(new CMyValue(2)));
    b.Add(shared_ptr< CValue >(new CValue(3)));
    b.Add(shared_ptr< CValue >(new CMyValue(4)));

    Now it compiles.
    Interesting article, if a bit short.
    General.NET Pin
    davedrums7-Sep-04 5:01
    davedrums7-Sep-04 5:01 
    GeneralRe: .NET Pin
    Audrius Vasiliauskas13-Sep-04 1:37
    Audrius Vasiliauskas13-Sep-04 1:37 
    GeneralRemoveAt Pin
    PhilTordoff18-Mar-03 5:12
    PhilTordoff18-Mar-03 5:12 
    GeneralRe: RemoveAt Pin
    Audrius Vasiliauskas18-Mar-03 5:28
    Audrius Vasiliauskas18-Mar-03 5:28 
    QuestionSetSize missing? Pin
    9-May-01 3:20
    suss9-May-01 3:20 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.