获得两个集合的并集。两个输入序列须保证已排好序。

数组用的时候

// set_union 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_union (first, first+5, second, second+5, v.begin());
// 5 10 15 20 25 30 40 50 0 0
cout << "union has " << int(it - v.begin()) << " elements.\n";
return 0;
}

set用的时候

// set_union2 example
#include <set>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std; int main(void)
{
set<int> a,b,c;
a.insert(1);
a.insert(6);
a.insert(6);
b.insert(2);
b.insert(6);
b.insert(9); //最后一个参数若使用c.begin()会产生编译错误assignment of read-only localtion. set_union(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.begin()));
copy(c.begin(), c.end(), ostream_iterator <int> (cout, " "));
return 0;
}

vector用的时候

// set_union3 example
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std; int main()
{
vector<int> a,b,c;
for(int e=0;e<10;e++)
{
a.push_back(e);
b.push_back(e+5);
}
//最后一个参数若使用c.begin(),运行时会出错“Segmentation fault (core dumped)”.
set_union(a.begin(),a.end(),b.begin(),b.end(),back_inserter(c));
copy(c.begin(), c.end(), ostream_iterator<int> (cout, " "));
return 0;
}

set_union的几个例子的更多相关文章

  1. STL set_difference set_intersection set_union 操作

    以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的. set_difference 这个是求得在第一个容器中有,第 ...

  2. 关于C++里set_intersection(取集合交集)、set_union(取集合并集)、set_difference(取集合差集)等函数的使用总结

    文章转载自https://blog.csdn.net/zangker/article/details/22984803 set里面有set_intersection(取集合交集).set_union( ...

  3. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  4. C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)

    第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...

  5. REGEX例子

    作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...

  6. CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子

    CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子 本文涉及的VolumeRendering相关的C#代码是从(https://github.com/toolchai ...

  7. 简单例子了解View的事件分发

    什么是事件分发 我们在写自定义ViewGroup或者自定义View的时候经常要处理用户的点击事件,如果我们的View在最底层,他在很多ViewGroup里面,我们如何让我们的点击事件准确传递到View ...

  8. 简单的例子了解自定义ViewGroup(一)

    在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...

  9. kqueue例子

    网络服务器通常都使用epoll进行异步IO处理,而开发者通常使用mac,为了方便开发,我把自己的handy库移植到了mac平台上.移植过程中,网上居然没有搜到kqueue的使用例子,让我惊讶不已.为了 ...

随机推荐

  1. Apache配置站点根目录、用户目录及页面访问属性

    一.配置站点根目录及页面访问属性 DocumentRoot "/www/htdoc" <Directory "/www/htdoc"> Option ...

  2. HDU 2095 find your present (2)

    HDU 2095 find your present (2) 解法一:使用set 利用set,由于只有一个为奇数次,对一个数进行查询,不在集合中则插入,在集合中则删除,最后剩下的就是结果 /* HDU ...

  3. URAL 1056 Computer Net(最短路)

    Computer Net Time limit: 2.0 secondMemory limit: 64 MB Background Computer net is created by consecu ...

  4. Logistic回归的牛顿法及DFP、BFGS拟牛顿法求解

    牛顿法 # coding:utf-8 import matplotlib.pyplot as plt import numpy as np def dataN(length):#生成数据 x = np ...

  5. C#的委托 Action<>和Func<>

    其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: [csharp]  public delegate vo ...

  6. Java 实现学生信息管理系统

    编写一个简单的学生管理信息系统. 在oracle中设计一张学生表,以学号作为关键字. 其他学生信息有:姓名.手机号. 在进入系统时,显示如下菜单: ************************** ...

  7. httpd-2.2

    http://httpd.apache.org/docs/2.2/logs.html httpd.conf文件 Configuration and logfile names: If the file ...

  8. FASTDFS .net 客户端

    FastDFS .net 客户端目前还比较少,而且使用大部分没有原生提供的方便 推荐一个比较方便的 下载地址: https://www.nuget.org/packages/NengLong.CMP. ...

  9. python3生成标签云

    标签云是现在大数据里面最喜欢使用的一种展现方式,其中在python3下也能实现标签云的效果,贴图如下: -------------------进入正文--------------------- 首先要 ...

  10. Java OCR 图像智能字符识别技术,可识别中文

    http://www.open-open.com/lib/view/open1363156299203.html