![]() |
VOOZH | about |
In C++, std::set::begin() and std::set::end() are built-in functions used to retrieve set::iterators to the beginning and the end of the set container. Set uses bidirectional iterators, so the iterators returned by these functions support the dereferencing, increment, decrement, relational, and equality operations only.
They are the member functions of the std::set class defined inside <set> header file.
Example:
9 56
You may have noticed we have decremented the set::end() iterator before dereferencing. The reason for this is mentioned below:
The std::set::begin() method is used to retrieve a set::iterator pointing to the first element of the std::set container.
s.begin();
Parameters
Return Value
The std::set::end() is used to retrieve a std::iterator pointing to the theoretical element that is just after the last element of the std::set container. It does not point to the last element, but can by decrementing.
s.end()
Parameters
Return value
Here we will learn how to implement set::begin() and set::end()
We can traverse the set by incrementing and dereferencing the set::begin() iterator till it is not equal to the set::end() iterator.
9 11 15 56
4
The following table lists some primary differences between the std::set::begin() and std::set::end() methods:
| set::begin() | set::end() |
|---|---|
| Returns an iterator to the first element in the set container. | Rturn an iterator to one element after the last element in the set container. |
Syntax: s.begin(); | Syntax: s.end(); |
We can dereference set::begin() iterator, because it point to the valid element. | We should not dereference set::end() iterator, because it does not point to valid element. |