Set Begin and Set End in C++ STL



The begin() and end() functions are member functions of the std::set container, which is defined in the <set> header file.

The std::set in C++ STL (Standard Template Library) always stores its elements in sorted order. So when you use begin() and end() functions, the elements in the set, which are automatically arranged in sorted order (from smallest to largest by default) retrieve the smallest as the first element and largest as the last element.

The begin() and end() functions are used to loop through all elements of the set.

The std::set uses bidirectional iterators, where you can:

  • Move the iterator forward (++it) or backward (--it)
  • Access the value pointed to by the iterator using *it
  • Compare iterators using relational (==, !=, etc.) and equality operators.

The set::begin() Function

The begin() function is used to return an iterator pointing to the first(smallest element) in the set.

Syntax

Following is the syntax to the set::begin() function in C++:

iterator begin();
const_iterator begin() const;

Parameters

This function doesn't accept any parameters

Return Value

This function returns an iterator pointing to the first element in the set.

Example

This program creates a set of integers by inserting multiple values into it, and prints the smallest element using the begin() function, as the "set" automatically keeps its elements in sorted order by default.

#include<iostream>
#include<set>
using namespace std;
int main() {
   set<int> s;
   set<int>::iterator it; 
   
   s.insert(7);
   s.insert(6);
   s.insert(1);
   s.insert(4);
   s.insert(2);
   s.insert(9);
   s.insert(10);
   
   it = s.begin(); 
   cout<<"The first element in the set: "<<*it<<endl;
   return 0;
}

Following is the output to the above program

The first element in the set: 1

The set::end() Function

The end() function is used to get an iterator that points just past the last (largest) element in the set, which is stored in sorted order by default.

Syntax

iterator end();
const_iterator end() const;

Parameters

This function doesn't accept any parameters

Return Value

This function returns an iterator pointing to the last element in the set.

Example

This program creates a set of integers by inserting multiple values into it, and prints the largest element using the end() function, as the "set" automatically keeps its elements in sorted order by default.

#include<iostream>
#include<set>
using namespace std;
int main() {
   set<int>s;
   set<int>::iterator it; 
   
   s.insert(7);
   s.insert(6);
   s.insert(1);
   s.insert(4);
   s.insert(2);
   s.insert(9);
   s.insert(10);
   it = --s.end();  
   cout<<"The last element in the set: "<<*it<<endl;
   
   return 0;
}

Following is the output to the above program

The last element in the set: 10

Using Both set::begin() and set::end() Functions

The set::begin() and set::end() functions are commonly used together to traverse the set from beginning to end.

Example

This program creates a set of integers by storing the values in sorted order, and uses a loop with begin() and end() functions to print all the elements from start to end.

#include<iostream>
#include<set>
using namespace std;
int main() {
   set<int>s;
   set<int>::iterator it;
   s.insert(7);
   s.insert(6);
   s.insert(1);
   s.insert(4);
   s.insert(2);
   s.insert(9);
   s.insert(10);
   
   for (auto it=s.begin(); it != s.end(); ++it)
      cout <<' '<< *it;
   return 0;
}

Following is the output to the above program

1 2 4 6 7 9 10
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-06-12T14:01:51+05:30

972 Views

Kickstart Your Career

Get certified by completing the course

Get Started