unordered_map end( ) function in C++ STL

The unordered_map::end() is a built-in function in C++ STL which returns an iterator pointing to the position past the last element in the container in the unordered_map container. In an unordered_map object, there is no guarantee that which specific element is considered its first element. But all the elements in the container are covered since the range goes from its begin to its end until invalidated.

Syntax:

iterator unordered_map_name.end(n)

Parameters : This function accepts one parameter n which is an optional parameter that specifies the bucket number. If it is set, the iterator retrieves points to the past-the-end element of a bucket, otherwise, it points to the past-the-end element of the container.

Return value: The function returns an iterator to the element past the end of the container.

Below programs illustrates the above-mentioned function:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the
// unordered_map::end() function
// returning all the elements of the multimap
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
  
int main()
{
    unordered_map<string, int> marks;
  
    // Declaring the elements of the multimap
    marks = { { "Rohit", 64 }, { "Aman", 37 }, { "Ayush", 96 } };
  
    // Printing all the elements of the multimap
    cout << "marks bucket contains : " << endl;
    for (int i = 0; i < marks.bucket_count(); ++i) {
  
        cout << "bucket #" << i << " contains:";
  
        for (auto iter = marks.begin(i); iter != marks.end(i); ++iter) {
            cout << "(" << iter->first << ", " << iter->second << "), ";
        }
  
        cout << endl;
    }
    return 0;
}

chevron_right


Output:

marks bucket contains : 
bucket #0 contains:
bucket #1 contains:
bucket #2 contains:
bucket #3 contains:(Aman, 37), (Rohit, 64), 
bucket #4 contains:(Ayush, 96),

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the
// unordered_map::end() function
// returning the elements along
// with their bucket number
#include <iostream>
#include <string>
#include <unordered_map>
  
using namespace std;
  
int main()
{
    unordered_map<string, int> marks;
  
    // Declaring the elements of the multimap
    marks = { { "Rohit", 64 }, { "Aman", 37 }, { "Ayush", 96 } };
  
    // Printing all the elements of the multimap
    for (auto iter = marks.begin(); iter != marks.end(); ++iter) {
  
        cout << "Marks of " << iter->first << " is "
             << iter->second << " and his bucket number is "
             << marks.bucket(iter->first) << endl;
    }
  
    return 0;
}

chevron_right


Output:

Marks of Ayush is 96 and his bucket number is 4
Marks of Aman is 37 and his bucket number is 3
Marks of Rohit is 64 and his bucket number is 3


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.