From 436b6fa9a6e1307a7e8b6931044a0183708fd005 Mon Sep 17 00:00:00 2001 From: 52coder Date: Tue, 24 Oct 2023 23:59:10 +0800 Subject: [PATCH] Add C++ iterator example for C++ (#837) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加c++迭代器访问示例 * Update hash_map.md * Update hash_map.cpp --------- Co-authored-by: Yudong Jin --- codes/cpp/chapter_hashing/hash_map.cpp | 12 +++--------- docs/chapter_hashing/hash_map.md | 10 +++------- 2 files changed, 6 insertions(+), 16 deletions(-) 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; } ```