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.




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)


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

  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 Revit 2018

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

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

  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

  6. Torrentsformac
    A free download hub to activate, crack your softwares for windows and you mac. you can download keygen, generator and activators to crack your desired

  7. This comment has been removed by a blog administrator.