
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
multiset begin() and end() function in C++ STL
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
#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
#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
- Related Articles
- match_results begin() and end() function in C++ STL
- map::begin() and end() in C++ STL
- set::begin() and set::end() in C++ STL
- vector::begin() and vector::end() in C++ STL
- list begin( ) and list end( ) in C++ STL
- forward_list::begin() and forward_list::end() in C++ STL
- deque::begin() and deque::end in C++ STL
- multimap::begin() and multimap::end() in C++ STL
- multiset cbegin() and cend() function in C++ STL
- multiset crbegin() and crend() function in C++ STL
- multiset insert() function in C++ STL
- multiset clear() function in C++ STL
- multiset count() function in C++ STL
- multiset empty() function in C++ STL
- Multiset emplace_hint() function in C++ STL
