Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 17 Jun, 2022
Improve Article
Save Article

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

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 are programs that illustrate the above function: 

Program 1: 

CPP




// C++ program to illustrate the
// unordered_multimap::begin() function
 
#include& lt; bits / stdc++.h & gt;
using namespace std;
 
int main()
{
 
    // declaration
    unordered_multimap& lt;
    int, int& gt;
    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& lt;
    <
    "
    Key and Elements : \n& quot;
    ;
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout& lt;
    <
    "
    "
    <
    <
    it - >
    first& lt;
    <
    "
    \t& quot;
    <
    <
    it - >
    second& lt;
    <
    endl;
 
    auto it = sample.begin();
 
    // print the first element
    cout& lt;
    <
    "
    \nThe first key and element : "
    <
    <
    it - >
    first& lt;
    <
    "
    "
    ;
    cout& lt;
    <
    it - >
    second;
 
    return 0;
}

Output:

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

The first key and element: 2 3

Program 2: 

CPP




// C++ program to illustrate the
// unordered_multimap::begin(bucket) function
 
#include& lt; bits / stdc++.h & gt;
using namespace std;
 
int main()
{
 
    // declaration
    unordered_multimap& lt;
    int, int& gt;
    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& lt;
    <
    "
    Key and Elements of first bucket : \n& quot;
    ;
    for (auto it = sample.begin(1); it != sample.end(1);
         it++)
        cout& lt;
    <
    "
    "
    <
    <
    it - >
    first& lt;
    <
    "
    \t& quot;
    <
    <
    it - >
    second& lt;
    <
    endl;
 
    auto it = sample.begin(1);
 
    // print the first element
    cout& lt;
    <
    "
    \nThe first key and element in first bucket : "
    <
    <
    it - >
    first& lt;
    <
    "
    "
    ;
    cout& lt;
    <
    it - >
    second;
 
    return 0;
}

Output:

Key and Elements of first bucket: 
   1          2

The first key and element in first bucket: 1 2

The unordered_multimap::end() is a built-in function in C++ STL that 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 buckets. 

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 are programs that illustrate the above function: 

Program 1: 

CPP




// C++ program to illustrate the
// unordered_multimap::end() function
 
#include& lt; bits / stdc++.h & gt;
using namespace std;
 
int main()
{
 
    // declaration
    unordered_multimap& lt;
    int, int& gt;
    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& lt;
    <
    "
    Key and Elements : \n& quot;
    ;
    for (auto it = sample.begin(); it != sample.end(); it++)
        cout& lt;
    <
    "
    "
    <
    <
    it - >
    first& lt;
    <
    "
    \t& quot;
    <
    <
    it - >
    second& lt;
    <
    endl;
 
    return 0;
}

Output:

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

Program 2: 

CPP




// C++ program to illustrate the
// unordered_multimap::end(bucket) function
 
#include& lt; bits / stdc++.h & gt;
using namespace std;
 
int main()
{
 
    // declaration
    unordered_multimap& lt;
    int, int& gt;
    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& lt;
    <
    "
    Key and Elements of first bucket : \n& quot;
    ;
    for (auto it = sample.begin(1); it != sample.end(1);
         it++)
        cout& lt;
    <
    "
    "
    <
    <
    it - >
    first& lt;
    <
    "
    \t& quot;
    <
    <
    it - >
    second& lt;
    <
    endl;
 
    return 0;
}

Output:

Key and Elements of first bucket: 
   1          2

Let us see the differences in a tabular form as shown below as follows:

 unordered_multimap::beginunordered_multimap::end
1.It is used to return an iterator pointing to the first element in the unordered_multimap container or in one of its bucketsIt is used to return an iterator pointing to the past-the-end element in the unordered_multimap container or in one of its buckets.
2.

Its syntax is -:

iterator begin()

Its syntax is -:

iterator end()
3.It takes one parameter which is the Bucket number.It takes one parameter which is the Bucket number.
4.Its complexity is constant.Its complexity is constant.
5.Its iterator validity does not change.Its iterator validity does not change.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!