Enhanced collection template classes: CArrayEx, CMapEx and CSortedArray | CodeGuru

Enhanced collection template classes: CArrayEx, CMapEx and CSortedArray

Environment: WinNT, VC 6.0 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Dec 7, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: WinNT, VC 6.0

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.

Motivation for CArrayEx, CMapEx

Many of us have tried to create a 2 dimensional array using MFC’s standard
CArray class, only to encounter the
following error:


error C2582: 'operator =' function is unavailable

Example 1:

  typedef CArray <int,int> CIntArray;
  CArray<CIntArray, CIntArray &> a2D;
  CIntArray aInt;
  a2D.Add (aInt);   // error C2582 ****

Example 2:

  typedef CMap<int,int,int,int> CIntMap;
  CMap<int,int,CIntMap,CIntMap &> m2D;
  CIntMap mInt;
  m2D.SetAt (1, mInt);   // error C2582 ****

The error occurs because CArray doesn’t have copy a
constructor and an assignment operator.  The need for these is obvious.  If we
create array of pointers [CArray<void,void>],
we need to copy not only the pointer, but also the values.  Microsoft refers us to
their knowledge base articles “Collection Class Helpers” and “Collections: How to Make a
Type-Safe Collection” for more information.

The problem is solved using CArrayEx:

  typedef CArrayEx<int,int> CIntArray;
  CArrayEx<CIntArray, CIntArray &> a2D;
  CIntArray aInt;
  a2D.Add (aInt);    // OK

and by using CMapEx:

  typedef CMapEx<int,int,int,int> CIntMap;
  CMapEx<int,int,CIntMap,CIntMap &> m2D;
  CIntMap mInt;
  m2D.SetAt (1, mInt);    // OK

Another motivation for CMapEx

If you’ve tried to create a mapping (using CMap)
with a CString key, you’d have encountered the following error:

  CMap<CString,CString,int,int> a;
  a.SetAt (CString(“a”), 1);
  // error: cannot convert fromclass CStringto ‘unsigned long’

This happens because there’s no function to hash a CString to an unsigned long. 
CMapEx addresses this problem by providing a
HashKey() function.

  template<> inline UINT AFXAPI HashKey<CString> (CString strKey)
  {
    LPCSTR key = strKey;
    UINT nHash = 0;
    while (*key)
          nHash = (nHash<<5) + nHash + *key++;
    return nHash;
  }

Motivation for CSortedArray

CSortedArray allows you to easily find data (by
doing a binary search) because it maintains itself in sorted order.

Downloads

Download demo project – 17 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.