set_union的几个例子
获得两个集合的并集。两个输入序列须保证已排好序。
数组用的时候
// 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的几个例子的更多相关文章
- STL set_difference set_intersection set_union 操作
以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的. set_difference 这个是求得在第一个容器中有,第 ...
- 关于C++里set_intersection(取集合交集)、set_union(取集合并集)、set_difference(取集合差集)等函数的使用总结
文章转载自https://blog.csdn.net/zangker/article/details/22984803 set里面有set_intersection(取集合交集).set_union( ...
- SQLServer地址搜索性能优化例子
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...
- C#+HtmlAgilityPack+XPath带你采集数据(以采集天气数据为例子)
第一次接触HtmlAgilityPack是在5年前,一些意外,让我从技术部门临时调到销售部门,负责建立一些流程和寻找潜在客户,最后在阿里巴巴找到了很多客户信息,非常全面,刚开始是手动复制到Excel, ...
- REGEX例子
作为REGEX的例子,代码9.3显示了一个给定的文件有多少行,具有给定的模式,通过命令行输入(注:有更有效率的方式来实现这个功能,如Unix下的grep命令,在这里只是给出了另一种方式).这个程序像下 ...
- CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子
CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子 本文涉及的VolumeRendering相关的C#代码是从(https://github.com/toolchai ...
- 简单例子了解View的事件分发
什么是事件分发 我们在写自定义ViewGroup或者自定义View的时候经常要处理用户的点击事件,如果我们的View在最底层,他在很多ViewGroup里面,我们如何让我们的点击事件准确传递到View ...
- 简单的例子了解自定义ViewGroup(一)
在Android中,控件可以分为ViewGroup控件与View控件.自定义View控件,我之前的文章已经说过.这次我们主要说一下自定义ViewGroup控件.ViewGroup是作为父控件可以包含多 ...
- kqueue例子
网络服务器通常都使用epoll进行异步IO处理,而开发者通常使用mac,为了方便开发,我把自己的handy库移植到了mac平台上.移植过程中,网上居然没有搜到kqueue的使用例子,让我惊讶不已.为了 ...
随机推荐
- Vue.js相关知识2-组件
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- click 绑定(二)带参数的click 事件绑定
注1:传参数给你的click 句柄 最简单的办法是传一个function包装的匿名函数: <button data-bind="click: function() { viewMode ...
- 1029c语言文法定义与c程序的推导过程
program → external_declaration | program external_declaration <源程序>→ <外部声明> | <源程序> ...
- Checking the content of the pointer
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/d481b7ec-cd67-476c-b2d7-ca3334effa16/h ...
- gprof使用介绍
gprof 1.1 简介 gprof实际上只是一个用于读取profile结果文件的工具.gprof采用混合方法来收集程序的统计信息,他使用检测方法,在编译过程中在函数入口处插入计数器用于收集 ...
- NoSQL之Redis高级实用命令详解--安全和主从复制
Android IOS JavaScript HTML5 CSS jQuery Python PHP NodeJS Java Spring MySQL MongoDB Redis NOSQL Vim ...
- vb6通send和recv请求网络资源
最近为了弄清楚send和recv的用法,特意用vb6测试了一下,头文件冗余的比较多: Option Explicit Private Declare Function setsockopt Lib & ...
- shell脚本实例-游戏脚本
http://bbs.chinaunix.net/thread-3580033-1-1.html shell游戏收集贴 #!/bin/bash # Tetris Game #APP declarati ...
- 在没有spineRunTime情况下手动使用spine数据
用spine做了一些简单UI动画,结果发现游戏用的cocos2dx版本太低了,无法集成spineRunTime,无奈只好把spine记下的关键帧数据抄到一个txt文件中,然后读取txt,再自己写代码播 ...
- Windows电脑上安装Appium
Windows机器上,除了JAVA的安装和配置外,安装Appium需要如下设置: 0. 安装SDK后设置环境变量 1)新建JAVA_HOME: SDK安装路径,如D:\Program Files\Ja ...