Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

How to set difference in std : : set _ difference?


Asked by Itzel Spence on Dec 11, 2021 FAQ



std:: set_difference 1 [ edit] Parameters. See execution policy for details. ... 2 [ edit] Complexity. At most 2· (N1+N2)-1 comparisons, where N1 = std::distance (first1, last1) and N2 = std::distance (first2, last2) . 3 [ edit] Exceptions. ...
Indeed,
C++ Algorithm set_difference () C++ Algorithm set_difference () function is used to find the difference of two sorted ranges [first1, last1) and [first2, last2), which is formed by the elements that are present in the first range, but not in the second one.
In addition, Std::set can contain element of any specified type in template argument i.e. std::set<int> // contains only int elements. std::set<Sample> // contains only Sample class objects.
Thereof,
By default std::set uses the operator < . It internally maintains a binary balanced tree and during insertion it compares the new element with already present nodes and finds the right position of new element in tree. If that element is already present then it doesn’t insert the new element.
In respect to this,
Yes, there is a set_difference function in the algorithms header. FYI, the set data structure is able to efficiently use that algorithm, as stated in its documentation. The algorithm also works not just on sets but on any pair of iterators over sorted collections. As others have mentioned, this is an external algorithm, not a method.