mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +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:
		@ -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;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user