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

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of multiset::begin() and multiset::end() functions in C++ STL.

What is a multiset in C++ STL?

Multisets are the containers similar to the set container, meaning they store the values in the form of keys same like a set, in a specific order.

In multiset the values are identified as keys as same as sets. The main difference between multiset and set is that the set has distinct keys, meaning no two keys are the same, in multiset there can be the same keys value.

Multiset keys are used to implement binary search trees.

What is multiset::begin()?

multiset::begin() function is an inbuilt function in C++ STL, which is defined in <set>header file.

This function returns an iterator which is pointing to the first element in the multiset container.

As the multiset containers stores the values in the ascending order, the begin() points to the element which is the first element of the container according to the sorting criteria.

Syntax

ms_name.begin();

Parameters

The function accepts no parameter.

Return value

This function returns an iterator pointing to the first element of the multiset container associated with it.

Example

Input: std::multiset<int> mymultiset = {1, 2, 2, 3, 4};
   mymultiset.begin();
Output: 1

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {2, 4, 1, 3, 8, 5, 6};
   multiset<int> check(arr, arr + 7);
   cout<<"List is : ";
   for (auto i = check.begin(); i != check.end(); i++)
      cout << *i << " ";
   cout<<"\nStarting Element is : "<<*(check.begin());
   return 0;
}

Output

If we run the above code it will generate the following output −

List is : 1 2 3 4 5 6 8
Starting Element is : 1

What is multiset::end()

multiset::end() function is an inbuilt function in C++ STL, which is defined in <set> header file.

This function returns an iterator which is pointing to the past to end position in the multiset container.

Past to end element is the element which follows the last element of the multiset container. In short it doesn’t point to any specific element of the multiset container. This function is generally used with begin() to give the range of the multiset container.

Syntax

ms_name.end();

Parameters

The function accepts no parameter.

Return value

This function returns an iterator pointing to past the end element of the multiset container associated with it.

Example

Input: std::multiset<int> mymultiset = {1, 2, 2, 3, 4};
   for( std::multiset<int>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it )
Output: 1 2 2 3 4

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {2, 4, 1, 3, 8, 5, 6};
   multiset<int> check(arr, arr + 7);
   cout<<"List is : ";
   for (auto i = check.begin(); i != check.end(); i++)
      cout << *i << " ";
   return 0;
}

Output

If we run the above code it will generate the following output −

List is : 1 2 3 4 5 6 8
raja
Published on 23-Mar-2020 07:29:40
Advertisements