C++ Standard Library C++ STL Library

C++ multiset - cbegin() Function



The C++ multiset::cbegin function returns the constant iterator (const_iterator) pointing to the first element of the multiset.

C++ cbegin cend

Note: A const_iterator is an iterator that points to constant value. The difference between iterator and const_iterator is that the const_iterator cannot be used to modify the content it points to, even if the multiset element is not itself constant.

Note: Multiset is an ordered data container which implies all its elements are ordered all the time.

Syntax

const_iterator cbegin() const noexcept;

Parameters

No parameter is required.

Return Value

A const_iterator to the beginning of the sequence container.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the multiset::cbegin function returns the const_iterator pointing to the first element of the multiset called MyMSet.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<string> MyMSet{"Alpha","Coding","Skills"};
  multiset<string>::const_iterator cit;

  cit = MyMSet.cbegin();
  cout<<*cit<<" ";
  cit++;
  cout<<*cit<<" ";
  cit++;
  cout<<*cit<<" ";
  return 0;
}

The output of the above code will be:

Alpha Coding Skills

Example:

Lets see another example where the multiset called MyMSet contains integer values and multiset::cbegin function is used with multiset::cend function to specify a range including all elements of the multiset container. Please note that, Multiset is an ordered data container.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  multiset<int> MyMSet{55, 25, 128, 5, 72, 55};
  multiset<int>::const_iterator cit;

  for(cit = MyMSet.cbegin(); cit != MyMSet.cend(); ++cit)
    cout<<*cit<<" ";

  return 0;
}

The output of the above code will be:

5 25 55 55 72 128 

❮ C++ <set> Library