Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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()
{
    // Create a multimap of integers to strings
    std::multimap<int, std::string> mmap = {
        {1, "one"},
        {2, "two"},
        {3, "three"},
        {3, "three again"},
    };
 
    // Print the first and last elements of the multimap
    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




// C++ function to illustrate
// the multimap::begin() function
#include& lt; bits / stdc++.h & gt;
using namespace std;
 
int main()
{
 
    // initialize container
    multimap& lt;
    int, int& gt;
    mp;
 
    // insert elements in random order
    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;
    ;
 
    // prints the elements
    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




// C++ function to illustrate
// the multimap::end() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    // initialize container
    multimap<int, int> mp;
 
    // insert elements in random order
    mp.insert({ 2, 30 });
    mp.insert({ 1, 40 });
    mp.insert({ 3, 60 });
    mp.insert({ 4, 20 });
    mp.insert({ 5, 50 });
 
    // prints the elements
    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.


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