Breyta

Deila með


CSimpleArray Class

This class provides methods for managing a simple array.

Syntax

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.

Members

Public Constructors

Name Description
CSimpleArray::CSimpleArray The constructor for the simple array.
CSimpleArray::~CSimpleArray The destructor for the simple array.

Public Methods

Name Description
CSimpleArray::Add Adds a new element to the array.
CSimpleArray::Find Finds an element in the array.
CSimpleArray::GetData Returns a pointer to the data stored in the array.
CSimpleArray::GetSize Returns the number of elements stored in the array.
CSimpleArray::Remove Removes a given element from the array.
CSimpleArray::RemoveAll Removes all elements from the array.
CSimpleArray::RemoveAt Removes the specified element from the array.
CSimpleArray::SetAtIndex Sets the specified element in the array.

Public Operators

Name Description
CSimpleArray::operator[] Retrieves an element from the array.
CSimpleArray::operator = Assignment operator.

Remarks

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.

Requirements

Header: atlsimpcoll.h

Example

// 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;   

CSimpleArray::Add

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);  

CSimpleArray::CSimpleArray

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.

CSimpleArray::~CSimpleArray

The destructor.

~CSimpleArray();

Remarks

Frees all allocated resources.

CSimpleArray::Find

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);   

CSimpleArray::GetData

Returns a pointer to the data stored in the array.

T* GetData() const;

Return Value

Returns a pointer to the data in the array.

CSimpleArray::GetSize

Returns the number of elements stored in the array.

int GetSize() const;

Return Value

Returns the number of elements stored in the array.

CSimpleArray::operator []

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]);

CSimpleArray::operator =

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');   

CSimpleArray::Remove

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.

CSimpleArray::RemoveAll

Removes all elements from the array.

void RemoveAll();

Remarks

Removes all elements currently stored in the array.

CSimpleArray::RemoveAt

Removes the specified element from the array.

BOOL RemoveAtint 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.

CSimpleArray::SetAtIndex

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.

See also

Class Overview