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
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<std::string, std::string> mymap;
mymap = { { "Australia" , "Canberra" },
{ "U.S." , "Washington" },
{ "France" , "Paris" } };
auto it = mymap.begin();
cout << it->first << " " << it->second;
return 0;
}
|
- 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
unordered_map<std::string, std::string> mymap;
mymap = { { "Australia" , "Canberra" },
{ "U.S." , "Washington" }, { "France" , "Paris" } };
auto it = mymap.begin(0);
cout << it->first << " " << it->second;
return 0;
}
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!