...

/

Set and Map Templates in C++

Set and Map Templates in C++

Learn to use the set and map templates of the STL.

We'll cover the following...

Whatever data structures we implement or algorithms we use, we need to remember that time complexity is a significant factor. In this lesson, we’ll look at set and then map. Both belong to the associative container classes. Because both implement sorted data structures, the search operation takes less time.

STL container set

Let’s explore how the set data structures from the STL fit in with their key algorithms.

#include <iostream>
#include <string>
#include <set>

using namespace std;
//we will see a lot of set algorithms in this example

int main(){
    // first we need an empty set container
    set <int, greater <int> > setOne;

    // now we can insert elements in random order
    // however, the numbers are unique
    setOne.insert(55);
    setOne.insert(89);
    setOne.insert(1);
    setOne.insert(41);
    setOne.insert(74);
    setOne.insert(23);
    setOne.insert(32);

    // to print set one, we need an iterator
    set <int, greater <int> > :: iterator itr;
    cout << "The iterator automatically orders the arrangement." << endl;
    cout << "Set one is now in descending order : ";
    for (itr = setOne.begin(); itr != setOne.end(); ++itr){
        cout << *itr << ", ";
    }
    cout << endl;

    // we can use a special algorithm to reverse the order
    // to do that, we need to assign the values of set one
    // to another set called set two
    set <int> setTwo(setOne.begin(), setOne.end());
    cout << endl;

    // now we can display the value of set two
    cout << "Set two is now in ascending order : ";
    for (itr = setTwo.begin(); itr != setTwo.end(); ++itr){
        cout << *itr << ", ";
    }
    cout << endl;

    // we can remove elements below 41 from set two
    cout << "Removing values below 41 from set two: " << endl;
    cout << "Now set two will start from 41: " << endl;
    cout << "Now set two looks like this: " << endl;
    setTwo.erase(setTwo.begin(), setTwo.find(41));
    for (itr = setTwo.begin(); itr != setTwo.end(); ++itr){
        cout << *itr << ", ";
    }
    cout << endl;

    // we can remove any particular element whose value is 41 in set two
    setTwo.erase (41);
    cout << "Erasing 41 from set two" << endl;
    cout << "Now set two will start from the value after 41 : " << endl;
    cout << "Now set two looks like this: " << endl;
    for (itr = setTwo.begin(); itr != setTwo.end(); ++itr){
        cout << *itr << ", ";
    }

    cout << endl;
    cout << "None of the algorithms applied on set two have affected set one : " << endl;
    cout << "Set one remains the same; it looks like this: " << endl;
    for (itr = setOne.begin(); itr != setOne.end(); ++itr){
            cout << *itr << ", ";
    }
    cout << endl;
    return 0;
}
C++ code for the set STL associative container

As we can see, many different types of algorithms are applied in set. Moreover, it implements various data structures. But we can always change that order quite easily. We get the following output when we run the code above:

The iterator automatically orders the arrangement.
Set one is now in descending order : 89, 74,
...