CSimpleArray Class

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

This class provides methods for managing a simple array.

template <class T, class TEqual = CSimpleArrayEqualHelper<T>>  
class CSimpleArray

Parameters

T
The type of data to store in the array.

TEqual
A trait object, defining the equality test for elements of type T.

Public Constructors

NameDescription
CSimpleArray::CSimpleArrayThe constructor for the simple array.
CSimpleArray::~CSimpleArrayThe destructor for the simple array.

Public Methods

NameDescription
CSimpleArray::AddAdds a new element to the array.
CSimpleArray::FindFinds an element in the array.
CSimpleArray::GetDataReturns a pointer to the data stored in the array.
CSimpleArray::GetSizeReturns the number of elements stored in the array.
CSimpleArray::RemoveRemoves a given element from the array.
CSimpleArray::RemoveAllRemoves all elements from the array.
CSimpleArray::RemoveAtRemoves the specified element from the array.
CSimpleArray::SetAtIndexSets the specified element in the array.

Public Operators

NameDescription
CSimpleArray::operatorRetrieves an element from the array.
CSimpleArray::operator =Assignment operator.

CSimpleArray provides methods for creating and managing a simple array, of any given type T.

The parameter TEqual provides a means of defining an equality function for two elements of type T. By creating a class similar to CSimpleArrayEqualHelper, it is possible to alter the behavior of the equality test for any given array. For example, when dealing with an array of pointers, it may be useful to define the equality as depending on the values the pointers reference. The default implementation utilizes operator=().

Both CSimpleArray and CSimpleMap are designed for a small number of elements. CAtlArray and CAtlMap should be used when the array contains a large number of elements.

Header: atlsimpcoll.h

   // Create an array of integers
   CSimpleArray<int> iArray;

   // Create an array of char pointers
   // and use a new equality function
   CSimpleArray<char *, MyEqualityEqualHelper<char *> > cMyArray;   

Adds a new element to the array.

BOOL Add(const T& t);

Parameters

t
The element to add to the array.

Return Value

Returns TRUE if the element is successfully added to the array, FALSE otherwise.

Example

   // Create an array of integers and add some elements
   CSimpleArray<int> iMyArray;
   for (int i = 0; i < 10; i++)
      iMyArray.Add(i);  

The constructor for the array object.

CSimpleArray(const CSimpleArray<T, TEqual>& src);  
CSimpleArray();

Parameters

src
An existing CSimpleArray object.

Remarks

Initializes the data members, creating a new empty CSimpleArray object, or a copy of an existing CSimpleArray object.

The destructor.

~CSimpleArray();

Remarks

Frees all allocated resources.

Finds an element in the array.

int Find(const T& t) const;

Parameters

t
The element for which to search.

Return Value

Returns the index of the found element, or -1 if the element is not found.

Example

   // Create an array of floats and search for a particular element

   CSimpleArray<float> fMyArray;

   for (int i = 0; i < 10; i++)
      fMyArray.Add((float)i * 100);

   int e = fMyArray.Find(200);
   if (e == -1)
      _tprintf_s(_T("Could not find element\n"));
   else
      _tprintf_s(_T("Found the element at location %d\n"), e);   

Returns a pointer to the data stored in the array.

T* GetData() const;

Return Value

Returns a pointer to the data in the array.

Returns the number of elements stored in the array.

int GetSize() const;

Return Value

Returns the number of elements stored in the array.

Retrieves an element from the array.

T& operator[](int   nIndex);

Parameters

nIndex
The element index.

Return Value

Returns the element of the array referenced by nIndex.

Example

   // Create an array and display its contents
    CSimpleArray<int> iMySampleArray;

    for (int i = 0; i < 10; i++)
       iMySampleArray.Add(i);

    for (int i = 0; i < iMySampleArray.GetSize(); i++)
       _tprintf_s(_T("Array index %d contains %d\n"), i, iMySampleArray[i]);

Assignment operator.

CSimpleArray<T, TEqual>
& operator=(
    const CSimpleArray<T, TEqual>& src);

Parameters

src
The array to copy.

Return Value

Returns a pointer to the updated CSimpleArray object.

Remarks

Copies all elements from the CSimpleArray object referenced by src into the current array object, replacing all existing data.

Example

   // Create an array of chars and copy it to a second array
   CSimpleArray<char> cMyArray1;
   cMyArray1.Add('a');
   CSimpleArray<char> cMyArray2;
   cMyArray2 = cMyArray1;
   ATLASSERT(cMyArray2[0] == 'a');   

Removes a given element from the array.

BOOL Remove(const T& t);

Parameters

t
The element to remove from the array.

Return Value

Returns TRUE if the element is found and removed, FALSE otherwise.

Remarks

When an element is removed, the remaining elements in the array are renumbered to fill the empty space.

Removes all elements from the array.

void RemoveAll();

Remarks

Removes all elements currently stored in the array.

Removes the specified element from the array.

BOOL RemoveAt(int nIndex);

Parameters

nIndex
Index pointing to the element to remove.

Return Value

Returns TRUE if the element was removed, FALSE if the index was invalid.

Remarks

When an element is removed, the remaining elements in the array are renumbered to fill the empty space.

Set the specified element in the array.

BOOL SetAtIndex(
    int nIndex,
    const T& t);

Parameters

nIndex
The index of the element to change.

t
The value to assign to the specified element.

Return Value

Returns TRUE if successful, FALSE if the index was not valid.

Class Overview

Show: