multimap::begin() is a built-in function in C++ STL that returns an iterator referring to the first element in the multimap container. Since the multimap container contains the element in an ordered way, begin() will point to that element that will come first according to the container’s sorting criterion.
Syntax:
multimap_name.begin()
Parameters: The function does not accept any parameter.
Return Value: The function returns an iterator referring to the first element in the multimap container
Simple Example
C++
#include <iostream>
#include <map>
int main()
{
std::multimap< int , std::string> mmap = {
{1, "one" },
{2, "two" },
{3, "three" },
{3, "three again" },
};
std::cout << "First: " << mmap.begin()->second << std::endl;
std::cout << "Last: " << (--mmap.end())->second << std::endl;
return 0;
}
|
Output
First: one
Last: three again
CPP
#include& lt; bits / stdc++.h & gt;
using namespace std;
int main()
{
multimap& lt;
int , int & gt;
mp;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 4, 20 });
mp.insert({ 5, 50 });
auto ite = mp.begin();
cout& lt;
<
"
The first element is : "
;
cout& lt;
<
"
{
"
<
<
ite - >
first& lt;
<
"
, "
<
<
ite - >
second& lt;
<
"
}
\n& quot;
;
cout& lt;
<
"
\nThe multimap is : \n& quot;
;
cout& lt;
<
"
KEY\tELEMENT\n& quot;
;
for ( auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout& lt;
<
itr - >
first& lt;
<
'\t' & lt;
<
itr - >
second& lt;
<
'\n' ;
}
return 0;
}
|
Output:
The first element is: {1, 40}
The multimap is :
KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
multimap::end() is a built-in function in C++ STL that returns an iterator to the theoretical element that follows the last element in the multimap. Since the multimap container contains the element in an ordered way, end() will point to that theoretical position which follows the last element according to the container’s sorting criterion.
Syntax:
multimap_name.end()
Parameters: The function does not accept any parameter.
Return Value: The function returns an iterator referring to the first element in the multimap container
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
multimap< int , int > mp;
mp.insert({ 2, 30 });
mp.insert({ 1, 40 });
mp.insert({ 3, 60 });
mp.insert({ 4, 20 });
mp.insert({ 5, 50 });
cout << "\nThe multimap is : \n" ;
cout << "KEY\tELEMENT\n" ;
for ( auto itr = mp.begin(); itr != mp.end(); ++itr) {
cout << itr->first
<< '\t' << itr->second << '\n' ;
}
return 0;
}
|
Output:
The multimap is :
KEY ELEMENT
1 40
2 30
3 60
4 20
5 50
Let us see the differences in a tabular form as shown below as follows:
multimap::begin() |
multimap::end() |
It is used to return an iterator referring to the first element in the multimap container. |
It is used to return an iterator referring to the past-the-end element in the multimap container. |
Its syntax is -:
iterator begin();
|
Its syntax is -:
iterator end();
|
It does not take any parameters. |
It does not take any parameters. |
Its complexity is constant. |
Its complexity is constant. |
Its iterator validity does not change. |
Its iterator validity does not change. |
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 :
24 Jan, 2023
Like Article
Save Article