How to take set difference of two sets in C++?

Set difference of two sets A and B is defined as the set C consisting of elements from A not present in B.

In C++, algorithm header in STL defines a function set_difference which in conjunction with inserter can be used to take set difference of two sorted range of elements.

Example:

#include<algorithm>

#include<set>

using namespace std;

..

set<int> A, B, C;

..

set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(C, C.end()))




So, this basically takes the set difference of the whole of A and B and copies those elements to C.

set_difference can even work on vectors and arrays. Just make sure that the vectors and arrays are sorted before you take the set difference.

Complexity: O(n)



7 comments:

  1. This is very nice one and gives indepth information. Thanks for this nice article.
    the best carding forum

    ReplyDelete
  2. They design engineering software solutions for a broad assortment of applications and devices, from device dependent embedded software to mobile solutions and enterprise desktop applications.cheap Revit 2018

    ReplyDelete
  3. Application segments for the global engineering software market are product design and testing, 3D modeling, drafting, design automation, and others.buy SolidWorks 2018

    ReplyDelete
  4. Other programming forums serve a broader horizon and include threads which discuss multiple programming languages and scripting languages like Perl, python and PHP..net forums

    ReplyDelete
  5. It is really great work and the way in which you are sharing the knowledge is excellent. Thanks for your informative article
    Php course in chennai

    ReplyDelete