The Wayback Machine - https://web.archive.org/web/20240920163539/https://www.geeksforgeeks.org/setbegin-setend-c-stl/
Open In App

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

Last Updated : 08 May, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Previous Article
Next Article

Similar Reads

Why are Standard Iterator Ranges [begin, end) Instead of [begin, end]?
In C++, the Standard Template Library (STL) uses a half-open range convention [begin, end) for iterator ranges. This means the range includes the begin iterator but excludes the end iterator. A common question arises: Why does the STL adopt this convention instead of the fully closed range [begin, end]? In this article, we will learn the advantages
4 min read
array::begin() and array::end() in C++ STL
Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::begin() begin() function is used to return an iterator pointing to the first element of the array container. begin() function returns a bidirectional iterat
3 min read
deque::begin() and deque::end in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::begin()begin() function
3 min read
forward_list::begin() and forward_list::end() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, the forward list is more useful than other containers in insertion, removal, and moving operations (like sort) and allows time constant insertion and removal of elements. It differs from list by the fact that the forward list keeps track of the location of only the next eleme
3 min read
list::begin() and list::end() in C++ STL
Lists are containers used in C++ to store data in a non-contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::begin() begin() function is used to return an iterator pointing to the first element of the l
3 min read
map::begin() and end() in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::begin() begin() function is used to return an iterator pointing to the first element of the map container. begin() function returns a bidirectional iterator to the first elemen
3 min read
unordered_multimap begin() and end() function in C++ STL
The unordered_multimap::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the container or to the first element in one of its buckets. Syntax: unordered_multimap_name.begin(n) Parameters: The function accepts one parameter. If a parameter is passed, it returns an iterator pointing to the first eleme
5 min read
multimap::begin() and multimap::end() in C++ STL
multimap::begin() is a built-in function in C++ STL that returns an iterator referring to the first element in the multimap container. Since the multimap container contains the element in an ordered way, begin() will point to that element that will come first according to the container's sorting criterion. Syntax: multimap_name.begin() Parameters:
3 min read
multiset begin() and end() function in C++ STL
The multiset::begin() is a built-in function in C++ STL that returns an iterator pointing to the first element in the multiset container. Since multiset always contains elements in an ordered way, begin() always points to the first element according to the sorting criterion. Syntax: iterator multiset_name.begin() Parameters: The function does not a
2 min read
match_results begin() and end() function in C++ STL
match_results::cbegin() is an inbuilt function in C++ STL that returns an iterator that points to the first match in the match_results object. Syntax: smatch_name.begin()Parameters: This function does not accept any parameters. Return value: This function returns an iterator pointing to the first match in the match_results object. The matches conta
3 min read
vector::begin() and vector::end() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::begin()begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional
3 min read
unordered_set begin() function in C++ STL
The unordered_set::begin() method is a builtin function in C++ STL which is used to return an iterator pointing to the first element in the unordered_set container. All of the iterators of an unordered_set can be used to only access the elements, iterators are not allowed to modify elements present in an unordered_set container. Note: This iterator
2 min read
unordered_multiset begin() function in C++ STL
The unordered_multiset::begin() is a built-in function in C++ STL which returns an iterator pointing to the first element in the container or to the first element in one of its bucket. Syntax: unordered_multiset_name.begin(n) Parameters: The function accepts one parameter. If a parameter is passed, it returns an iterator pointing to the first eleme
2 min read
Difference Between deque::begin and deque::assign in C++
Deque or Double-ended queue are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. Here we will see the difference
3 min read
unordered_map begin() in C++
The unordered_map::begin() is a built-in function in C++ STL which returns an iterator pointing to the first element in the unordered_map container or in any of its bucket. Syntax for first element in unordered_map container: unordered_map.begin() Parameters: This function does not accepts any parameters. Return Value: The function returns an itera
2 min read
valarray begin() function in C++
The begin() function is defined in valarray header file. This function returns an iterator pointing to the first element in the valarray v. Syntax: template&lt; class T &gt; begin( valarray&lt;T&gt;&amp; v ); Parameter: This function takes a mandatory parameter v which represents the valarray object. Returns: This function returns the iterator to t
1 min read
list end() function in C++ STL
The list::end() is a built-in function in C++ STL which is used to get an iterator to past the last element. By past the last element it is meant that the iterator returned by the end() function return an iterator to an element which follows the last element in the list container. It can not be used to modify the element or the list container. This
2 min read
unordered_multiset end() function in C++ STL
The unordered_multiset::end() is a built-in function in C++ STL which returns an iterator pointing to the position immediately after the last element in the container or to the position immediately after the last element in one of its bucket. Syntax: unordered_multiset_name.end(n) Parameters: The function accepts one parameter. If a parameter is pa
2 min read
unordered_map end( ) function in C++ STL
The unordered_map::end() is a built-in function in C++ STL which returns an iterator pointing to the position past the last element in the container in the unordered_map container. In an unordered_map object, there is no guarantee that which specific element is considered its first element. But all the elements in the container are covered since th
3 min read
unordered_set end() in C++ STL
The unordered_set::end() function is a built-in function in C++ STL which returns an iterator pointing to the past-the-end-element. This iterator does not directly point to an element, rather it points to the location just after the last element. Syntax umap_name.end() or, umap_name.end(int i) Parameters: This function takes a single integer parame
2 min read
Count number of unique Triangles using STL | Set 1 (Using set)
We are given n triangles along with length of their three sides as a,b,c. Now we need to count number of unique triangles out of these n given triangles. Two triangles are different from one another if they have at least one of the sides different.Example: Input: arr[] = {{1, 2, 2}, {4, 5, 6}, {4, 5, 6} Output: 2 Input: arr[] = {{4, 5, 6}, {6, 5, 4
3 min read
set crbegin() and crend() function in C++ STL
The set::crbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the last element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator set_name.crbegin() Parameters: The funct
2 min read
set cbegin() and cend() function in C++ STL
The set::cbegin() is a built-in function in C++ STL which returns a constant iterator pointing to the first element in the container. The iterator cannot be used to modify the elements in the set container. The iterators can be increased or decreased to traverse the set accordingly. Syntax: constant_iterator set_name.cbegin() Parameters: The functi
2 min read
numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
The numeric header is part of the numeric library in C++ STL. This library consists of basic mathematical functions and types, as well as optimized numeric arrays and support for random number generation. Some of the functions in the numeric header: iotaaccumulatereduceinner_productpartial_sum etc. See this article for more reference: accumulate()
4 min read
Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
We have introduced Graph basics in Graph and its representations. In this post, a different STL-based representation is used that can be helpful to quickly implement graphs using vectors. The implementation is for the adjacency list representation of the graph. Following is an example undirected and unweighted graph with 5 vertices. Below is an adj
7 min read
C++ STL Set Insertion and Deletion
Prerequisite: Set A Set is a container implemented in C++ language in STL and has a concept similar to how the set is defined in mathematics. The fact that separates the set from the other containers is that it contains only the distinct elements and elements can be traversed in sorted order. Having the stronghold on sets is useful in competitive p
7 min read
set::swap() in C++ STL
Sets are a type of associative containers 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::swap() This function is used to exchange the contents of two sets
2 min read
set::size() in C++ STL
Sets are containers that store unique elements following a specific order. Internally, the elements in a set are always sorted. Sets are typically implemented as binary search trees. The set class provides a member function, set::size() function is used to return the size of the set container or the number of elements in the set container. Syntaxse
1 min read
set::empty() in C++ STL
Sets are a type of associative containers 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::empty() empty() function is used to check if the set container is
2 min read
set::emplace() in C++ STL
Sets are a type of associative containers 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::emplace() This function is used to insert a new element into the s
4 min read
Article Tags :
Practice Tags :