set::begin() and set::end() in C++ STL

17 Jun, 2022
Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. 
begin() function is used to return an iterator pointing to the first element of the set container. begin() function returns a bidirectional iterator to the first element of the container.
Syntax: 
setname.begin()

Parameters :
No parameters are passed.

Return Type:
This function returns a bidirectional
iterator pointing to the first element.
Examples:  
Input  : myset{1, 2, 3, 4, 5};
         myset.begin();
Output : returns an iterator to the element 1

Input  : myset{8, 7};
         myset.end();
Output : returns an iterator to past-the-end element. 
Errors and Exceptions1. It has a no exception throw guarantee. 2. Shows error when a parameter is passed. 
CPP
// INTEGER SET EXAMPLE// CPP program to illustrate// Implementation of begin() function#include <iostream>#include <set>using namespace std; int main(){    // declaration of set container    set<int> myset{ 1, 2, 3, 4, 5 };     // using begin() to print set    for (auto it = myset.begin(); it !=                             myset.end(); ++it)        cout << ' ' << *it;    return 0;}
Output: 
1 2 3 4 5
CPP
// CHARACTER SET EXAMPLE// CPP program to illustrate// Implementation of begin() function#include <iostream>#include <set>using namespace std; int main(){    // declaration of set container    set<char> myset{ 'a', 'c', 'g', 'z' };     // using begin() to print set    for (auto it = myset.begin(); it !=                           myset.end(); ++it)        cout << ' ' << *it;    return 0;}
Output: 
a c g z 
CPP
// STRING SET EXAMPLE// CPP program to illustrate// Implementation of begin() function#include <iostream>#include <set>#include <string>using namespace std; int main(){    // declaration of set container    set<string> myset{ "This", "is",                            "Geeksforgeeks" };     // using begin() to print set    for (auto it = myset.begin(); it !=                               myset.end(); ++it)        cout << ' ' << *it;    return 0;}
Output: 
Geeksforgeeks This is 
Time Complexity: O(1) 
It returns an iterator pointing to past the last element of the set container. Since it does not refer to a valid element, it cannot de-referenced end() function returns a bidirectional iterator.Syntax :  
setname.end()
Parameters :
No parameters are passed.
Returns :
This function returns a bidirectional
iterator pointing to next of the last element.
Examples:  
Input  : myset{1, 2, 3, 4, 5};
         myset.end();
Output : returns an iterator to next of 5
Errors and Exceptions1. It has a no exception throw guarantee. 2. Shows error when a parameter is passed. 
CPP
// INTEGER Example// CPP program to illustrate// Implementation of end() function#include <iostream>#include <set>using namespace std; int main(){    // declaration of set container    set<int> myset{ 1, 2, 3, 4, 5 };     // using end() to print set    for (auto it = myset.begin(); it !=                          myset.end(); ++it)        cout << ' ' << *it;    return 0;}
Output: 
1 2 3 4 5
CPP
// CHARACTER SET EXAMPLE// CPP program to illustrate// Implementation of begin() function#include <iostream>#include <set>using namespace std;  int main(){    // declaration of set container    set<char> myset{'a', 'c', 'g', 'z'};         // using begin() to print set    for (auto it=myset.begin(); it != myset.end(); ++it)        cout << ' ' << *it;    return 0;}
Output: 
a c g z
CPP
// STRING SET EXAMPLE// CPP program to illustrate// Implementation of begin() function#include <iostream>#include <set>#include <string> using namespace std; int main(){    // declaration of set container    set<string> myset{ "This", "is", "Geeksforgeeks" };     // using begin() to print set    for (auto it = myset.begin(); it != myset.end(); ++it)        cout << ' ' << *it;    return 0;}
Output: 
Geeksforgeeks This is 
Time Complexity: O(1) 
Let us see the differences in a tabular form -:
Its syntax is -:
iterator begin();
Its syntax is -:
iterator end();
jai_balayya
gauravkumarcs2828
dark_hunter
mayank007rawa
cpp-set
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
std::string class in C++
Pair in C++ Standard Template Library (STL)
Queue in C++ Standard Template Library (STL)
Unordered Sets in C++ Standard Template Library
List in C++ Standard Template Library (STL)
std::find in C++
Inline Functions in C++