unordered_map end() Function in C++ with Examples

C++ unordered_map end() Function: Here, we will learn about the end() function, its usages, syntax and examples.
Submitted by Shivang Yadav, on July 20, 2022

unordered_map:

An unordered_map is a special type of container that stores data in the form of key-value pairs in an unordered manner. The key stored is used to identify the data value mapped to it uniquely.

std::unordered_map::end() Function

The end() function is used to return the iterator that points to the element next to the last element of the unordered_map container.

std::unordered_map::end() Function Syntax:

unordered_map.end();

Parameter(s): It does not accept any parameter for its working.

Return Value: It returns an iterator that points towards the last elements next to the last element in the unordered_map.

Note: The unordered_map does not follow any strict indexing pattern, hence there is no specific end or begin element. The end() function is generally used along with begin() function for iterating over the elements of the unordered_map.

C++ unordered_map end() Function Example

// C++ program to illustrate working of begin() function 
// on unordered_map

#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int main()
{
    // Creating unordered_map
    unordered_map<int, string> unorderedMap;
    unorderedMap = { { 1, "Scala" }, { 2, "Python" }, { 3, "JavaScript" }, { 4, "C++" } };

    // Printing unordered_map elements
    cout << "The elements of unordered_map are ";
    for (auto it = unorderedMap.begin(); it != unorderedMap.end(); it++)
        cout << it->first << " : " << it->second << "\t";

    return 0;
}

Output:

The elements of unordered_map are 4 : C++       3 : JavaScript  2 : Python      1 : Scala




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.