Works of begin() in C++

Works of begin() in C++
Techiio-author
Written by Sagar RabidasFebruary 12, 2022
10 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Works of begin() in C++

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 direction 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 values of the elements and also the elements are stored in sorted order where keys are the sorting criteria. The function requires no arguments thus is called directly using map container reference and provides a guarantee of no exception when the 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 a 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 iterator that are returned according to the reference of the map object calling the function.

Iterator – In case the 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 to as an element.

How begin() function works in C++?

The map is an associative container that stores the elements in key-value pairs in a 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 traverse through all the elements stored in the container.

When a map says:- map<datatype1, datatype1> map object; is declared where datatype1 and datatype2 are datatypes of key and values of map reference map object. If one needs to traverse the elements in the given map, one needs an iterator. Thus when one call begin to 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 forwarding as well as the backward direction that helps to traverse elements in reverse order easily. This function is present in the std template of a map in the 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 an example of a map to store percentages of students in a class against their roll numbers. To print their values we need an iterator to traverse over all the elements in the map. This iterator is retrieved using class10.begin() function and stored in reference variable it and traversed until end of the map is reached and iterator points to an element being pointed by map. end() function. Elements from it are accessed using its->first and it->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;
}

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;
}

Example #3

Let's 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 the 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;
}

C
C++
begin() function
begin() in C++
C++ functions
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance