unordered_multimap begin() and end() function in C++ STL

  1. The unordered_multimap::begin() is a built-in function in C++ STL which returns an iterator pointing to the first element in the container or to the first element in one of its bucket.

    Syntax:

    unordered_multimap_name.begin(n)

    Parameters: The function accepts one parameter. If a parameter is passed, it returns an iterator pointing to the first element in the bucket. If no parameter is passed, then it returns a constant iterator pointing to the first element in the unordered_multimap container.

    Return Value: It returns an iterator. It cannot be used to change the value of the unordered_multimap element.

    Below programs illustrates the above function:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // unordered_multimap::begin() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // declaration
        unordered_multimap<int, int> sample;
      
        // inserts element
        sample.insert({ 1, 2 });
        sample.insert({ 3, 4 });
        sample.insert({ 3, 4 });
        sample.insert({ 2, 3 });
        sample.insert({ 2, 3 });
      
        // prints all element
        cout << "Key and Elements: \n";
        for (auto it = sample.begin(); it != sample.end(); it++)
            cout << "   " << it->first << "\t      "
                 << it->second << endl;
      
        auto it = sample.begin();
      
        // print the first element
        cout << "\nThe first key and element: "
             << it->first << " ";
        cout << it->second;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Key and Elements: 
       2          3
       2          3
       1          2
       3          4
       3          4
    
    The first key and element: 2 3
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // unordered_multimap::begin(bucket) function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // declaration
        unordered_multimap<int, int> sample;
      
        // inserts element
        sample.insert({ 1, 2 });
        sample.insert({ 3, 4 });
        sample.insert({ 3, 4 });
        sample.insert({ 2, 3 });
        sample.insert({ 2, 3 });
      
        // prints all element
        cout << "Key and Elements of first bucket: \n";
        for (auto it = sample.begin(1); it != sample.end(1); it++)
            cout << "   " << it->first << "\t      "
                 << it->second << endl;
      
        auto it = sample.begin(1);
      
        // print the first element
        cout << "\nThe first key and element in first bucket: "
             << it->first << " ";
        cout << it->second;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Key and Elements of first bucket: 
       1          2
    
    The first key and element in first bucket: 1 2
    
  2. The unordered_multimap::end() is a built-in function in C++ STL which returns an iterator pointing to the position after the last element in the container or to the position after the last element in one of its bucket.

    Syntax:

    unordered_multimap_name.end(n)

    Parameters: The function accepts one parameter. If a parameter is passed, it returns an iterator pointing to the position after the last element in one of its bucket. If no parameter is passed, then it returns an iterator pointing to the position after the last element in the unordered_multimap container.

    Return Value: It returns an iterator. It cannot be used to change the value of the unordered_multimap element.

    Below programs illustrates the above function:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // unordered_multimap::end() function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // declaration
        unordered_multimap<int, int> sample;
      
        // inserts element
        sample.insert({ 1, 2 });
        sample.insert({ 3, 4 });
        sample.insert({ 3, 4 });
        sample.insert({ 2, 3 });
        sample.insert({ 2, 3 });
      
        // prints all element
        cout << "Key and Elements: \n";
        for (auto it = sample.begin(); it != sample.end(); it++)
            cout << "   " << it->first << "\t      "
                 << it->second << endl;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Key and Elements: 
       2          3
       2          3
       1          2
       3          4
       3          4
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to illustrate the
    // unordered_multimap::end(bucket) function
    #include <bits/stdc++.h>
    using namespace std;
      
    int main()
    {
      
        // declaration
        unordered_multimap<int, int> sample;
      
        // inserts element
        sample.insert({ 1, 2 });
        sample.insert({ 3, 4 });
        sample.insert({ 3, 4 });
        sample.insert({ 2, 3 });
        sample.insert({ 2, 3 });
      
        // prints all element
        cout << "Key and Elements of first bucket: \n";
        for (auto it = sample.begin(1); it != sample.end(1); it++)
            cout << "   " << it->first << "\t      "
                 << it->second << endl;
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Key and Elements of first bucket: 
       1          2
    


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.