Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ set::end function is used to return the iterator pointing to the past-the-last element of the set container. The past-the-last element of the set is the theoretical element that follows the last element. It does not point to any element, and hence could not be dereferenced.

C++ begin end

Note: Set is an ordered data container which implies all its elements are ordered all the time.

Syntax

iterator end();
const_iterator end() const;
iterator end() noexcept;
const_iterator end() const noexcept;

Parameters

No parameter is required.

Return Value

An iterator to the past-the-last element 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 below example, the set::end function is used to return the iterator pointing to the past-the-last element of the set MySet.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<string> MySet{"Alpha","Coding","Skills"};
  set<string>::iterator it;

  it = MySet.end();
  it--;
  cout<<*it<<" ";
  it--;
  cout<<*it<<" ";
  it--;
  cout<<*it<<" ";
  return 0;
}

The output of the above code will be:

Skills Coding Alpha

Example:

Lets see another example where the set called MySet contains integer values and set::end function is used with set::begin function to specify a range including all elements of the set container. Please note that, Set is an ordered data container.

#include <iostream>
#include <set>
using namespace std;
 
int main (){
  set<int> MySet{55, 25, 128, 5, 72};
  set<int>::iterator it;

  for(it = MySet.begin(); it != MySet.end(); ++it)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

5 25 55 72 128  

❮ C++ - Set