共用方式為


CMapStringToOb::SetAt

The primary means to insert an element in a map.

void SetAt(
   LPCTSTR key,
   CObject* newValue 
);

Parameters

  • key
    Specifies the string that is the key of the new element.

  • newValue
    Specifies the CObject pointer that is the value of the new element.

Remarks

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.

The following table shows other member functions that are similar to CMapStringToOb::SetAt.

Class

Member Function

CMapPtrToPtr

void SetAt( void* key, void* newValue );

CMapPtrToWord

void SetAt( void* key, WORD newValue );

CMapStringToPtr

void SetAt( LPCTSTR key, void* newValue );

CMapStringToString

void SetAt( LPCTSTR key, LPCTSTR newValue );

CMapWordToOb

void SetAt( WORD key, CObject* newValue );

CMapWordToPtr

void SetAt( WORD key, void* newValue );

Example

See CObList::CObList for a listing of the CAge class used in all collection examples.

CMapStringToOb map;
CAge* pa;

map.SetAt(_T("Bart"), new CAge(13));
map.SetAt(_T("Lisa"), new CAge(11)); // Map contains 2 
                                        // elements.
#ifdef _DEBUG
   afxDump.SetDepth(1);
   afxDump << _T("before Lisa's birthday: ") << &map << _T("\n");
#endif
if (map.Lookup(_T("Lisa"), (CObject *&)pa))
{ // CAge 12 pointer replaces CAge 11 pointer.
   map.SetAt(_T("Lisa"), new CAge(12));
   delete pa;  // Must delete CAge 11 to avoid memory leak.
}
#ifdef _DEBUG
   afxDump << _T("after Lisa's birthday: ") << &map << _T("\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

Requirements

Header: afxcoll.h

See Also

Concepts

CMapStringToOb Class

CMapStringToOb Members

Hierarchy Chart

CMapStringToOb::Lookup

CMapStringToOb::operator [ ]