C++

Sorting of Elements in a C++ Set

An example of a set is:

st = {'E', 'A', 'D', 'B', 'C'}

The input characters here are unsorted. This set can be created with the following statement:

set<char> st = {'E', 'A', 'D', 'B', 'C'};

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 <iostream>
    #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:

A, B, C, D, E,

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:

template<class Key, class Compare = less<Key>, class Allocator = allocator<Key>> class set;

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 <iostream>

#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:

A, B, C, D, E,

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 <iostream>
    #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:

E, D, C, B, A,

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:

key_compare key_comp() const;

and

value_compare value_comp() const;

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.

value_compare value_comp() const;

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.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.