Multiset Cbegin Function - C++ Programming Tutorial
C++ Course / STL Set & Map / Multiset Cbegin Function

Multiset Cbegin Function

BLUF: Mastering Multiset Cbegin Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Multiset Cbegin Function

C++ is renowned for its efficiency. Learn how Multiset Cbegin Function enables low-level control and high-performance computing in the tutorial below.

C++ multiset cbegin

The cbegin function in C++ for multiset is employed to retrieve a constant iterator that points to the initial element of the multiset container.

Syntax

Example

const_iterator cbegin() const noexcept;  //since C++ 11

A const_iterator is an iterator that points to read-only data in C++ tutorials.

Parameter

Return value

The cbegin method provides a const_iterator that points to the initial element of the multiset.

Complexity

Constant

Iterator validity

No changes.

Data Races

The container is accessed.

Simultaneously retrieving the elements from a multiset container is considered to be a secure operation.

Exception Safety

This member function never throws exception.

Example 1

Let's explore a basic illustration showcasing the cbegin function:

Example

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  multiset<string> mymultiset= {"Java", "C","SQL","C++" };

  // show content

  for (auto it = mymultiset.cbegin(); it != mymultiset.cend(); ++it)
    cout <<*it << '\n';
    
  return 0;
}

Output:

Output

C
C++
Java
SQL

In the provided illustration, the cbegin method is employed to yield a constant iterator that points to the initial element within the mymultiset multiset.

Example 2

Let's see a simple example:

Example

#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   multiset <int> s1;  
   multiset <int>::iterator s1_Iter;  
   multiset <int>::const_iterator s1_cIter;  
  
   s1.insert( 1 );  
   s1.insert( 2 );  
   s1.insert( 3 );  
   s1.insert( 2 );
   
   s1_Iter = s1.begin( );  
   cout << "The first element of s1 is " << *s1_Iter << endl;  
  
   s1_Iter = s1.begin( );  
   s1.erase( s1_Iter );  
  
   // The following 2 lines would err because the iterator is const  
   // s1_cIter = s1.begin( );  
   // s1.erase( s1_cIter );  
  
   s1_cIter = s1.begin( );  
   cout << "The first element of s1 is now " << *s1_cIter << endl;  
}

Output:

Output

The first element of s1 is 1
The first element of s1 is now 2

Example 3

Let's explore a basic illustration of traversing through the multiset utilizing a while loop:

Example

#include <iostream>
#include <set>
#include <string>

int main()
{
    using namespace std;
 
      multiset<string> mymultiset = {"Robin","Dolly", "John","Nikita","Nikita"};

    multiset<string>::const_iterator it; // declare an iterator

    it = mymultiset.cbegin(); // assign it to the start of the vector

    while (it != mymultiset.cend()) // while it hasn't reach the end
    {
        cout << *it<< "\n"; 
    // print the value of the element icpp tutorials to
        ++it; // and iterate to the next element
    }
    cout << endl;
}

Output:

Output

Dolly
John
Nikita
Nikita
Robin

In the previous example, the cbegin method is employed to retrieve an iterator indicating the initial element in the mymultiset data structure.

Example 4

Let's see another simple example:

Example

#include <iostream>
#include <string>
#include <set>

using namespace std;

int main ()
{
  multiset<int> number = {400, 350, 465, 290, 410, 400};

   cout << "Increasing order: " << '\n';
   cout<<"______________________\n";
   
  multiset<int>::const_iterator cit;
  for (cit=number.cbegin(); cit!=number.cend(); ++cit)
    cout << *cit<< '\n';

    auto low = number.cbegin();
    auto high = number.rbegin();
    
    cout << "\nSmallest Number is: "<< *low <<endl;
    cout<< "Biggest Number is: "<<*high <<endl;

  return 0;
  }

Output:

Output

Increasing order: 
______________________
290
350
400
400
410
465

Smallest Number is: 290
Biggest Number is: 465

In the previous example, the cbegin method is employed to retrieve an iterator that points to the initial element within the mymultiset multiset.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience