Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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.
 

set::begin()

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 Exceptions
1. 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) 

set::end()

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 Exceptions
1. 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(n) 

Let us see the differences in a tabular form -:

set::begin() set::end()
It is used to return an iterator referring to the first element in the set container. It is used to return an iterator referring to the past-the-end element in the set container.

Its syntax is -:

iterator begin();

Its syntax is -:

iterator end();
It does not take any parameters. It does not take any parameters.
Its complexity is constant. Its complexity is constant.
Its iterator validity does not change. Its iterator validity does not change.


Last Updated : 08 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads