Saturday, November 23, 2013

STL set operations

There are some useful algorithms that run on two sets of values
  • set_union
  • set_intersection
  • set_difference
  • set_symmetric_difference
These operations are very useful, but their names are a bit confusing.
Here's a graphical interpretation of how they work, and it is how I remember them:

Remember, that these algorithms work for any sorted container, ie
  • a sorted array
  • a sorted vector
  • a sorted deque
  • a sorted list
  • a set

Therefore, we should consider them as boolean operations on sets.
Consider two sets, A and B:

A: {  1, 2, 3, 4, 5, 6        }
B: {           4, 5, 6, 7, 8  }


vector<int> A = { 1, 2, 3, 4, 5, 6 };
vector<int> B = { 4, 5, 6, 7, 8 };
vector<int> result;


set_union( A, B )   or A | B

      A: {  1, 2, 3, 4, 5, 6        }
      B: {           4, 5, 6, 7, 8  }
result = {  1, 2, 3, 4, 5, 6, 7, 8  }

std::set_union( A.begin(), A.end(),
                B.begin(), B.end(), 
                std::back_inserter(result) );


set_intersection( A, B )  or A.B

      A: {  1, 2, 3, 4, 5, 6        }
      B: {           4, 5, 6, 7, 8  }
result = {           4, 5, 6        }

std::set_intersection( A.begin(), A.end(),
                       B.begin(), B.end(), 
                      std::back_inserter(result) );


set_difference( A, B )  or A - B

      A: {  1, 2, 3, 4, 5, 6        }
      B: {           4, 5, 6, 7, 8  }
result = {  1, 2, 3                 }

std::set_difference( A.begin(), A.end(),
                     B.begin(), B.end(), 
                    std::back_inserter(result) );



set_symmetric_difference( A, B )  or  (A-B) | (B-A)

      A: {  1, 2, 3, 4, 5, 6        }
      B: {           4, 5, 6, 7, 8  }
result = {  1, 2, 3,          7, 8  }

std::set_symmetric_difference( A.begin(), A.end(),
                               B.begin(), B.end(), 
                               std::back_inserter(result) );




Notes: the sorting criterion used is operator< or a function object which you can provide.

1 comment:

  1. In many cases, it is useful.
    Especially, when we use an already sorted containers ( like as: map or set).
    But, keep in mind "An unordered_map is, as the name implies, inherently not sorted or sortable in-place."!!!

    ReplyDelete