场景:

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. javascript:void(0)知多少

    在做页面时,如果想做一个链接点击后不做任何事情,或者响应点击而完成其他事情,可以设置其属性 href = "#",但是,这样会有一个问题,就是当页面有滚动条时,点击后会返回到页面顶 ...

  2. nosqlunit开源框架

    import com.lordofthejars.nosqlunit.annotation.UsingDataSet;import com.lordofthejars.nosqlunit.core.L ...

  3. rmi rpc restful soa 区别

    rmi rpc restful soa 区别 rmi vs rpc 参考文档:http://stackoverflow.com/questions/2728495/what-is-the-differ ...

  4. 粗俗易懂的SQL存储过程在.NET中的实例运用

    整理了一下存储过程在项目中的运用,防止遗忘,便记录于此!存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中.用户通过指定存储过程的名字并给出参数( ...

  5. A Bug's Life(hdu1829种类并查集)

    题意:有一群虫子,现在给你一些关系,判断这些关心有没有错 思路:向量种类并查集,下面讲一下向量的种类并查集 本题的各个集合的关心有两种0同性,1异性,怎么判断有错, 1.先判断他们是否在一个集合,即父 ...

  6. nuc950支持nand的mtd驱动的kernel修改

    支持nand的mtd驱动的kernel修改 一.更新nanddriver文件 将新的nanddriver文件nuc900_nand.c放到kernel的drivers/mtd/nand目录下 二.修改 ...

  7. Oracle数据库之序列

    Oracle数据库之序列(sequence) 序列是一个计数器,它并不会与特定的表关联.我们可以通过创建Oracle序列和触发器实现表的主键自增.序列的用途一般用来填充主键和计数. 一.创建序列 语法 ...

  8. 使用Office2007向cnblogs.com发布文章

    步骤: 在cnblogs.com创建一个博客 在office2007中新建->博客文章->创建. 配置帐户:

  9. php开发通用采集程序

    php采集程序构建基本步骤: 采集程序是什么?获取远程数据(文字.图片.图片)并快速保存到本地或指定地址. 如天气预报(小偷程序): 远程获取-->替换内容-->展示给用户 如实时更新的新 ...

  10. Python学习之--异常处理

    Python中的Exceptions是所有异常的基类,内置的异常类都放在了exceptions模块中,通过dir()函数可以看到这些内置的类 通过raise 语句触发异常,如 >>> ...