Map operators

提供:cppreference.com
移動: 案内, 検索

Syntax:

    #include <map>
    mapped_type& operator[]( const key_type& key );
    map& operator=(const map& c2);
    bool operator==(const map& c1, const map& c2);
    bool operator!=(const map& c1, const map& c2);
    bool operator<(const map& c1, const map& c2);
    bool operator>(const map& c1, const map& c2);
    bool operator<=(const map& c1, const map& c2);
    bool operator>=(const map& c1, const map& c2);

マップを比較することができる、標準的な比較演算子で割り当てられているのは、 "==, !=, <=, >=, <, > および=です。 マップの個々の要素を検査することができる演算子です。

1つのマップに対する、比較・代入は線形的に時間がかかります。

2つのマップが等しい場合では、

  1. 2つの大きさは同じです。
  2. 1つのマップの"i"のメンバは、別のマップの"i"のメンバと等しいです。

マップ間の比較は、辞書形式で行われます。

たとえば、次のコードでは、[]演算子を使用して、変数に文字列と整数の要素を定義しています。

    struct strCmp {
      bool operator()( const char* s1, const char* s2 ) const {
        return strcmp( s1, s2 ) < 0;
      }
    };
 
    map<const char*, int, strCmp> ages;
    ages["Homer"] = 38;
    ages["Marge"] = 37;
    ages["Lisa"] = 8;
    ages["Maggie"] = 1;
    ages["Bart"] = 11;
 
    cout << "Bart is " << ages["Bart"] << " years old" << endl;
 
    cout << "In alphabetical order: " << endl;
    for( map<const char*, int, strCmp>::iterator iter = ages.begin(); iter != ages.end(); ++iter ) {
      cout << (*iter).first << " is " << (*iter).second << " years old" << endl;
    }

実行時に、上記のコードは、次の出力が表示されます。

    Bart is 11 years old
    In alphabetical order:
    Bart is 11 years old
    Homer is 38 years old
    Lisa is 8 years old
    Maggie is 1 years old
    Marge is 37 years old

Related Topics: insert, map_constructors

個人用ツール
名前空間
変種
操作
案内
ツールボックス
他の言語