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
#include <iostream>
#include <map>
using namespace std;
int main()
{
map< char , int > mymap;
mymap[ 'a' ] = 1;
mymap[ 'b' ] = 2;
mymap[ 'c' ] = 3;
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
#include <iostream>
#include <map>
using namespace std;
int main()
{
map< char , int > mymap;
mymap[ 'a' ] = 1;
mymap[ 'b' ] = 2;
mymap[ 'c' ] = 3;
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. |
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!
Last Updated :
12 Jun, 2023
Like Article
Save Article