std::mapは、C++の標準テンプレートライブラリ(STL)に含まれるキーと値のペアを管理する連想コンテナです。 通常、内部的にはバランス木が使用されているため、キーに対する挿入、検索、削除がO(log n)で行えます。 要素はキーによってソートされて保持されます
#include <map> // for std::map
#include <algorithm> // for std::for_each
using std::map;
std::map<int, std::string> m; // 空のmap
std::map<int, std::string> m = {
{1, "one"},
{2, "two"}
}; // 初期値を指定
// 2次元連想配列(mapの入れ子)
std::map<int, std::map<std::string, int>> map2D; // 空の2次元map
std::map<int, std::map<std::string, int>> map2D = { // 初期値を指定
{1, {{"apple", 100}, {"banana", 200}}},
{2, {{"apple", 150}}}
};
// 参照による子mapへのアクセス
std::map<std::string, int>& refMap = map2D[1];
C++11以降、=はオプションなので省略可能。以下の二つは等価です。
std::map<int, int> mapData = {{1,1}, {2,2}, {3,3}};
std::mao<int, int> mapData{{1,1}, {2,2}, {3,3}};
m[1] = "one"; // 添字でアクセス(キーが存在しない場合は新規に作成される)
m.at(1) = "one"; // atメソッドでアクセス(キーが存在しない場合は例外が投げられる)
添字によるアクセスで、存在しないキーを指定しても要素が追加される動作となり、例外は発生しません
m.insert({3, "three"}); // 要素を追加
m.erase(3); // キーが3の要素を削除
auto it = m.find(1); // キーが1の要素を検索
if(it != m.end()) {
// 見つかった
}
キーでソートされた順番で要素が参照されます
// 通常のforループ
for(auto it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << ": " << it->second << std::endl;
}
// 範囲forループ(C++17)
for(const auto& [key, value] : m) {
std::cout << key << ": " << value << std::endl;
}
size_t size = m.size(); // 要素の数
bool empty = m.empty(); // 空かどうか
m.clear(); // 全要素を削除
// 全要素をコピー
anotherMap = map;
// 他のマップ(anotherMap)の全要素を後ろに追加
map.insert(anotherMap.begin(), anotherMap.end());
// マップの入れ替え
map1.swap(map2);
// 初期値付きで宣言
std::map<int, std::map<std::string, int>> map2D = {
{1, {{"apple", 100}, {"banana", 200}}},
{2, {{"apple", 150}}}
};
// 要素の書き換え(追加も同じ)
map2D[1]["apple"] = 1100;
map2D[1]["banana"] = 1200;
map2D[2]["apple"] = 1150;
// 要素へのアクセス
std::cout << map2D[1]["apple"] << std::endl; // 出力: 100
std::cout << map2D[1]["banana"] << std::endl; // 出力: 200
std::cout << map2D[2]["apple"] << std::endl; // 出力: 150
// mapの参照
std::map<std::string, int>& refMap = map2D[1];
ソート保持のために、キーの方には比較オペレーターが必須となっている
しかし、カスタム比較関数を提供することで、operator<を持たない型でもキーとして扱うことが可能
// カスタム比較関数
struct MyCompare {
bool operator()(const MyType& a, const MyType& b) const {
// ここでaとbを比較するロジックを記述
// 引数aが引数bより小さい場合にtrueを返すように記述する
}
};
std::map<MyType, ValueType, MyCompare> myMap;