共用方式為


map::operator (STL/CLR)

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

    mapped_type operator[](key_type key);

參數

  • Key - 索引鍵
    若要搜尋的索引鍵值。

備註

成員函式的努力時,若要尋找具有相同順序來的項目key。如果找到,則會傳回關聯的對應的值。 否則,它會插入value_type(key, mapped_type()) ,並傳回關聯 (預設值) 的對應值。您用它來查閱對應的值,指定關聯的索引鍵,或者如果找不到機碼存在一個項目。

範例

// cliext_map_operator_sub.cpp 
// compile with: /clr 
#include <cliext/map> 
 
typedef cliext::map<wchar_t, int> Mymap; 
int main() 
    { 
    Mymap c1; 
    c1.insert(Mymap::make_value(L'a', 1)); 
    c1.insert(Mymap::make_value(L'b', 2)); 
    c1.insert(Mymap::make_value(L'c', 3)); 
 
// display contents " [a 1] [b 2] [c 3]" 
    for each (Mymap::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 (Mymap::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 (Mymap::value_type elem in c1) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
    return (0); 
    } 
 
  

需求

標頭: < cliext/地圖 >

Namespace: cliext

請參閱

參考

map (STL/CLR)

map::find (STL/CLR)

map::insert (STL/CLR)