The input characters here are unsorted. This set can be created with the following statement:
This is a set of chars. It is possible to have a set of another type. Whatever is the case to do set coding, the C++ set library has to be included into the program. Consider the following program:
#include <set>
using namespace std;
int main()
{
setst = {'E', 'A', 'D', 'B', 'C'};
for (set::iterator iter = st.begin(); iter != st.end(); iter++)
cout<< *iter<< ", ";
cout<<endl;
return 0;
}
The output is:
The output is sorted ascending when the input was not sorted. After elements have been inserted into a set, they become sorted. With default setting, as in the above program, the sort is ascending.
The above program began with the inclusion of the iostream library. This is needed for use with the terminal (console). The next line is another directive that includes the set library. The line after is not a directive. It is a statement ending with a semicolon insisting that any name not preceded by “std::” is from the standard namespace.
The header lines are followed by the C++ main() function. The first statement in the main function declares the set. The second code segment displays the values of the set, which should have undergone internal sorting, by C++.
Having Set Sorted Ascending
In the standard namespace, the syntax to construct a set is actually:
There are three template specializations here. If the last one is not given by the programmer, the default value is chosen by C++. If the last and second one is not given by the programmer, their default values are chosen. The default value for the second specialization is “less<Key>”, which means, sort ascending. If omitted, the set is still sorted ascending. If present as “less<Key>”, the set is sorted ascending, as the following program shows:
#include <set>
using namespace std;
int main()
{
set<char, less>st = {'E', 'A', 'D', 'B', 'C'};
for (set::iterator iter = st.begin(); iter != st.end(); iter++)
cout<< *iter<< ", ";
cout<<endl;
return 0;
}
Notice that “char” is in the place of “key” in “less<Key>”. The output is:
sorted ascending. The program begins with the inclusion of the iostream library. This is needed for use with the terminal (console). The next line is another directive that includes the set library. The line after is not a directive. It is a statement ending with a semicolon insisting that any name not preceded by “std::” is from the standard namespace.
The header lines are followed by the C++ main() function. The first statement in the main function declares the set using “less<char>” as the second template specialization. The second code segment displays the values of the set, which should have undergone internal sorting appropriately, by C++.
Having Set Sorted Descending
To have a set sorted descending, the second specialization must be included. It is “greater<Key>”, where “Key” is replaced by the data type. Less<Key> and greater<Key> are predefined functions in the set library. The following program results in a set that is sorted descending:
#include
using namespace std;
int main()
{
set<char, greater>st = {'E', 'A', 'D', 'B', 'C'};
for (set::iterator iter = st.begin(); iter != st.end(); iter++)
cout<< *iter<< ", ";
cout<<endl;
return 0;
}
The output is:
sorted descending. The program begins with the inclusion of the iostream library. This is needed for use with the terminal (console). The next line is another directive that includes the set library. The line after is not a directive. It is a statement ending with a semicolon, insisting that any name not preceded by “std::” is of the standard namespace.
The header lines are followed by the C++ main() function. The first statement in the main function declares the set using “greater<char>” as the second template specialization. The second code segment displays the values of the set, which should have undergone internal sorting appropriately, by C++.
Observers
The syntaxes for the set observers, are:
and
key_compare key_comp() const
Consider the following code segment:
set<char, less<char>> st = {'E', 'A', 'D', 'B', 'C'};
bool bl = st.key_comp()('C', 'D');
cout << bl << endl;
The output is: 1, for true.
key_comp() is a member function of the set class. It does not take any argument. It returns a function object which is a function that takes two arguments. The function object (call) is identified in the second statement above as “st.key_comp()()”. Its arguments are expected to be elements of the set after internal sorting based on the Compare template specialization.
If its first argument comes first in the set after internal sorting, then the function object will return true, otherwise, it will return false. All that is coded in the second statement above.
If the Compare template specialization had been “greater<char>”, then the output would have been 0, for false.
This concerns the values of the set of key/value pairs – see later.
Conclusion
After elements have been inserted into a set in C++, they are immediately sorted internally. If the Compare template specialization is “less<Key>”, which is the default, and can be omitted, then the sorting will be done ascending. If it is “greater<Key>”, then the sorting will be done descending. “Key” in these expressions is replaced by the type of values in the set. The values are of one type.
So, a set does not need a sort member function because the values are always sorted. When a set is created with some initial elements, these elements are sorted. Any insert of any element after that causes re-sorting. The values of a set as the one described above are called keys. However, some sets can have key/value pairs – see later.