2.2.7.1.8 Array::SetAt

Description

Adds a reference to the passed object newElement, at the indicated index nIndex.

Syntax

BOOL SetAt( int nIndex, _TemplType newElement )

Parameters

nIndex
[input] The array index where the reference of the object needs to be added
newElement
[input] The reference of the new item to be set at nIndex

Return

Returns a boolean indicating success or failure of the requested operation

Examples

EX1

#include <Array.h>
struct PERSON
{
	int nID;
};

void Array_SetAt_ex1()
{
	Array<PERSON&> arp(true);
	int nTotal = 5;
	for ( int ii = 0; ii < nTotal; ii++ )
	{
		PERSON* pp = new PERSON;
		//assume memory allocated successfully.
		pp->nID = ii + 1;
		arp.Add(*pp);
	}
	//get the middle item to change its value.
	int nMid = nTotal/2;
	if ( nMid < nTotal )
	{
		PERSON* pp = new PERSON;
		pp->nID = 12345;
		if ( !arp.SetAt(nMid, *pp) )
			printf("Fail to set the %dth item.", nMid);
	}
	for ( ii = 0; ii < nTotal; ii++ )
		printf("Person %d's ID is : %d\n", ii + 1, arp.GetAt(ii).nID);
		
}

Remark

See Also

Array::GetAt, Array::SetAtGrow

Header to Include

Array.h