Open In App

map::begin() and end() in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values.

map::begin()

begin() function is used to return an iterator pointing to the first element of the map container. begin() function returns a bidirectional iterator to the first element of the container. 

Syntax :

mapname.begin()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the first element.

Examples:

Input  : mymap['a'] = 1;
         mymap['b'] = 2;
         mymap['c'] = 3;
         mymap.begin();
Output : returns an iterator to the element 'a' = 1

Input  : mymap['d'] = 1;
         mymap.begin();
Output : returns an iterator to the element 'd' = 1

Errors and Exceptions 

1. It has a no exception throw guarantee. 
2. Shows error when a parameter is passed. 

CPP




// Demonstrates begin() and end()
#include <iostream>
#include <map>
using namespace std;
 
int main()
{
    // declaration of map container
    map<char, int> mymap;
    mymap['a'] = 1;
    mymap['b'] = 2;
    mymap['c'] = 3;
 
    // using begin() to print map
    for (auto it = mymap.begin();
         it != mymap.end(); ++it)
        cout << it->first << " = "
             << it->second << '\n';
    return 0;
}


Output:

a = 1
b = 2
c = 3
map::end()

end() function is used to return an iterator pointing to past the last element of the map container. Since it does not refer to a valid element, it cannot de-referenced end() function returns a bidirectional iterator. 

Syntax :

mapname.end()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to the next of last element.

Examples:

Input  : mymap['a'] = 1;
         mymap['b'] = 2;
         mymap['c'] = 3;
         mymap.end();
Output : returns an iterator to next to c 
(after last element)

Errors and Exceptions 

1. It has a no exception throw guarantee. 
2. Shows error when a parameter is passed. 

CPP




// CPP program to illustrate
// Demonstrates begin() and end()
#include <iostream>
#include <map>
using namespace std;
 
int main()
{
    // declaration of map container
    map<char, int> mymap;
    mymap['a'] = 1;
    mymap['b'] = 2;
    mymap['c'] = 3;
 
    // using begin() to print map
    for (auto it = mymap.begin();
         it != mymap.end(); ++it)
        cout << it->first << " = "
             << it->second << '\n';
    return 0;
}


Output:

a = 1
b = 2
c = 3

Let us see the differences in a tabular form -:

  map::begin() map::end()
1. It is used to return an iterator referring to the first element in the map container. It is used to return an iterator referring to the past-the-end element in the map container.
2.

Its syntax is -:

iterator begin();

Its syntax is -:
iterator end();
3. It does not take any parameters. It does not take any parameters.
4. Its complexity is constant. Its complexity is constant.
5. Its iterator validity does not changes. Its iterator validity does not changes.


Last Updated : 12 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads