unordered_map begin() in C++
The unordered_map::begin() is a built-in function in C++ STL which returns an iterator pointing to the first element in the unordered_map container or in any of its bucket.
- Syntax for first element in unordered_map container:
unordered_map.begin()
Parameters: This function does not accepts any parameters.
Return Value: The function returns an iterator pointing to the first element in the unordered_map container.
Note: In an unordered map, there is no specific element which is considered as the first element.
Below program illustrate the above function.
// CPP program to demonstrate the// unordered_map::begin() function// when first element of the container// is to be returned as iterator#include <bits/stdc++.h>usingnamespacestd;intmain(){// Declarationunordered_map<std::string, std::string> mymap;// Initilisationmymap = { {"Australia","Canberra"},{"U.S.","Washington"},{"France","Paris"} };// Iterator pointing to the first element// in the unordered mapautoit = mymap.begin();// Prints the elements of the first element in mapcout << it->first <<" "<< it->second;return0;}chevron_rightfilter_noneOutput:France Paris
- Syntax for first element in unordered_map bucket:
unordered_map.begin( n )
Parameters: The function accepts one mandatory parameter n which specifies the bucket number whose first element’s iterator is to be returned.
Return Value: The function returns an iterator pointing to the first element in the n-th bucket.
Below program illustrate the above function.
// CPP program to demonstrate the// unordered_map::begin() function// when first element of n-th container// is to be returned as iterator#include <bits/stdc++.h>usingnamespacestd;intmain(){// Declarationunordered_map<std::string, std::string> mymap;// Initilisationmymap = { {"Australia","Canberra"},{"U.S.","Washington"}, {"France","Paris"} };// Iterator pointing to the first element// in the n-th bucketautoit = mymap.begin(0);// Prints the elements of the n-th bucketcout << it->first <<" "<< it->second;return0;}chevron_rightfilter_noneOutput:U.S. Washington
Recommended Posts:
- map::begin() and end() in C++ STL
- set::begin() and set::end() in C++ STL
- valarray begin() function in C++
- multiset begin() and end() function in C++ STL
- match_results begin() and end() function in C++ STL
- unordered_multiset begin() function in C++ STL
- unordered_set begin() function in C++ STL
- unordered_multimap begin() and end() function in C++ STL
- deque::begin() and deque::end in C++ STL
- array::begin() and array::end() in C++ STL
- forward_list::begin() and forward_list::end() in C++ STL
- list::begin() and list::end() in C++ STL
- vector::begin() and vector::end() in C++ STL
- multimap::begin() and multimap::end() in C++ STL
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.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



