O'Reilly logo

Mastering C++ Programming by Jeganathan Swaminathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required