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.
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. Returns : 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.begin();
Output : returns an iterator to the element 8
Errors and Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// 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
// 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
// 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 Exceptions
1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.
// 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
// 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
// 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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- fill() and fill_n() functions in C++ STL
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- accumulate() and partial_sum() in C++ STL : numeric header
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
- Descending order in Map and Multimap of C++ STL
- std::upper_bound and std::lower_bound for Vector in C++ STL
- Insertion and Deletion in STL Set C++
- std::minmax() and std::minmax_element() in C++ STL
- Find and print duplicate words in std::vector<string> using STL functions
- deque::at() and deque::swap() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- list::front() and list::back() in C++ STL
- list::empty() and list::size() in C++ STL
- list::remove() and list::remove_if() in C++ STL
- queue::empty() and queue::size() in C++ STL
- queue::push() and queue::pop() in C++ STL
- deque::pop_front() and deque::pop_back() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

