场景:

1. 计算std::vector A和 std::vector B里的同样的元素, 用于保留不删除.

2. 计算std::vector A和 std::vector B里各自的补集, 用于删除A的补集和加入B的补集,用在一些更新关联表的操作里. 比方联系人A所属分组B是一个集合BV, 把联系人A的所属分组

改动为集合CV, 就须要删除两个集合BV,CV的CV补集和新增BV补集.

3. C++标准库为我们提供了这些算法.

代码:

// test_AndroidAssistant.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <algorithm>
#include <vector>
#include <string>
#include <iostream> #include "gtest/gtest.h" TEST(test_AndroidAssistant,SetIntersection)
{
std::vector<int> v1;
v1.push_back(3);
v1.push_back(121);
v1.push_back(5); std::vector<int> v2;
v2.push_back(2);
v2.push_back(89);
v2.push_back(3);
v2.push_back(5); std::sort(v1.begin(),v1.end());
std::sort(v2.begin(),v2.end()); std::vector<int> result;
std::set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),std::back_inserter(result)); std::cout << "set_intersection" << std::endl;
for(int i = 0; i< result.size(); ++i)
{
std::cout << result[i] << std::endl;
} std::vector<int> d1;
std::vector<int> d2;
std::cout << "set_different" << std::endl;
//Copies the elements from the sorted range [first1, last1)
// which are not found in the sorted range [first2, last2) to the range beginning at d_first
std::set_difference(v1.begin(),v1.end(),result.begin(),result.end(),std::back_inserter(d1));
std::set_difference(v2.begin(),v2.end(),result.begin(),result.end(),std::back_inserter(d2)); std::cout << "set_difference d1" << std::endl;
for(int i = 0; i< d1.size(); ++i)
{
std::cout << d1[i] << std::endl;
} std::cout << "set_difference d2" << std::endl;
for(int i = 0; i< d2.size(); ++i)
{
std::cout << d2[i] << std::endl;
}
} int _tmain(int argc, _TCHAR* argv[])
{
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
return 0;
}

输出:

set_intersection
3
5
set_different
set_difference d1
121
set_difference d2
2
89

注意: 这里 std::back_inserter 是创建输出枚举类型.

看下set_intersection 的实现非常巧妙:

template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_intersection(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first)
{
while (first1 != last1 && first2 != last2) {
if (*first1 < *first2) {
++first1;
} else {
if (!(*first2 < *first1)) {
*d_first++ = *first1++;
}
++first2;
}
}
return d_first;
}

再看下set_difference的实现非常巧妙:

template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_difference(InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2,
OutputIt d_first)
{
while (first1 != last1) {
if (first2 == last2) return std::copy(first1, last1, d_first); if (*first1 < *first2) {
*d_first++ = *first1++;
} else {
if (! (*first2 < *first1)) {
++first1;
}
++first2;
}
}
return d_first;
}

[C/C++标准库]_[0基础]_[交集和补集]的更多相关文章

  1. [Zlib]_[0基础]_[使用zlib库压缩文件]

    场景: 1. WIndows上没找到系统提供的win32 api来生成zip压缩文件, 有知道的大牛麻烦留个言. 2. zlib比較经常使用,编译也方便,使用它来做压缩吧. MacOSX平台默认支持z ...

  2. [libcurl]_[0基础]_[使用libcurl下载大文件]

    场景: 1. 在Windows编程时, 下载http页面(html,xml)能够使用winhttp库,可是并非非常下载文件,由于会失败. 由此引出了WinINet库,无奈这个库的稳定性比較低,使用样例 ...

  3. [zlib]_[0基础]_[使用Zlib完整解压zip内容]

    场景: 1. 解压文件一般用在下载了一个zip文件之后解压,或者分析某个文件须要解压的操作上. 2. 解压文件,特别是解压带目录的zip文件往往系统没有提供这类Win32 API,当然C#自带库能解压 ...

  4. [C/C++标准库]_[0基础]_[使用fstream合并文本文件]

    场景: 1. 就是合并文本文件,而且从第2个文件起不要合并第一行. 2. 多加了一个功能,就是支持2个以上的文件合并. 3. 问题: http://ask.csdn.net/questions/192 ...

  5. [C/C++标准库]_[0基础]_[优先队列priority_queue的使用]

    std::priority_queue 场景: 1. 对于一个任务队列,任务的优先级由任务的priority属性指明,这时候就须要优先级越高的先运行.而queue并没有排序功能,这时priority_ ...

  6. [C/C++标准库]_[0基础]_[怎样实现std::string自己的Format(sprintf)函数]

    场景: 1.  C语言有自己的sprintf函数,可是这个函数有个缺点,就是不知道须要创建多大的buffer, 这时候能够使用snprintf函数来计算大小,仅仅要參数 buffer为NULL, co ...

  7. [Windows]_[0基础]_[Release程序的崩溃报告minidump解决方式]

    场景: 1. Release的程序崩溃时,崩溃报告能够让开发者查明代码哪里出了问题,用处大大的. 2. 仅仅实用VS的编译器才支持,所以MinGW就无缘了. 3. 使用了未处理异常过滤处理函数. 4. ...

  8. [ATL/WTL]_[0基础]_[CBitmap复制图片-截取图片-平铺图片]

    场景: 1.当你须要截取图片部分区域作为某个控件的背景. 2.须要平铺图片到一个大区域让他自己主动放大时. 3.或者须要合并图片时. 代码: CDC sdc; CDC ddc; sdc.CreateC ...

  9. [网络]_[0基础]_[使用putty备份远程数据]

    场景: 1. putty是windows上訪问linux服务的免费client之中的一个.用它来ssh到远程server备份数据是常见的做法(在没做好自己主动备份机制前), 通过putty界面尽管也不 ...

随机推荐

  1. c#将Excel数据导入到数据库的实现代码(转载)

    假如Excel中的数据如下:     数据库建表如下:     其中Id为自增字段:      代码如下: using System; using System.Collections.Generic ...

  2. mysql的limit性能,数据库索引问题,dblog问题

    mysql的limit性能,数据库索引问题,dblog问题,redis学习 继续学习. dblog实际上是把日志记录在另一个数据库里面. 问题1: 一张表定义了5个索引,但是sql语句中用到了3个有索 ...

  3. 打jar包的方法

    打jar包的方法是什么? java打jar包,引用其他.jar文件 java项目打jar包 将java源码打成jar包 maven打jar例子 打war包的方法是什么? Eclipse->项目右 ...

  4. Examples_08_08

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARYAAAGsCAIAAACXfh8LAAAgAElEQVR4nO2db3AT193v903e8yIzbV

  5. 执行CMD命令

    可以执行多条命令,用“\r\n”分割 using System; using System.Diagnostics; namespace Tool { public class CMDHelper { ...

  6. .net 实现 URL重写,伪静态

    一,获得Mircosoft URLRewriter.dll: 获得Mircosoft URLRewriter.dll可以到http://www.microsoft.com/china/msdn/lib ...

  7. (转,感谢原作者!)既然选择了Linux,有何必在乎这些——Linux wine国服LOL英雄联盟,完美运行!!

    Linux下玩国服LOL,国服哦.网络上随处都可以搜到wine美服LOL的教程,但腾讯运营的国服客户端跟美服原版相差比较大,按照美服的方式不能搞起国服LOL,由于宿舍文化,这几天我专注于wine一个国 ...

  8. Knockoutjs官网翻译系列(三) 使用Computed Observables

    书接上回,前面谈到了在视图模型中可以定义普通的observable属性以及observableArray属性实现与UI元素的双向绑定,这一节我们继续探讨第三种可实现绑定的属性类型:computed o ...

  9. oracle 使用 ALTER 操作列

    使用 ALTER TABLE 语句追加, 修改, 或删除列的语法

  10. 武汉科技大学ACM:1008: 明明的随机数

    Problem Description 明明想在学校中请一些同学一起做一项问卷 调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个, ...