mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 20:16:28 +08:00
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:
parent
2035aa0cf1
commit
436b6fa9a6
2 changed files with 6 additions and 16 deletions
|
@ -37,15 +37,9 @@ int main() {
|
||||||
for (auto kv : map) {
|
for (auto kv : map) {
|
||||||
cout << kv.first << " -> " << kv.second << endl;
|
cout << kv.first << " -> " << kv.second << endl;
|
||||||
}
|
}
|
||||||
|
cout << "\n使用迭代器遍历 Key->Value" << endl;
|
||||||
cout << "\n单独遍历键 Key" << endl;
|
for (auto iter = map.begin(); iter != map.end(); iter++) {
|
||||||
for (auto kv : map) {
|
cout << iter->first << "->" << iter->second << endl;
|
||||||
cout << kv.first << endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
cout << "\n单独遍历值 Value" << endl;
|
|
||||||
for (auto kv : map) {
|
|
||||||
cout << kv.second << endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -298,13 +298,9 @@
|
||||||
for (auto kv: map) {
|
for (auto kv: map) {
|
||||||
cout << kv.first << " -> " << kv.second << endl;
|
cout << kv.first << " -> " << kv.second << endl;
|
||||||
}
|
}
|
||||||
// 单独遍历键 key
|
// 使用迭代器遍历 key->value
|
||||||
for (auto kv: map) {
|
for (auto iter = map.begin(); iter != map.end(); iter++) {
|
||||||
cout << kv.first << endl;
|
cout << iter->first << "->" << iter->second << endl;
|
||||||
}
|
|
||||||
// 单独遍历值 value
|
|
||||||
for (auto kv: map) {
|
|
||||||
cout << kv.second << endl;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue