diff --git a/codes/cpp/chapter_hashing/hash_map.cpp b/codes/cpp/chapter_hashing/hash_map.cpp index 090c2eb10..22d64e991 100644 --- a/codes/cpp/chapter_hashing/hash_map.cpp +++ b/codes/cpp/chapter_hashing/hash_map.cpp @@ -37,15 +37,9 @@ int main() { for (auto kv : map) { cout << kv.first << " -> " << kv.second << endl; } - - cout << "\n单独遍历键 Key" << endl; - for (auto kv : map) { - cout << kv.first << endl; - } - - cout << "\n单独遍历值 Value" << endl; - for (auto kv : map) { - cout << kv.second << endl; + cout << "\n使用迭代器遍历 Key->Value" << endl; + for (auto iter = map.begin(); iter != map.end(); iter++) { + cout << iter->first << "->" << iter->second << endl; } return 0; diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 666ae508e..061f8a29a 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -298,13 +298,9 @@ for (auto kv: map) { cout << kv.first << " -> " << kv.second << endl; } - // 单独遍历键 key - for (auto kv: map) { - cout << kv.first << endl; - } - // 单独遍历值 value - for (auto kv: map) { - cout << kv.second << endl; + // 使用迭代器遍历 key->value + for (auto iter = map.begin(); iter != map.end(); iter++) { + cout << iter->first << "->" << iter->second << endl; } ```