Set

A set container stores only unique values in a sorted fashion. A set organizes the values using the value as a key. The set container is immutable, that is, the values stored in a set can't be modified; however, the values can be deleted. A set generally uses a red-black tree data structure, which is a form of balanced BST. The time complexity of set operations are guaranteed to be O ( log N )

Let's write a simple program using a set:

#include <iostream>#include <set>#include <vector>#include <iterator>#include <algorithm>using namespace std;int main( ) {    set<int> s1 = { 1, 3, 5, 7, 9 };    set<int> s2 = { 2, 3, 7, 8, 10 };    vector<int> v( s1.size() + s2.size() );            cout << "\nFirst set values are ..." << endl; copy ( s1.begin(), s1.end(), ...

Get Mastering C++ Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.