unordered_map容器
unordered_mapの概要
高速な検索に使用され、内部はハッシュに基づいており、ハッシュテーブルは内部で実装されているため、要素の順序は無秩序です。
1キー値によって値をすばやく取得します。
2内部で順序付けられていないマップが順序付けされ、キーに従ってソートされます
。3各値は一意のキー値にのみ対応し、キーのハッシュは要素が同じであるかどうかを判断するために使用されます
。4メモリの動的管理
5.頻度の低いクエリの場合、コンテナ要素は1000未満であり、マップは比較的安定しています。
非常に高頻度のクエリ(100を超える要素)の場合、unordered_mapを使用するとマップよりも高速になります
定義と初期化
1つのヘッダーファイル
#include <unordered_map>
2初期化
unoredered_map<const Key, T> map
`unordered_map <int、int> h;
3トラバーサル
イテレーター付きのトラバース
unordered_map<int, int> h;
for (int i = 4; i <= 9; i ++ ) h[i] = i - 4;
unordered_map<int, int>::iterator it;
for (it = h.begin(); it != h.end(); it ++ )
cout << (*it).first << ' ' << (*it).second << endl;
/*
9 5
5 1
4 0
6 2
7 3
8 4
*/
オートでも使いやすく、同じ効果が得られます
for (auto it : h) cout << it.first << ' ' << it.second << endl;
配列で繰り返す
for (int i = 0; i <= 9; i ++ ) cout << h[i] << ' ' << endl;
/*
0 0 0 0 1 2 3 4 5
*/
イテレータ
begin()
end()
関数
size()は要素の数を返します
cout << h.size() << endl;
要素を挿入
unordered_map<int, int> h, h1;
h.insert(unordered_map<int, int>::value_type(4, 1));
h.insert(unordered_map<int, int>::value_type(5, 2));
h.insert(unordered_map<int, int>::value_type(6, 3));
h1.insert(h.begin(), h.end());
for (auto it : h) cout << it.first << ' ' << it.second << endl;
for (auto it : h1) cout <<it.first<<' '<<it.second<< endl;
/*
6 3
4 1
5 2
5 2
4 1
6 3
*/
消す
for (int i = 4; i <= 7; i ++ ) h[i] = i - 4;
h.erase(5);
for (auto it : h) cout << it.first << ' ' << it.second << endl;
/*
7 3
6 2
4 0
*/
空の
h.erase(h.begin(), h.end());
スワップ
晴れ
h.clear();
据える
見つける
if (h.find(5) != h.end()) puts("yes");
else puts("no");
カウント
cout << h.count(5) << endl;//1
cout << h.count(7) << endl;//0