Extend collections






3.27/5 (7 votes)
Dec 12, 1999

88498

1581
Extended Collection classes to provide copy, compare and find operations with 2 dimensional arrays and maps
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 CString
s, 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 CArrayExCIntArray; 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> > a; CArray ,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