Skip to content

Commit

Permalink
Add C++ iterator example for C++ (krahets#837)
Browse files Browse the repository at this point in the history
* 增加c++迭代器访问示例

* Update hash_map.md

* Update hash_map.cpp

---------

Co-authored-by: Yudong Jin <[email protected]>
  • Loading branch information
52coder and krahets authored Oct 24, 2023
1 parent 2035aa0 commit 436b6fa
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
12 changes: 3 additions & 9 deletions codes/cpp/chapter_hashing/hash_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 3 additions & 7 deletions docs/chapter_hashing/hash_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```

Expand Down

0 comments on commit 436b6fa

Please sign in to comment.