欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c++ STL set_difference set_intersection set_union 操作

 更新時(shí)間:2017年03月13日 22:36:02   投稿:mdxy-dxy  
這篇文章主要介紹了c++ STL set_difference set_intersection set_union 操作,需要的朋友可以參考下

以下是STL algorithm的幾個(gè)函數(shù),使用的條件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的。

set_difference 這個(gè)是求得在第一個(gè)容器中有,第二個(gè)容器中沒(méi)有的。set_intersection 求兩個(gè)容器的交, set_union 求兩個(gè)容器的并。

set_symmetric_difference 求兩個(gè)容器的差。

最后使用的時(shí)候注意要提前分配好最后的盛放容器,其大小最好是兩個(gè)操作容器的和,然后需要根據(jù)返回的迭代器resize一下,看下面的例子。

// set_symmetric_difference example
 #include <iostream>   // std::cout
 #include <algorithm>  // std::set_symmetric_difference, std::sort
 #include <vector>    // std::vector
  
 int main () {
  int first[] = {5,10,15,20,25};
  int second[] = {50,40,30,20,10};
  std::vector<int> v(10);           // 0 0 0 0 0 0 0 0 0 0
  std::vector<int>::iterator it;
  
  std::sort (first,first+5);   // 5 10 15 20 25
  std::sort (second,second+5);  // 10 20 30 40 50
  
  it=std::set_symmetric_difference (first, first+5, second, second+5, v.begin());
                         // 5 15 25 30 40 50 0 0 0 0
  v.resize(it-v.begin());           // 5 15 25 30 40 50
  
  std::cout << "The symmetric difference has " << (v.size()) << " elements:\n";
  for (it=v.begin(); it!=v.end(); ++it)
   std::cout << ' ' << *it;
  std::cout << '\n';
  
  return 0;
 }

相關(guān)文章

最新評(píng)論