set_difference()

set_difference()算法计算两个集合[start1, end1)和[start2, end2)的差集, 并将差集存放到result.

两个集合以序列的形式给出, 且必须先按升序排好位置.

set_difference()是一个指向result序列末尾的迭代器.

如果严格弱排序的比较函数对象cmp未指定, set_difference()将使用<操作符比较元素.

范例

// set_difference example
#include 
#include 
#include 
using namespace std;
 
int main () {
  int first[] = {5,10,15,20,25};
  int second[] = {50,40,30,20,10};
  vector v(10);                           // 0  0  0  0  0  0  0  0  0  0
  vector::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;
}


输出: difference has 3 elements

你可能感兴趣的:(STL)