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

请参见

参考

hash_map (STL/CLR)

hash_map::find (STL/CLR)

hash_map::insert (STL/CLR)