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. Elements are compared using operator < for the first version or using the given binary comparison function comp for the second version. SyntaxParameterfirst1: An input iterator pointing to the first element in the first of two sorted source sequences. last1: An input iterator pointing to the past last element in the first of two sorted source sequences. first2: An input iterator pointing to the first element in the second sorted source sequence. last2: An input iterator pointing to the past last element in the second sorted source sequence. comp: A user-defined binary predicate function that accepts two arguments and returns true if the two arguments are in order and false otherwise. It follows the strict weak ordering to order the elements. result: An output iterator addressing to the position of the first element in the destination range. Return valueThis function returns an iterator to the end of the constructed range. ComplexityComplexity is linear in the distance between [first1, last1) and [first2, last2): performs up to 2*(count1+count2)-1 comparisons. Where count1 = last1- first1 and count2 = last2- first2. Data racesThe object in the range [first1, last1) and [first2. last2) are accessed. The object in the range between result and returned value are modified. ExceptionsThis function throws an exception if any of element comparison, the element assignments or an operation on iterator throws an exception. Note: The invalid parameters cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of set_difference(): Output: 0 6 7 Example 2Let's see another simple example: Output: First array contains : 5 10 15 20 25 Second array contains : 50 40 30 20 10 The difference has 3 elements: 5 15 25 Example 3Let's see another simple example to find the elements that are present in the forst list not in second list: Output: Students in first class : Nikita Aman Deep Nidhi Students in second class : Aman Deep Rakesh Neha Students attending first class only are : Nidhi Nikita Example 4Let's see a simple example: Output: A B
Next TopicC++ Algorithm
|