mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 03:24:24 +08:00
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
46 lines
1.4 KiB
C++
46 lines
1.4 KiB
C++
/**
|
|
* File: hash_map.cpp
|
|
* Created Time: 2022-12-14
|
|
* Author: msk397 (machangxinq@gmail.com)
|
|
*/
|
|
|
|
#include "../utils/common.hpp"
|
|
|
|
/* ドライバーコード */
|
|
int main() {
|
|
/* ハッシュテーブルを初期化 */
|
|
unordered_map<int, string> map;
|
|
|
|
/* 追加操作 */
|
|
// キー値ペア(key, value)をハッシュテーブルに追加
|
|
map[12836] = "Ha";
|
|
map[15937] = "Luo";
|
|
map[16750] = "Suan";
|
|
map[13276] = "Fa";
|
|
map[10583] = "Ya";
|
|
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
|
printHashMap(map);
|
|
|
|
/* クエリ操作 */
|
|
// ハッシュテーブルにキーを入力、値を取得
|
|
string name = map[15937];
|
|
cout << "\nEnter student ID 15937, found name " << name << endl;
|
|
|
|
/* 削除操作 */
|
|
// ハッシュテーブルからキー値ペア(key, value)を削除
|
|
map.erase(10583);
|
|
cout << "\nAfter removing 10583, the hash table is\nKey -> Value" << endl;
|
|
printHashMap(map);
|
|
|
|
/* ハッシュテーブルを走査 */
|
|
cout << "\nTraverse key-value pairs Key->Value" << endl;
|
|
for (auto kv : map) {
|
|
cout << kv.first << " -> " << kv.second << endl;
|
|
}
|
|
cout << "\nIterate through Key->Value using an iterator" << endl;
|
|
for (auto iter = map.begin(); iter != map.end(); iter++) {
|
|
cout << iter->first << "->" << iter->second << endl;
|
|
}
|
|
|
|
return 0;
|
|
} |