c++ STL set_difference set_intersection set_union 操作

栏目: 编程工具 · 发布时间: 7年前

内容简介:c++ STL set_difference set_intersection set_union 操作

以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的。

set_difference 这个是求得在第一个容器中有,第二个容器中没有的。set_intersection 求两个容器的交, set_union 求两个容器的并。

set_symmetric_difference 求两个容器的差。

最后使用的时候注意要提前分配好最后的盛放容器,其大小最好是两个操作容器的和,然后需要根据返回的迭代器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;
 }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

实用Common Lisp编程

实用Common Lisp编程

Peter Seibel / 田春 / 人民邮电出版社 / 2011-10 / 89.00元

由塞贝尔编著的《实用Common Lisp编程》是一本不同寻常的Common Lisp入门书。《实用Common Lisp编程》首先从作者的学习经过及语言历史出发,随后用21个章节讲述了各种基础知识,主要包括:REPL及Common Lisp的各种实现、S-表达式、函数与变量、标准宏与自定义宏、数字与字符以及字符串、集合与向量、列表处理、文件与文件I/O处理、类、FORMAT格式、符号与包,等等。......一起来看看 《实用Common Lisp编程》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具