The Wayback Machine - https://web.archive.org/web/20241006041603/https://www.geeksforgeeks.org/multiset-begin-and-end-function-in-c-stl/
Open In App

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

Last Updated : 17 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Previous Article
Next Article

Similar Reads

Why are Standard Iterator Ranges [begin, end) Instead of [begin, end]?
In C++, the Standard Template Library (STL) uses a half-open range convention [begin, end) for iterator ranges. This means the range includes the begin iterator but excludes the end iterator. A common question arises: Why does the STL adopt this convention instead of the fully closed range [begin, end]? In this article, we will learn the advantages
4 min read
unordered_multimap begin() and end() function in C++ STL
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 eleme
5 min read
match_results begin() and end() function in C++ STL
match_results::cbegin() is an inbuilt function in C++ STL that returns an iterator that points to the first match in the match_results object. Syntax: smatch_name.begin()Parameters: This function does not accept any parameters. Return value: This function returns an iterator pointing to the first match in the match_results object. The matches conta
3 min read
array::begin() and array::end() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::begin() begin() function is used to return an iterator pointing to the first element of the array container. begin() function returns a bidirectional iterat
3 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::begin()begin() function
3 min read
forward_list::begin() and forward_list::end() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, the forward list is more useful than other containers in insertion, removal, and moving operations (like sort) and allows time constant insertion and removal of elements. It differs from list by the fact that the forward list keeps track of the location of only the next eleme
3 min read
list::begin() and list::end() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::begin() begin() function is used to return an iterator pointing to the first element of the l
3 min read
map::begin() and end() in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::begin() begin() function is used to return an iterator pointing to the first element of the map container. begin() function returns a bidirectional iterator to the first elemen
3 min read
set::begin() and set::end() in C++ STL
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::begin() begin() function is used to return an iterator pointing to t
4 min read
multimap::begin() and multimap::end() in C++ STL
multimap::begin() is a built-in function in C++ STL that returns an iterator referring to the first element in the multimap container. Since the multimap container contains the element in an ordered way, begin() will point to that element that will come first according to the container's sorting criterion. Syntax: multimap_name.begin() Parameters:
3 min read
vector::begin() and vector::end() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::begin()begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional
3 min read
unordered_set begin() function in C++ STL
The unordered_set::begin() method is a builtin function in C++ STL which is used to return an iterator pointing to the first element in the unordered_set container. All of the iterators of an unordered_set can be used to only access the elements, iterators are not allowed to modify elements present in an unordered_set container. Note: This iterator
2 min read
unordered_multiset begin() function in C++ STL
The unordered_multiset::begin() is a built-in function in C++ STL which returns an iterator pointing to the first element in the container or to the first element in one of its bucket. Syntax: unordered_multiset_name.begin(n) Parameters: The function accepts one parameter. If a parameter is passed, it returns an iterator pointing to the first eleme
2 min read
multiset rbegin() and rend() function in C++ STL
multiset::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the multiset container. Syntax: reverse_iterator multiset_name.rbegin() Parameters: The function does not take any parameter. Return value: The function returns a reverse iterator pointing to the last element in the container. Below
2 min read
multiset crbegin() and crend() function in C++ STL
The multiset::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator pointing to the last element in the container. The iterator cannot be used to modify the elements in the multiset container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_reverse_iterator multiset_name.
2 min read
multiset cbegin() and cend() function in C++ STL
The multiset::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator multiset_name.cbegin() Parameters:
3 min read
multiset insert() function in C++ STL
The multiset::insert() is a built-in function in C++ STL which insert elements in the multiset container or inserts the elements from a position to another position from one multiset to a different multiset. Syntax: iterator multiset_name.insert(element)Time Complexity: O(log n) Since the elements are always in sorted order, the newly inserted elem
4 min read
multiset find() function in C++ STL
The multiset::find() is a built-in function in C++ STL which returns an iterator pointing to the lower_bound of the element which is searched in the multiset container. If the element is not found, then the iterator points to the position past the last element in the set. Syntax: multiset_name.find(element) Parameters: The function accepts one mand
2 min read
multiset equal_range() function in C++ STL
The multiset::equal_range() is a built-in function in C++ STL which returns an iterator of pairs. The pair refers to the range that includes all the elements in the container which have a key equivalent to k. The lower bound will be the element itself and the upper bound will point to the next element after key k. If there are no elements matching
3 min read
multiset clear() function in C++ STL
The multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container. The final size of multiset container after removal is 0. Syntax: multiset_name.clear() Parameters: The function does not accept any parameter. Return Value: The function does not returns anything. Below programs illustrates the m
2 min read
multiset empty() function in C++ STL
The multiset::empty() function is a built-in function in C++ STL which checks if the multiset is empty or not. It returns true if the multiset is empty, else it returns false. Syntax: multiset_name.empty() Parameters: The function does not accept any parameter. Return Value: The function returns true if the multiset is empty, else it returns false.
1 min read
multiset count() function in C++ STL
The multiset::count() function is a built-in function in C++ STL that searches for a specific element in the multiset container and returns the number of occurrences of that element. Syntax: multiset_name.count(val) Parameters: The function accepts a single parameter val which specifies the element to be searched in the multiset container. Return V
2 min read
multiset emplace_hint() function in C++ STL
The multiset::emplace_hint() is a built-in function in C++ STL which inserts a new element in the multiset. A position is passed in the parameter of the function which acts as a hint from where the searching operation starts before inserting the element at its current position. The position only helps the process to get faster, it does not decide w
3 min read
multiset get_allocator() function in C++ STL
The multiset::get_allocator() method in C++ STL is a built-in function in C++ STL which returns a copy of the allocator object associated with the multiset. Syntax: multiset_name.get_allocator() where allocator_type is the type of the allocator used by the container. Parameters: The function does not take any parameter. Return Value: This method re
1 min read
multiset::operator= in C++ STL
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::operator= This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents.Syntax : multisetname1 = (multisetname2) Parameter
3 min read
multiset::swap() in C++ STL
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::swap() This function is used to exchange the contents of two multisets but the sets must be of same type, although sizes may differ. Syntax : multisetname1.swap(multisetname2) Parameters : The name of the multiset
3 min read
multiset::emplace() in C++ STL
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::emplace() This function is used to insert a new element into the multiset container. Syntax : multisetname.emplace(value) Parameters : The element to be inserted into the multiset is passed as the parameter. Result
3 min read
multiset lower_bound() in C++ STL with Examples
The multiset::lower_bound() is a built-in function in C++ STL which returns an iterator pointing to the first element in the container which is equivalent to k passed in the parameter. In case k is not present in the set container, the function returns an iterator pointing to the immediate next element which is just greater than k. If the key passe
3 min read
multiset upper_bound() in C++ STL with Examples
The multiset::upper_bound() is a built-in function in C++ STL that returns an iterator pointing to the immediate next element which is just greater than k. If the key passed in the parameter exceeds the maximum key in the container, then the iterator returned points an element which points to the position after the last element in the container. Sy
3 min read
multiset max_size() in C++ STL with Examples
The multiset::max_size() is a built-in function in C++ STL which returns the maximum number of elements a multiset container can hold. Syntax: multiset_name.max_size() Parameters: The function does not accept any parameters. Return Value: The function returns the maximum number of elements a multiset container can hold. Below programs illustrate th
1 min read
Practice Tags :