Click here to Skip to main content
Licence 
First Posted 11 Dec 1999
Views 69,955
Bookmarked 24 times

Extend collections

By | 11 Dec 1999 | Article
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;
    
    TRACE(_T("Create\n"));
    
    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("Append\n"));
    a.Append(b);
    
    TRACE(_T("Remove all\n"));
    b.RemoveAll();
    

    The classes provided with this article are as follows:

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

    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

    Audrius Vasiliauskas

    Business Analyst

    Lithuania Lithuania

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralLicense Rights PinmemberJohn McNeill6:31 2 Dec '10  
    GeneralRe: License Rights PinmemberAudrius Vasiliauskas7:15 2 Dec '10  
    GeneralRe: License Rights PinmemberJohn McNeill7:17 2 Dec '10  
    Generallicense Pinmemberyhing6:51 24 Aug '09  
    GeneralRe: license PinmemberAudrius Vasiliauskas21:56 24 Aug '09  
    GeneralProblem with unicode Pinmemberbitkidoku2:04 13 Dec '06  
    GeneralVC 7.1 error C2679 PinmemberHobbitCoder13:50 25 Oct '05  
    General.NET Pinmemberdavedrums4:01 7 Sep '04  
    GeneralRe: .NET PinmemberAudrius Vasiliauskas0:37 13 Sep '04  
    GeneralRemoveAt PinmemberPhilTordoff4:12 18 Mar '03  
    GeneralRe: RemoveAt PinmemberAudrius Vasiliauskas4:28 18 Mar '03  
    QuestionSetSize missing? PinmemberAnonymous2:20 9 May '01  

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

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

    Permalink | Advertise | Privacy | Mobile
    Web03 | 2.5.120517.1 | Last Updated 12 Dec 1999
    Article Copyright 1999 by Audrius Vasiliauskas
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid