hash_map::operator[] (STL/CLR)
映射到键映射及其关联的值。
mapped_type operator[](key_type key);
参数
- 键
要搜索的键值。
备注
成员函数、查找具有等效的元素顺序为 key。 如果找到了一个对象,它返回关联映射值;否则,其插入 value_type(key, mapped_type()) 并返回关联 (默认) 映射值。 使用它查找提供的映射的值关联的键,或者确保输入的键存在,如果没有找到。
示例
// cliext_hash_map_operator_sub.cpp
// compile with: /clr
#include <cliext/hash_map>
typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
{
Myhash_map c1;
c1.insert(Myhash_map::make_value(L'a', 1));
c1.insert(Myhash_map::make_value(L'b', 2));
c1.insert(Myhash_map::make_value(L'c', 3));
// display contents " [a 1] [b 2] [c 3]"
for each (Myhash_map::value_type elem in c1)
System::Console::Write(" [{0} {1}]", elem->first, elem->second);
System::Console::WriteLine();
System::Console::WriteLine("c1[{0}] = {1}",
L'A', c1[L'A']);
System::Console::WriteLine("c1[{0}] = {1}",
L'b', c1[L'b']);
// redisplay altered contents
for each (Myhash_map::value_type elem in c1)
System::Console::Write(" [{0} {1}]", elem->first, elem->second);
System::Console::WriteLine();
// alter mapped values and redisplay
c1[L'A'] = 10;
c1[L'c'] = 13;
for each (Myhash_map::value_type elem in c1)
System::Console::Write(" [{0} {1}]", elem->first, elem->second);
System::Console::WriteLine();
return (0);
}
要求
标头: <cliext/hash_map>
命名空间: cliext