# Traversing a map (or unordered_map) in C++ STL

We can traverse map and unordered_map using following different ways.

Using a range based for loop

 `// CPP program to traverse a map using range  ` `// based for loop ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``map<``int``, ``int``> m; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``m[arr[i]]++; ` ` `  `    ``cout << ``"Element  Frequency"` `<< endl; ` `    ``for` `(``auto` `i : m) ` `        ``cout << i.first << ``"   "` `<< i.second  ` `             ``<< endl; ` ` `  `    ``return` `0; ` `} `

 `// CPP program to traverse a unordered_map using  ` `// range based for loop ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``unordered_map<``int``, ``int``> m; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``m[arr[i]]++; ` ` `  `    ``cout << ``"Element Frequency"` `<< endl; ` `    ``for` `(``auto` `i : m) ` `        ``cout << i.first << ``"    "` `<< i.second ` `             ``<< endl; ` ` `  `    ``return` `0; ` `} `

Output [NOTE: For unordered_map output rows can be
in any order]
```Element  Frequency
1        4
2        1
3        2
4        1
```

Traversing using begin() and end()

 `// CPP program to traverse a map using iterators ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``map<``int``, ``int``> m; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``m[arr[i]]++; ` ` `  `    ``cout << ``" Element Frequency"` `<< endl; ` `    ``for` `(``auto` `i = m.begin(); i != m.end(); i++) ` `        ``cout << i->first << ``"      "` `<< i->second ` `             ``<< endl; ` ` `  `    ``return` `0; ` `} `

 `// CPP program to traverse a unordered_map  ` `// using iterators ` `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `arr[] = { 1, 1, 2, 1, 1, 3, 4, 3 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` ` `  `    ``unordered_map<``int``, ``int``> m; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``m[arr[i]]++; ` ` `  `    ``cout << ``" Element Frequency"` `<< endl; ` `    ``for` `(``auto` `i = m.begin(); i != m.end(); i++) ` `        ``cout << i->first << ``"       "` `<< i->second  ` `             ``<< endl; ` ` `  `    ``return` `0; ` `} `

Output [NOTE: For unordered_map output rows can be
in any order]
```Element  Frequency
1        4
2        1
3        2
4        1
```

This article is contributed by Kartik. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.