In C++, a set as typed like this, is a set literal or an array literal. It is also the initializer_list. In C++, an iterator is a class. Though it is a class, its object behaves like a pointer. When it is incremented, it points to the next element. When it is decremented, it points to the previous element. Just as the pointer can be dereferenced by the indirection operator, the iterator can also be dereferenced in the same way. There are different types of iterators for the set in C++. This article explains the different iterators for the set and how to use them.
A program to code the above set, or any set, should begin with the following:
#include <set>
#include <string>
using namespace std;
The first line includes the iostream library. This is needed for the terminal (console). The second line includes the set library. This is needed for set programming. The third line includes the string library. To use strings, the string class has to be included; otherwise, it is the pointers to the strings that will be sorted and not the string alphabetic literals themselves. These are all sub-libraries from the main standard library, in C++. STD in the title of this article means standard. The fourth line is not a directive. It is a statement. It insists that any name used in the program that is not preceded by user namespace is from the standard namespace.
The iterator class does not have to be included. It is already in the set class.
Note: After values have been inserted into the set, they are sorted internally in ascending order with default settings.
Iterator
This iterator class object is returned by the begin() or end() member functions of the set class. The begin() member function returns an iterator that points to the first element of the set. The end() member function returns an iterator that points just after the last element of the set.
This iterator works with the == or != operator, but does not work with the <= and >= operators. Though this iterator is not officially constant, the value it points to may not be changed. The following code shows how to use this iterator:
#include <set>
#include <string>
using namespace std;
int main()
{
set<string> st({"passion fruit", "banana", "watermelon", "blackberry", "grape"});
for (set<string>::iterator iter = st.begin(); iter != st.end(); iter++)
cout << *iter << ", ";
cout << endl;
return 0;
}
The output is:
banana, blackberry, grape, passion fruit, watermelon,
In order to change (modify) the value of a set, the set’s erase function has to be used to erase the element. After that, a new value can be inserted. After insertion, there will be internal sorting, and the value may not fit exactly where the old value was. Modifying or changing the value (or element) of a set, is discussion, for some other time – see later.
reverse_iterator
This is the opposite of the above iterator. This reverse_iterator class object is returned by the rbegin() or rend() member functions of the set class. The rbegin() member function returns an iterator that points to the last element of the set. The rend() member function returns an iterator that points just before the first element of the set.
This reverse_iterator works with the == or != operator, but does not work with the <= and >= operators. Though this iterator is not officially constant, the value it points to may not be changed. The following code shows how to use this iterator:
for (set<string>::reverse_iterator iter = st.rbegin(); iter != st.rend(); iter++)
cout << *iter << ", ";
cout << endl;
The output is:
sorted in reversed order.
const_iterator
This const_iterator class object is returned by the cbegin() or cend() member functions of the set class. The rbegin() member function returns a const_iterator that points to the first element of the set. The rend() member function returns a const_iterator that points just after the last element of the set.
This const_iterator works with the == or != operator, but does not work with the <= and >= operators. This iterator is officially constant and the value it points to cannot be changed. The following code shows how to use this iterator:
for (set<string>::const_iterator iter = st.cbegin(); iter != st.cend(); iter++)
cout << *iter << ", ";
cout << endl;
The output is:
const_reverse_iterator
This is the opposite of the above iterator. This const_reverse_iterator class object is returned by the crbegin() or crend() member functions of the set class. The crbegin() member function returns an iterator that points to the last element of the set. The crend() member function returns an iterator that points just before the first element of the set.
This const_reverse_iterator works with the == or != operator, but does not work with the <= and >= operators. This iterator is officially constant, and the value it points to cannot be changed. The following code shows how to use this iterator:
for (set<string>::const_reverse_iterator iter = st.crbegin(); iter != st.crend(); iter++)
cout << *iter << ", ";
cout << endl;
The output is
sorted in reversed order.
const_iterator cbegin() and cend()
cbegin() returns an unconditional constant iterator to the first element of the set. cend() returns an unconditional constant iterator that is just after the last element of the set. The following code shows how to use it:
for (set<string>::const_iterator iter = st.cbegin(); iter != st.cend(); iter++)
cout << *iter << ", ";
cout << endl;
The output is:
const_reverse_iterator<strong> crbegin() and crend()</strong>
This is the opposite of the above. The following code shows how to use it:
for (set<string>::const_reverse_iterator iter = st.crbegin(); iter != st.crend(); iter++)
cout << *iter << ", ";
cout << endl;
The output is:
Conclusion
All the iterators returned by the member functions of the set object work with the == or != operator, but do not work with the <= and >= operators. All of them can be incremented or decremented. All the iterators returned by the member functions of the set are directly or indirectly constant. This means that the values they point to cannot be changed using the iterator.
In order to change (modify) the value of a set, the set’s erase function has to be used to erase the element. After that, a new value can be inserted. After insertion, there will be internal sorting, and the value may not fit exactly where the old value was. Modifying or changing the value (or element) of a set is discussion for some other time – see later.