Add C++ iterator example for C++ (#837)

* 增加c++迭代器访问示例

* Update hash_map.md

* Update hash_map.cpp

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
52coder 2023-10-24 23:59:10 +08:00 committed by GitHub
parent 2035aa0cf1
commit 436b6fa9a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 16 deletions

View file

@ -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;

View file

@ -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;
}
```