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

Set Begin Function

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

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

C++ set begin

The begin function in C++ set is employed to retrieve an iterator pointing to the initial element within the set data structure.

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 returns an iterator that references the initial element of the set.

Complexity

Constant.

Iterator validity

No changes.

Data Races

Accessing the container does not result in any modifications, regardless of whether the constant or non-constant versions are used.

Exception Safety

This function never throws exceptions.

Example 1

Let's explore a straightforward illustration showcasing the begin function:

Example

#include <iostream>
#include <set>

using namespace std;

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

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

  return 0;
}

Output:

Output

Contents of myset are: 
C++
Java
SQL

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

Example 2

Let's see a simple example:

Example

#include <iostream>
#include <set>

using namespace std;

int main()
{
  set<int> c;
  c.insert(5);
  c.insert(2);
  c.insert(4);
  c.insert(1);
  c.insert(0);
  c.insert(9);

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

Output:

Output

0 1 2 4 5 9

Example 3

Let's examine a basic illustration of iterating through a set using a while loop:

Example

#include <iostream>
#include <set>
#include <string>
int main()
{
    using namespace std;
 
      set<string> myset = { "Nikita","Deep","Priya","Suman","Aman" };

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

    it = myset.begin(); // assign it to the start of the set

    while (it != myset.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 myset are: 
Aman
Deep
Nikita
Priya
Suman

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

Example 4

Let's see a simple example:

Example

#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   set <int> s1;  
   set <int>::iterator s1_Iter;    
  
   s1.insert( 1 );  
   s1.insert( 2 );  
   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

In the example provided, the begin method is employed to retrieve an iterator that points to the initial element within the myset set.

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