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

Multiset Begin Function

BLUF: Mastering Multiset Begin 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 Begin Function

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

C++ multiset begin

The begin function in C++ multiset is utilized to retrieve an iterator pointing to the initial element within the multiset container.

Syntax

Example

iterator begin();                            //until C++ 11

const_iterator begin() const;                //until C++ 11

      iterator begin() noexcept;              //since C++ 11

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

Parameter

Return value

It yields an iterator referencing the initial element of the multiset.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is retrieved. Both the constant and non-constant variations do not alter the container.

Exception Safety

This function never throws exceptions.

Example 1

Let's explore a basic illustration showcasing the functionality of the begin method:

Example

#include <iostream>
#include <set>

using namespace std;

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

  // show content:
  cout<<"Contents of mymultiset are: "<<endl;
  for (multiset<string>::iterator it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it<< '\n';

  return 0;
}

Output:

Output

Contents of mymultiset are: 
C++
C++
Java
SQL

The begin method is employed to retrieve an iterator that points to the initial element within the mymultiset multiset.

Example 2

Let's see a simple example:

Example

#include <iostream>
#include <set>

using namespace std;

int main()
{
  multiset<int> c;
  c.insert(5);
  c.insert(2);
  c.insert(4);
  c.insert(2);
  c.insert(0);
  c.insert(5);

  multiset<int>::iterator i = c.begin();
  while (i != c.end())
  cout << *i++ << " ";
  cout << endl;
}

Output:

Output

0 2 2 4 5 5

Example 3

Let's examine a basic illustration of iterating through the multiset utilizing a while loop:

Example

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

int main()
{
    using namespace std;
 
      multiset<string> mymultiset = { "Nikita","Deep","Priya","Suman","Aman","Nikita" };

 cout<<"Elements of mymultiset are: "<<endl;
    multiset<string>::const_iterator it; // declare an iterator

    it = mymultiset.begin(); // assign it to the start of the multiset

    while (it != mymultiset.end()) // 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

Elements of mymultiset are: 
Aman
Deep
Nikita
Nikita
Priya
Suman

The start method is employed to retrieve an iterator that points to the initial element within the mymultiset multiset.

Example 4

Let's see a simple example:

Example

#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   multiset <int> s1;  
   multiset <int>::iterator s1_Iter;    
  
   s1.insert( 1 );  
   s1.insert( 2 );  
   s1.insert( 3 ); 
   s1.insert( 3 );
  
   s1_Iter = s1.begin( );  
   cout << "The first element of s1 is " << *s1_Iter << endl;  
  
   s1_Iter = s1.begin( );  
   s1.erase( s1_Iter );  

   s1_Iter = s1.begin( );  
   cout << "The first element of s1 is now " << *s1_Iter << endl;  
}

Output:

Output

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

The begin 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