パープルハット

※当サイトではGoogleアドセンス広告を利用しています

C++ mapの使い方(追加、for文、find、erase、clear)



宣言

  • mapを使うには「#include <map>」が必要です。
  • 宣言時に、KeyとValueの型を宣言する必要があります(例ではKeyがstring、Valueがint)。
#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> mp;
}





追加と表示

mp[Key] = Value
とすれば、map型変数mpに対して

  • Keyが存在しない:新規作成
  • Keyが既に存在:上書き

を行ってくれます。

ソースコード

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> mp;

    mp["C"] = 5;
    mp["B"] = 18;
    mp["A"] = 4;
    mp["D"] = 6;

    std::cout << "key = A, Value = " << mp["A"] << std::endl;
    std::cout << "key = B, Value = " << mp["B"] << std::endl;
    std::cout << "key = C, Value = " << mp["C"] << std::endl;
    std::cout << "key = D, Value = " << mp["D"] << std::endl;
}

実行結果

key = A, Value = 4
key = B, Value = 18
key = C, Value = 5
key = D, Value = 6





for文を使って全要素を表示

  • for文を使ってループ処理が可能です。
  • Keyの値で自動的に整列されています。

ソースコード

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> mp;

    mp["C"] = 5;
    mp["B"] = 18;
    mp["A"] = 4;
    mp["D"] = 6;

    for (auto ite = mp.begin(); ite != mp.end(); ite++) {
        std::cout << "Key = " << ite->first << ", Value = " << ite->second << std::endl;
    }
}

実行結果

Key = A, Value = 4
Key = B, Value = 18
Key = C, Value = 5
Key = D, Value = 6





要素数の表示(size)

  • vectorなどと同様にmp.size()とすることで要素数が表示できます。

ソースコード

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> mp;

    mp["C"] = 5;
    mp["B"] = 18;
    mp["A"] = 4;
    mp["D"] = 6;

    std::cout << "mp.size() = " << mp.size() << std::endl;
}

実行結果

mp.size() = 4





Keyの存在を検索(find)

mp.find(Key) != mp.end()は、

  • Keyがある⇒bool型のtrue
  • Keyがない⇒bool型のfalse

なので、Keyの存在をチェックできます。

ソースコード

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> mp;

    mp["C"] = 5;
    mp["A"] = 4;

    //KeyにAがあるか 
    std::map<std::string, int>::iterator  ite = mp.find("A");
    if (ite != mp.end()) {
        std::cout << "Key = Aが存在" << std::endl;
    }
    else {
        std::cout << "Key = Aはない" << std::endl;
    }

    //KeyにWがあるか 
    if (mp.find("W") != mp.end()) {
        std::cout << "Key = Wが存在" << std::endl;
    }
    else {
        std::cout << "Key = Wはない" << std::endl;
    }
}

実行結果

Key = Aが存在
Key = Wはない





指定した要素の削除(erase)

iterator指定でも、Key指定のどちらでも可。

ソースコード

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> mp;

    mp["C"] = 5;
    mp["B"] = 18;
    mp["A"] = 4;
    mp["D"] = 4;

    std::cout << "Aを削除して表示" << std::endl;
    mp.erase("A");
    for (auto ite = mp.begin(); ite != mp.end(); ite++) {
        std::cout << "Key = " << ite->first << ", Value = " << ite->second << std::endl;
    }
    std::cout << std::endl;


    std::cout << "Cを削除して表示" << std::endl;
    std::map<std::string, int>::iterator  ite = mp.find("C");
    mp.erase(ite);
    for (auto ite = mp.begin(); ite != mp.end(); ite++) {
        std::cout << "Key = " << ite->first << ", Value = " << ite->second << std::endl;
    }
    std::cout << std::endl;
}

実行結果

Aを削除して表示
Key = B, Value = 18
Key = C, Value = 5
Key = D, Value = 4

Cを削除して表示
Key = B, Value = 18
Key = D, Value = 4





空にする(clear)

  • mp.clear()で空にできる。

ソースコード

#include <iostream>
#include <map>

int main() {
    std::map<std::string, int> mp;

    mp["C"] = 5;
    mp["B"] = 18;

    mp.clear();
    std::cout << "clear後のsize = " << mp.size() << std::endl;
}

実行結果

clear後のsize = 0