CMapStringToOb::SetAt
The primary means to insert an element in a map.
void SetAt(
LPCTSTR key,
CObject* newValue
);
Параметры
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.
Заметки
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 |
---|---|
void SetAt( void* key, void* newValue ); |
|
void SetAt( void* key, WORD newValue ); |
|
void SetAt( LPCTSTR key, void* newValue ); |
|
void SetAt( LPCTSTR key, LPCTSTR newValue ); |
|
void SetAt( WORD key, CObject* newValue ); |
|
void SetAt( WORD key, void* newValue ); |
Пример
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
Требования
Header: afxcoll.h