|
set_difference
function template
<algorithm>
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result );
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator set_difference ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp );
Difference of two sorted ranges
Constructs a sorted range beginning in the location pointed by result with the set difference of range [first1,last1) with respect to [first2,last2) as content.
The difference of two sets is formed by the elements that are present in the first set, but not in the second one. Notice that this is a directional operation - for a symmetrical equivalent, see set_symmetric_difference.
The comparison to check for equivalence of values, uses either operator< for the first version, or comp for the second, in order to test this; The value of an element, a, is equivalent to another one, b, when (!a<b && !b<a) or (!comp(a,b) && !comp(b,a)).
For the function to yield the expected result, the elements in the ranges shall be already ordered according to the same strict weak ordering criterion (operator< or comp).
The behavior of this function template is equivalent to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_difference ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result )
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) *result++ = *first1++;
else if (*first2<*first1) first2++;
else { first1++; first2++; }
}
return copy(first1,last1,result);
}
|
Parameters
- first1, last1
- Input iterators to the initial and final positions of the first sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
- first2, last2
- Input iterators to the initial and final positions of the second sequence. The range used is [first2,last2).
- result
- Output iterator to the initial position of the range where the resulting set is to be copied. This shall not overlap with either of the original ranges.
- comp
- Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
Return value
An iterator to the end of the constructed range.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
// set_difference example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
vector<int>::iterator it;
sort (first,first+5); // 5 10 15 20 25
sort (second,second+5); // 10 20 30 40 50
it=set_difference (first, first+5, second, second+5, v.begin());
// 5 15 25 0 0 0 0 0 0 0
cout << "difference has " << int(it - v.begin()) << " elements.\n";
return 0;
}
|
Output:
difference has 3 elements
|
Complexity
At most, performs 2*(count1+count2)-1 comparisons or applications of comp (where countX is the distance between firstX and lastX).
See also
set_union | Union of two sorted ranges (function template) |
merge | Merge sorted ranges (function template) |
|