最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
c++ STL set_difference set_intersection set_union 操作
时间:2017-06-26 编辑:简简单单 来源:一聚教程网
以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的。
set_difference 这个是求得在第一个容器中有,第二个容器中没有的。set_intersection 求两个容器的交, set_union 求两个容器的并。
set_symmetric_difference 求两个容器的差。
最后使用的时候注意要提前分配好最后的盛放容器,其大小最好是两个操作容器的和,然后需要根据返回的迭代器resize一下,看下面的例子。
代码如下 | 复制代码 |
// set_symmetric_difference example #include #include #include
intmain () { intfirst[] = {5,10,15,20,25}; intsecond[] = {50,40,30,20,10}; std::vector std::vector
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'
return0; } |
相关文章
- C++构造析构赋值运算函数应用介绍 09-26
- C语言制作通讯录 09-23
- 学生成绩管理系统C++实现代码示例 12-29
- C++标准C函数在各平台编译结果都相同 12-23
- 查找算法之二分查找的C++实现 12-14
- 易语言取时间的方法 12-14