Skip to content
Related Articles

Related Articles

Improve Article

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

  • Difficulty Level : Basic
  • Last Updated : 22 Jun, 2018
  1. multimap::begin() is a built-in function in C++ STL which returns an iterator referring to the first element in the multimap container. Since 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




    // C++ function to illustrate
    // the multimap::begin() 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 });
      
        auto ite = mp.begin();
      
        cout << "The first element is: ";
        cout << "{" << ite->first << ", "
             << ite->second << "}\n";
      
        // 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 first element is: {1, 40}
    
    The multimap is : 
    KEY    ELEMENT
    1    40
    2    30
    3    60
    4    20
    5    50
    
  2. multimap::end() is a built-in function in C++ STL which returns an iterator to the theoretical element that follows last element in the multimap. Since 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




    // 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
    
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :