CMapStringToOb::SetAt
void SetAt( LPCTSTR key, CObject* newValue );
throw( CMemoryException );
参数:
key | 指定新元素关键码值的字符串。
|
newValue | 指定新元素值的CObject指针。
|
说明:
主要用途是在映射中插入一个元素。首先,查找关键码,如果发现了该关键码,然后改变对应的值;否则,将创建新的关键码值元素。
示例:
请参阅CObList::CObList,了解所有收集示例中使用的CAge类。
//example for CMapStringToOb::SetAt
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", (CObject *&)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
该程序的结果如下:
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
请参阅:CMapStringToOb::Lookup, CMapStringToOb::operator []