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: The function does not accept any parameters.
Return value: The function returns a constant iterator pointing to the first element in the container.
Below programs illustrate the multiset::cbegin() method.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 14, 10, 15, 11, 10 };
multiset< int > s(arr, arr + 5);
cout << "The first elements is: " << *(s.cbegin()) << endl;
for ( auto it = s.cbegin(); it != s.cend(); it++)
cout << *it << " " ;
return 0;
}
|
Output:
The first elements is: 10
10 10 11 14 15
The multiset::cend() is a built-in function in C++ STL which returns a constant iterator pointing to the position past the last 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 in the set accordingly.
Syntax:
constant_iterator multiset_name.cend()
Parameters: The function does not accept any parameters.
Return value: The function returns a constant iterator pointing to the position past the last element in the container in the container.
Below programs illustrate the multiset::cend() method.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 14, 10, 15, 11, 10, 15, 17, 17 };
multiset< int > s(arr, arr + 8);
for ( auto it = s.cbegin(); it != s.cend(); it++)
cout << *it << " " ;
return 0;
}
|
Output:
10 10 11 14 15 15 17 17
Let us see the differences in a tabular form -:
|
multiset cbegin() |
multiset cend() |
1. |
It is used to return a const_iterator pointing to the first element in the container. |
It is used to return a const_iterator pointing to the past-the-end element in the container. |
2. |
Its syntax is -:
const_iterator cbegin();
|
Its syntax is -:
const_iterator cend();
|
3. |
It does not take any parameters |
It does not take any parameters. |
4. |
Its complexity is constant. |
Its complexity is constant. |
5. |
Its iterator validity does not changes. |
Its iterator validity does not changes. |
Last Updated :
10 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...