Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 17 Jun, 2022

The multiset::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the multiset container. Since multiset always contains elements in an ordered way, begin() always points to the first element according to the sorting criterion. 

Syntax:

iterator multiset_name.begin()

Parameters: The function does not accept any parameters.

Return value: The function returns an iterator pointing to the first element in the container.

Below program illustrate the above-mentioned function: 

CPP




// CPP program to demonstrate the
// multiset::begin() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
 
    int arr[] = { 14, 10, 15, 11, 10 };
 
    // initializes the set from an array
    multiset<int> s(arr, arr + 5);
 
    // Print the first element
    cout << "The first element is: " << *(s.begin()) << endl;
 
    // prints all elements in set
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

The first element is: 10
10 10 11 14 15

The multiset::end() is a built-in function in C++ STL that returns an iterator pointing to the position past the last element in the container.

Syntax:

iterator multiset_name.end()

Parameters: The function does not accept any parameters.

Return value: The function returns an iterator pointing to the position past the last element in the container in the multiset container.

Below program illustrate the above-mentioned function: 

CPP




// CPP program to demonstrate the
// multiset::end() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
 
    int arr[] = { 14, 10, 15, 11, 10, 12, 17, 12 };
 
    // initializes the set from an array
    multiset<int> s(arr, arr + 8);
 
    // prints all elements in set
    for (auto it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

10 10 11 12 12 14 15 17

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

multiset begin()multiset end()
It is used to return an iterator referring to the first element in the multiset container.It is used to return an iterator referring to the past-the-end element in the multiset container.

Its syntax is -:

iterator begin();

Its syntax is -:

iterator end();
It does not take any parameters.It does not take any parameters.
Its complexity is constant.Its complexity is constant.
Its iterator validity does not change.Its iterator validity does not change.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!