multiset cbegin() and cend() function in C++ STL


In this article we will be discussing the working, syntax and examples of multiset::cbegin() and multiset::cend() function 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::cbegin()?

multiset::cbegin() function is an inbuilt function in C++ STL, which is defined in <set> header file. cbegin() means constant begin function, means this function returns the constant iterator pointing to the beginning of the multiset container.

The constant iterator can be just used for iterating through the multiset container, this can’t make changes in the multiset container.

Syntax

ms_name.cbegin();

Parameters

The function accepts no parameter.

Return value

This function returns a constant iterator which is pointing to the first element of the container.

Example

Input

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

Output

1

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {10, 20, 30, 40, 50, 60};
   multiset<int> check(arr, arr + 6);
   cout<<"First element fetched using cbegin() function: "<<*(check.cbegin()) << endl;
   for(auto i = check.cbegin(); i!= check.cend(); i++)
      cout << *i << " ";
   return 0;
}

Output

First element fetched using cbegin() function: 10
10 20 30 40 50 60

What is multiset::cend()?

multiset::cend() function is an inbuilt function in C++ STL, which is defined in <set> header file. cend() means constant end function, means this function returns the constant iterator pointing to past the last element of the multiset container.

The constant iterator can be just used for iterating through the multiset container, this can’t make changes in the multiset container.

Syntax

ms_name.cend();

Parameters

The function accepts no parameter.

Return value

This function returns a constant iterator which is pointing to the element past the last of the container.

Example

Input

std::multiset<int&t; mymultiset = {1, 2, 2, 3, 4};
mymultiset.cend();

Output

error

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
   int arr[] = {10, 20, 30, 40, 50, 60};
   multiset<int> check(arr, arr + 6);
   cout<<"Elements in the list are: ";
   for(auto i = check.cbegin(); i!= check.cend(); i++)
      cout << *i << " ";
   return 0;
}

Output

Elements in the list are: 10 20 30 40 50 60

Updated on: 17-Apr-2020

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements