# Pythonic Operators on STL Set Algorithms

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

## Introduction

Ever since I started working with Python and that have gotten me into alot thinking how to redesign my libraries to be pythonic, if I were to implement them from scratch again. In this first article of the series, I want to introduce Python’s wonderful and intuitive operators for working with set algebra into C++ world. These operators are nothing more than syntatic-sugar to reduce the amount of code to write.

[url href="https://docs.python.org/2/library/sets.html" text="Table of Python Set Operators"]

[img src="PythonSetOperators.png" /]

## Set Intersection

[img height="138" src="set_intersection2.png" width="217" /]

[url href="http://en.cppreference.com/w/cpp/algorithm/set_intersection" text="C++ Reference: std::set_intersection"]

Commutative

set_intersection is an algorithm that produces a set of elements which are common to both sets. It is commutative, meaning that even the 2 sets are switched places, the algorithm returns&amp;nbsp;the same result.

void intersection_example()
{
std::vector&lt;int&gt; v1{ 1,2,3,4,5,6,7,8 };
std::vector&lt;int&gt; v2{ 5, 7, 9,10 };
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());

std::vector&lt;int&gt; v_intersection;

std::set_intersection(v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::back_inserter(v_intersection));

for (int n : v_intersection)
std::cout &lt;&lt; n &lt;&lt; ‘ ‘;
}

Output

5 7

This is the example using &amp; operator to do intersection.

void intersection_example()
{
std::vector&lt;int&gt; v1{ 1,2,3,4,5,6,7,8 };
std::vector&lt;int&gt; v2{ 5, 7, 9,10 };

std::vector&lt;int&gt; v_intersection = s(v1) &amp; s(v2);

for (int n : v_intersection)
std::cout &lt;&lt; n &lt;&lt; ‘ ‘;
}

I skip showing the output of the operator example as it is the same.

s is a function, not class. If s is to be a class, to instantiate it, a container type would have to be specified (See below).

std::vector&lt;int&gt; v_intersection = s&lt;std::vector&lt;int&gt;&gt;(v1) &amp; s&lt;std::vector&lt;int&gt;&gt;(v2);

In order to make use of automatic type deduction, s has to be a function that does nothing but returns the wrapper class.

#include &lt;algorithm&gt;
#include &lt;iterator&gt;

template&lt;typename T&gt;
struct wrapper
{
wrapper(T&amp; container) : cont(container) {}
T&amp; cont;
};

template&lt;typename T&gt;
wrapper&lt;T&gt; s(T&amp; s_cont)
{
return wrapper&lt;T&gt;(s_cont);
}

The &amp; operator function checks whether to sort the container. Since std::sort only works with random access iterators, so we cannot use this function with STL list and slist which has non-random access iterators. In my 15 years of work, I have not seen a single use of list in any codebase.

template&lt;typename T&gt;
T operator&amp;(wrapper&lt;T&gt;&amp; left, wrapper&lt;T&gt;&amp; right)
{
T&amp; c1 = left.cont;
T&amp; c2 = right.cont;
if (!std::is_sorted(c1.begin(), c1.end()))
std::sort(c1.begin(), c1.end());
if (!std::is_sorted(c2.begin(), c2.end()))
std::sort(c2.begin(), c2.end());

T v_intersection;

std::set_intersection(c1.begin(), c1.end(),
c2.begin(), c2.end(),
std::back_inserter(v_intersection));

return std::move(v_intersection);
}

All set algorithm precondition requires the ranges to be sorted, hence this is_sorted check.

## Set Union

[img height="138" src="set_union2.png" width="217" /]

[url href="http://en.cppreference.com/w/cpp/algorithm/set_union" text="C++ Reference: std::set_union"]

Commutative

set_union&amp;nbsp;is an algorithm that produces a set of elements from&amp;nbsp;both sets. For the elements appearing in intersection, it always picks them from the 1st set, not 2nd set.

void union_example()
{
std::vector&lt;int&gt; v1 = { 1, 2, 3, 4, 5 };
std::vector&lt;int&gt; v2 = { 3, 4, 5, 6, 7 };
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());

std::vector&lt;int&gt; dest1;

std::set_union(v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::back_inserter(dest1));

for (const auto &amp;i : dest1) {
std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
}
std::cout &lt;&lt; ‘n’;
}

Output

1 2 3 4 5 6 7

The code required to write is much lesser therefore the code is more concise.

void union_example()
{
std::vector&lt;int&gt; v1 = { 1, 2, 3, 4, 5 };
std::vector&lt;int&gt; v2 = { 3, 4, 5, 6, 7 };

std::vector&lt;int&gt; dest1 = s(v1) | s(v2);

for (int n : dest1)
std::cout &lt;&lt; n &lt;&lt; ‘ ‘;
}

The | operator is almost similar to &amp; operator except that algorithm is different.

template&lt;typename T&gt;
T operator|(wrapper&lt;T&gt;&amp; left, wrapper&lt;T&gt;&amp; right)
{
T&amp; c1 = left.cont;
T&amp; c2 = right.cont;
if (!std::is_sorted(c1.begin(), c1.end()))
std::sort(c1.begin(), c1.end());
if (!std::is_sorted(c2.begin(), c2.end()))
std::sort(c2.begin(), c2.end());

T dest1;

std::set_union(c1.begin(), c1.end(),
c2.begin(), c2.end(),
std::back_inserter(dest1));

return std::move(dest1);
}

## Set Difference

[img height="138" src="set_difference2.png" width="217" /]

[url href="http://en.cppreference.com/w/cpp/algorithm/set_difference" text="C++ Reference: std::set_difference"]

Non-Commutative

set_difference&amp;nbsp;returns&amp;nbsp;the elements in&amp;nbsp;1st set which is not in 2nd set and is represented by minus operator in Python. For obvious reason, the results is different when the arguments are swapped place. set_difference is non-commutative like minus operation.

void set_difference_example()
{
std::vector&lt;int&gt; v1{ 1, 2, 5, 5, 5, 9 };
std::vector&lt;int&gt; v2{ 2, 5, 7 };
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());

std::vector&lt;int&gt; diff;

std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(),
std::inserter(diff, diff.begin()));

for (auto i : v1) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;minus &amp;quot;;
for (auto i : v2) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;is: &amp;quot;;

for (auto i : diff) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; ‘n’;
}

Output

1 2 5 5 5 9 minus 2 5 7 is: 1 5 5 9

This is example with minus operator.

void set_difference_example()
{
std::vector&lt;int&gt; v1{ 1, 2, 5, 5, 5, 9 };
std::vector&lt;int&gt; v2{ 2, 5, 7 };

std::vector&lt;int&gt; diff = s(v1) – s(v2);

for (auto i : v1) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;minus &amp;quot;;
for (auto i : v2) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;is: &amp;quot;;

for (auto i : diff) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; ‘n’;
}

The code for minus operator is shown below.

template&lt;typename T&gt;
T operator-(wrapper&lt;T&gt;&amp; left, wrapper&lt;T&gt;&amp; right)
{
T&amp; c1 = left.cont;
T&amp; c2 = right.cont;
if (!std::is_sorted(c1.begin(), c1.end()))
std::sort(c1.begin(), c1.end());
if (!std::is_sorted(c2.begin(), c2.end()))
std::sort(c2.begin(), c2.end());

T diff;

std::set_difference(c1.begin(), c1.end(),
c2.begin(), c2.end(),
std::back_inserter(diff));

return std::move(diff);
}

## Set Symmetric Difference

[img height="138" src="set_sym_difference2.png" width="217" /]

[url href="http://en.cppreference.com/w/cpp/algorithm/set_symmetric_difference" text="C++ Reference: std::set_symmetric_difference"]

Commutative

set_symmetric_difference computes the elements in either&amp;nbsp;set but not both.

void set_symmetric_difference_example()
{
std::vector&lt;int&gt; v1{ 1,2,3,4,5,6,7,8 };
std::vector&lt;int&gt; v2{ 5, 7, 9,10 };
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());

std::vector&lt;int&gt; v_symDifference;

std::set_symmetric_difference(
v1.begin(), v1.end(),
v2.begin(), v2.end(),
std::back_inserter(v_symDifference));

for (int n : v_symDifference)
std::cout &lt;&lt; n &lt;&lt; ‘ ‘;
}

Output

1 2 3 4 6 8 9 10

set_symmetric_difference is represented by logical exclusive or operator.

void set_symmetric_difference_example()
{
std::vector&lt;int&gt; v1{ 1,2,3,4,5,6,7,8 };
std::vector&lt;int&gt; v2{ 5, 7, 9,10 };

std::vector&lt;int&gt; v_symDifference = s(v1) ^ s(v2);

for (int n : v_symDifference)
std::cout &lt;&lt; n &lt;&lt; ‘ ‘;
}

The code for logical exclusive or operator is shown below.

template&lt;typename T&gt;
T operator^(wrapper&lt;T&gt;&amp; left, wrapper&lt;T&gt;&amp; right)
{
T&amp; c1 = left.cont;
T&amp; c2 = right.cont;
if (!std::is_sorted(c1.begin(), c1.end()))
std::sort(c1.begin(), c1.end());
if (!std::is_sorted(c2.begin(), c2.end()))
std::sort(c2.begin(), c2.end());

T v_symDifference;

std::set_symmetric_difference(c1.begin(), c1.end(),
c2.begin(), c2.end(),
std::back_inserter(v_symDifference));

return std::move(v_symDifference);
}

## Superset and Subset

[img height="129" src="set_superset2.png" width="165" /]

[url href="http://en.cppreference.com/w/cpp/algorithm/includes" text="C++ Reference: std::includes"]

Non-Commutative

STL includes can be used to find out whether a set is a superset(returns a boolean). To check if it is subset, just switch the 2 sets.

void is_superset_example()
{
std::vector&lt;char&gt; v1{ ‘a’, ‘b’, ‘c’, ‘f’, ‘h’, ‘x’ };
std::vector&lt;char&gt; v2{ ‘a’, ‘b’, ‘c’ };
std::vector&lt;char&gt; v3{ ‘a’, ‘c’ };
std::vector&lt;char&gt; v4{ ‘g’ };
std::vector&lt;char&gt; v5{ ‘a’, ‘c’, ‘g’ };
std::sort(v1.begin(), v1.end());
std::sort(v2.begin(), v2.end());
std::sort(v3.begin(), v3.end());
std::sort(v4.begin(), v4.end());
std::sort(v5.begin(), v5.end());

for (auto i : v1) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;nincludes:n&amp;quot; &lt;&lt; std::boolalpha;

for (auto i : v2) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: &amp;quot;
&lt;&lt; std::includes(v1.begin(), v1.end(), v2.begin(), v2.end()) &lt;&lt; ‘n’;
for (auto i : v3) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: &amp;quot;
&lt;&lt; std::includes(v1.begin(), v1.end(), v3.begin(), v3.end()) &lt;&lt; ‘n’;
for (auto i : v4) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: &amp;quot;
&lt;&lt; std::includes(v1.begin(), v1.end(), v4.begin(), v4.end()) &lt;&lt; ‘n’;
for (auto i : v5) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: &amp;quot;
&lt;&lt; std::includes(v1.begin(), v1.end(), v5.begin(), v5.end()) &lt;&lt; ‘n’;

auto cmp_nocase = [](char a, char b) {
return std::tolower(a) &lt; std::tolower(b);
};

std::vector&lt;char&gt; v6{ ‘A’, ‘B’, ‘C’ };
for (auto i : v6) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: (case-insensitive) &amp;quot;
&lt;&lt; std::includes(v1.begin(), v1.end(), v6.begin(), v6.end(), cmp_nocase)
&lt;&lt; ‘n’;
}

Output

a b c f h x
includes:
a b c : true
a c : true
g : false
a c g : false
A B C : (case-insensitive) true

The &gt;= operator example is below. The &lt;= operator example is not shown in this article.

void is_superset_example()
{
std::vector&lt;char&gt; v1{ ‘a’, ‘b’, ‘c’, ‘f’, ‘h’, ‘x’ };
std::vector&lt;char&gt; v2{ ‘a’, ‘b’, ‘c’ };
std::vector&lt;char&gt; v3{ ‘a’, ‘c’ };
std::vector&lt;char&gt; v4{ ‘g’ };
std::vector&lt;char&gt; v5{ ‘a’, ‘c’, ‘g’ };

for (auto i : v1) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;nincludes:n&amp;quot; &lt;&lt; std::boolalpha;

for (auto i : v2) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: &amp;quot; &lt;&lt; (s(v1) &gt;= s(v2)) &lt;&lt; ‘n’;
for (auto i : v3) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: &amp;quot; &lt;&lt; (s(v1) &gt;= s(v3)) &lt;&lt; ‘n’;
for (auto i : v4) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: &amp;quot; &lt;&lt; (s(v1) &gt;= s(v4)) &lt;&lt; ‘n’;
for (auto i : v5) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: &amp;quot; &lt;&lt; (s(v1) &gt;= s(v5)) &lt;&lt; ‘n’;

auto cmp_nocase = [](char a, char b) {
return std::tolower(a) &lt; std::tolower(b);
};

std::vector&lt;char&gt; v6{ ‘A’, ‘B’, ‘C’ };
for (auto i : v6) std::cout &lt;&lt; i &lt;&lt; ‘ ‘;
std::cout &lt;&lt; &amp;quot;: (case-insensitive) &amp;quot;
&lt;&lt; std::includes(v1.begin(), v1.end(), v6.begin(), v6.end(), cmp_nocase)
&lt;&lt; ‘n’;
}

User cannot opt for use of a custom comparator in the &gt;= and &lt;= overloaded operators at the moment, as shown in the case-insensitive example. In this situation, includes has to be called directly.

// Returns true if left is superset of right?
template&lt;typename T&gt;
bool operator&gt;=(wrapper&lt;T&gt;&amp; left, wrapper&lt;T&gt;&amp; right)
{
T&amp; c1 = left.cont;
T&amp; c2 = right.cont;

if (!std::is_sorted(c1.begin(), c1.end()))
std::sort(c1.begin(), c1.end());
if (!std::is_sorted(c2.begin(), c2.end()))
std::sort(c2.begin(), c2.end());

return std::includes(
c1.begin(), c1.end(),
c2.begin(), c2.end());
}

// Returns true if left is subset of right?
template&lt;typename T&gt;
bool operator&lt;=(wrapper&lt;T&gt;&amp; left, wrapper&lt;T&gt;&amp; right)
{
T&amp; c1 = left.cont;
T&amp; c2 = right.cont;

if (!std::is_sorted(c1.begin(), c1.end()))
std::sort(c1.begin(), c1.end());
if (!std::is_sorted(c2.begin(), c2.end()))
std::sort(c2.begin(), c2.end());

return std::includes(
c2.begin(), c2.end(),
c1.begin(), c1.end());
}

## &amp;quot;I have no use for all these!&amp;quot;

Before you are quick to exclaim that you have no use for these set algorithms, I like to show to you a typical selection example where you can use this. Imagine you are writing a subject enrollment website for college students. On the form, there are currently selected subjects which the student added, and the available subject dropdown which student can pick. It makes sense to remove subject from available dropdown after addition because you do not want the student to accidentally add the same subject twice. One way to compute leftover subjects available for selection, is to just subtract the selected set from the complete set of subjects with minus operator introduced in this article.

Article source code is hosted at [url href="https://github.com/shaovoon/cpp_set_operators" text="Github"]