The end function in C++ multimap is employed to retrieve an iterator positioned immediately after the final entry in the multimap.
Note:-This is a placeholder. No element exists in this location and attempting to access is undefined behavior.
Syntax
iterator end(); //until C++ 11
const_iterator end() const; //until C++ 11
iterator end() noexcept; //since C++ 11
const_iterator end() const noexcept; //since C++ 11
Parameter
Return value
It returns an iterator positioned after the final element of the multimap.
Complexity
Constant.
Iterator validity
No changes.
Data Races
The container is accessed in a way that neither the const nor the non-const versions alter the container.
Exception Safety
This member function never throws exception.
Example 1
Let's see the simple example for end function:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,string> mymultimap;
mymultimap = {
{'a',"Java"},
{'b', "C++"},
{'b', "Python"},
{'a', "Android"}
};
// show content:
for (multimap<char,string>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
cout << it->first << " => " << it->second << '\n';
return 0;
}
Output:
a => Java
a => Android
b => C++
b => Python
In the previous example, the end method is employed to retrieve an iterator pointing to the element following the last one in the mymultimap multimap.
Example 2
Let's examine a straightforward illustration of iterating through the multimap utilizing a for-each loop:
#include <iostream>
#include <map>
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
int main() {
multimap<string, int> m;
m= {
{"Room1", 100},
{"Room2", 200},
{"Room1", 300},
{"Room1", 100}
};
// Create a multimap iterator and point to beginning of multimap
multimap<string, int>::iterator it = m.begin();
// Iterate over a multimap using std::for_each and Lambda function
for_each(m.begin(), m.end(),
[](pair<string, int> element){
// Accessing KEY from element
string word = element.first;
// Accessing VALUE from element.
int count = element.second;
cout<<word<<" = "<<count<<endl;
});
return 0;
}
Output:
Room1 = 100
Room1 = 300
Room1 = 100
Room2 = 200
In the previous example, we employed an STL function std::for-each to traverse through the multimap. This function sequentially processes each element within the multimap and executes the custom callback function that we specify.
Example 3
Let's explore a basic illustration of iterating through the multimap utilizing a while loop:
#include <iostream>
#include <map>
#include <string>
int main()
{
using namespace std;
multimap<int,string> mymultimap = {
{ 100, "Nikita"},
{ 200, "Deep" },
{ 200, "Priya" },
{ 400, "Suman" },
{ 300, "Aman" }};
multimap<int, string>::const_iterator it; // declare an iterator
it = mymultimap.begin(); // assign it to the start of the vector
while (it != mymultimap.end()) // while it hasn't reach the end
{
cout << it->first << " = " << it->second << "\n";
// print the value of the element icpp tutorials to
++it; // and iterate to the next element
}
cout << endl;
return 0;
}
Output:
100 = Nikita
200 = Deep
200 = Priya
300 = Aman
400 = Suman
In the example provided, the end method is utilized to retrieve an iterator pointing to the element that comes after the last one in the mymultimap multimap.
Example 4
Let's see a simple example:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
multimap<int,int> mymultimap = {
{ 10, 10},
{ 20, 20 },
{ 10, 30 },
{ 20, 10}
};
cout<<"Elements are:" <<endl;
for (multimap<int,int>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
cout << it->first
<< " * "
<< it->second
<< " = "
<<it->first * it->second
<< '\n';
return 0;
}
Output:
Elements are:
10 * 10 = 100
10 * 30 = 300
20 * 20 = 400
20 * 10 = 200
In the example provided, the end method is utilized to retrieve an iterator pointing to the element immediately following the last element in the mymultimap multimap.