Multiset Cbegin Function

C++ multiset cbegin

C++ multiset cbegin function is used to return a constant iterator pointing to the first element of the multiset container.

Syntax

Example

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

A const_iterator is an iterator thacpp tutorials to constant content.

Parameter

Return value

The cbegin funtion returns a const_iterator pointing to the first element of the multiset.

Complexity

Constant

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of a multiset container is safe.

Exception Safety

This member function never throws exception.

Example 1

Let's see the simple example for 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 above example, cbegin function is used to return a constant iterator pointing to the first element in 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 see a simple example to iterate over the multiset using 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 above example, cbegin function is used to return an iterator pointing to the first element in the mymultiset multiset.

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 above example, cbegin function is used to return an iterator pointing to the first element in the mymultiset multiset.

Input Required

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