Updated April 1, 2023
Introduction to C++ begin()
This C++ begin() is used to get the iterator pointing to the initial element of the map container. This pointer is bidirectional since it can be moved to either directions in the sequence. This function is present in map associative container class template in std namespace where elements are stored in the form of key-value pairs. Here keys are used to uniquely identify the elements values and also the elements are stored in sorted order where keys are the sorting criteria. Function requires no arguments thus is called directly using map container reference and provides guarantee of no exception when correct syntax is followed.
Syntax
Map are associative containers that store the elements in key-value pair fashion, such that no 2 elements can have 2 same keys. This contains begin() function that gives an bidirectional iterator pointing to the first element of the container.
iterator begin() noexcept;
const_iterator begin() const noexcept;
Parameters- No parameter is required for calling begin function just map reference object is used to directly call this function using dot(.) operator.
Explanation: Here iterator and const_iterator are two different types of iterartor which is returned according to the reference of map object calling the function.
Iterator – In case map object is not cons-qualified then member -types iterator is returned. This is a bidirectional iterator that points to the first element of the map container, and here element refers to the key-value pair.
Const_iterator – In case map reference is const-qualified then a bidirectional const-iterator is returned pointing to the first element of the cont map container and here key-value pair is referred as element.
This function of map container guarantees to throw no exception.
How begin() function works in C++?
Map is an associative container that store the elements in key-value pair in sorted manner where elements are sorted by the key values. Here begin function helps to get an iterator pointing to the first element of the container and helps to travers through all the elements stored in the container.
When a map say :- map<datatype1, datatype1> mapObject; is declared where datatype1 and datatype2 are datatype of key and values of map reference mapObject. If one needs to traverse the elements in the given map, one needs an iterator. Thus when one calls begin function-
mapObject.begin()
it is checked if map reference is const-qualified or not and depending on that corresponding function is called and returns the const_iterator or iterator respectively. For calling begin function no parameter is required. The returned iterator is also of map function type in key-value pair form. The returned iterator is a bidirectional iterator that means one can traverse the elements in forward as well as backward direction that helps to traverse elements in reverse order easily. This function is present in std template of map in Standard Template Library. It guarantees that no exception will be thrown while calling it, even if there is no element in the map. One must note that the returned iterator object must not be dereferenced.
Examples to Implement begin() in C++
Below are the examples mentioned:
Example #1
Let us see example of map to store percentages of students in a class against their roll numbers. In order to print there values we need an iterator to travers over all the elements in the map. This iterator is retrieved using class10.begin() function and stored in reference variable itr and traversed until end of map is reached and iterator points to an element being pointed by map.end() function. Elements from itr is accessed using itr->first and itr->Second statements.
Code:
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
int main()
{
map<int, int> class10; //map declaration of key-value pair of int-int type
class10.insert(pair<int, int>(1, 65));
class10.insert(pair<int, int>(2, 78));
class10.insert(pair<int, int>(3, 60));
class10.insert(pair<int, int>(4, 50));
class10.insert(pair<int, int>(5, 90));
class10.insert(pair<int, int>(6, 80));
class10.insert(pair<int, int>(7, 75));
map<int, int>::iterator itr; // iterator of int-int type
cout << "\nThe map of percentages of students of class10 is : \n";
cout << "\tRoll_no \t Percentage\n";
for (itr = class10.begin(); itr != class10.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
}
cout << endl;
return 0;
}
Output:
Example #2
In this example we will try to access an iterator over an empty map and see no error or exception is thrown, but nothing is printed.
Code:
#include <iterator>
#include <map>
using namespace std;
int main()
{
map<int, int> class10;
map<int, int>::iterator itr;
cout << "\nThe map of percentages of students of class10 is : \n";
cout << "\tRoll_no \t Percentage\n";
for (itr = class10.begin(); itr != class10.end(); ++itr) {
cout << '\t' << itr->first
<< '\t' << itr->second << '\n';
} return 0;
}
Output:
Example #3
Lets see one example if we insert elements in unsorted order. We will see map will sort the elements according to the key values i.e roll_no since map stores the elements in sorted order only, and then values are traversed and will be printed.
Code:
#include <iostream>
#include <map>
#include <string>
int main()
{
using namespace std;
map<int,string> Student = {
{ 4, "Raj"},
{ 1, "Prakash" },
{ 3, "Priya" },
{ 2, "Savi" },
{ 5, "Rituraj" }};
cout<<"Students in class are:" <<endl;
map<int, string>::const_iterator myIterator;
myIterator = Student.begin(); //iterator to first element is returned
cout << "ROLL_NO"<<"\t" <<"NAME" << "\n";
while (myIterator != Student.end() )
{
cout << myIterator->first <<"\t" << myIterator->second << "\n";
++myIterator;
}
cout << endl;
return 0;
}
Output:
Conclusion
begin() function is used to retrieve a bidirectional iterator pointing to the first element of the map , an associative container which is of same type as of the map. This function guarantees not to throw any exception and the returned iterator object must not be dereferenced if the map is empty.
Recommended Articles
This is a guide to C++ begin(). Here we discuss an introduction to C++ begin() with appropriate syntax, how does it work, and respective examples. You can also go through our other related articles to learn more –