共用方式為


hash_map::operator[] (STL/CLR)

將索引鍵對應到它的關聯對應值。

    mapped_type operator[](key_type key);

參數

  • 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 >

Namespace: cliext

請參閱

參考

hash_map (STL/CLR)

hash_map::find (STL/CLR)

hash_map::insert (STL/CLR)