C++ Standard Library C++ STL Library

C++ multiset - begin() Function



The C++ multiset::begin function returns the iterator pointing to the first element of the multiset. Please note that, Multiset is an ordered data container which implies all its elements are ordered all the time.

C++ begin end

Syntax

iterator begin();
const_iterator begin() const;
iterator begin() noexcept;
const_iterator begin() const noexcept;

Parameters

No parameter is required.

Return Value

An iterator to the beginning of the sequence container. If the sequence object is constant qualified, the function returns a const_iterator, else returns an iterator.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the multiset::begin function returns the 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>::iterator it;

  it = MyMSet.begin();
  cout<<*it<<" ";
  it++;
  cout<<*it<<" ";
  it++;
  cout<<*it<<" ";
  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::begin function is used with multiset::end 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>::iterator it;

  cout<<"MyMSet contains: ";
  for(it = MyMSet.begin(); it != MyMSet.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

MyMSet contains: 5 25 55 55 72 128 

❮ C++ <set> Library