CMapStringToOb::SetAt

Syntax

void SetAt( const char* key, CObject* newValue )
throw( CMemoryException );

Parameters

key

The string that is the key of the new element.

newValue

The CObject pointer that is the value of the new element.

Remarks

The primary means to insert an element in a map. First, the key is looked up. If the key is found, then the corresponding value is changed; otherwise, a new key-value element is created.

Example

CMapStringToOb map;

CAge* pa;

map.SetAt( "Bart", new CAge( 13 ) );

map.SetAt( "Lisa", new CAge( 11 ) ); // Map contains 2 elements

#ifdef _DEBUG

afxDump.SetDepth( 1 );

afxDump << "before Lisa's birthday: " << &map << "\\n";

#endif

if( map.Lookup( "Lisa", pa ) )

{ // CAge 12 pointer replaces CAge 11 pointer

map.SetAt( "Lisa", new CAge( 12 ) );

delete pa; // Must delete CAge 11 to avoid memory leak

}

#ifdef _DEBUG

afxDump << "after Lisa's birthday: " << &map << "\\n";

#endif

The results from this program are as follows:

before Lisa's birthday: A CMapStringToOb with 2 elements

[Lisa] = a CAge at $493C 11

[Bart] = a CAge at $4654 13

after Lisa's birthday: A CMapStringToOb with 2 elements

[Lisa] = a CAge at $49C0 12

[Bart] = a CAge at $4654 13

See Also

CMapStringToOb::Lookup, CMapStringToOb::operator []