[C/C++标准库]_[0基础]_[交集和补集]
场景:
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基础]_[交集和补集]的更多相关文章
- [Zlib]_[0基础]_[使用zlib库压缩文件]
场景: 1. WIndows上没找到系统提供的win32 api来生成zip压缩文件, 有知道的大牛麻烦留个言. 2. zlib比較经常使用,编译也方便,使用它来做压缩吧. MacOSX平台默认支持z ...
- [libcurl]_[0基础]_[使用libcurl下载大文件]
场景: 1. 在Windows编程时, 下载http页面(html,xml)能够使用winhttp库,可是并非非常下载文件,由于会失败. 由此引出了WinINet库,无奈这个库的稳定性比較低,使用样例 ...
- [zlib]_[0基础]_[使用Zlib完整解压zip内容]
场景: 1. 解压文件一般用在下载了一个zip文件之后解压,或者分析某个文件须要解压的操作上. 2. 解压文件,特别是解压带目录的zip文件往往系统没有提供这类Win32 API,当然C#自带库能解压 ...
- [C/C++标准库]_[0基础]_[使用fstream合并文本文件]
场景: 1. 就是合并文本文件,而且从第2个文件起不要合并第一行. 2. 多加了一个功能,就是支持2个以上的文件合并. 3. 问题: http://ask.csdn.net/questions/192 ...
- [C/C++标准库]_[0基础]_[优先队列priority_queue的使用]
std::priority_queue 场景: 1. 对于一个任务队列,任务的优先级由任务的priority属性指明,这时候就须要优先级越高的先运行.而queue并没有排序功能,这时priority_ ...
- [C/C++标准库]_[0基础]_[怎样实现std::string自己的Format(sprintf)函数]
场景: 1. C语言有自己的sprintf函数,可是这个函数有个缺点,就是不知道须要创建多大的buffer, 这时候能够使用snprintf函数来计算大小,仅仅要參数 buffer为NULL, co ...
- [Windows]_[0基础]_[Release程序的崩溃报告minidump解决方式]
场景: 1. Release的程序崩溃时,崩溃报告能够让开发者查明代码哪里出了问题,用处大大的. 2. 仅仅实用VS的编译器才支持,所以MinGW就无缘了. 3. 使用了未处理异常过滤处理函数. 4. ...
- [ATL/WTL]_[0基础]_[CBitmap复制图片-截取图片-平铺图片]
场景: 1.当你须要截取图片部分区域作为某个控件的背景. 2.须要平铺图片到一个大区域让他自己主动放大时. 3.或者须要合并图片时. 代码: CDC sdc; CDC ddc; sdc.CreateC ...
- [网络]_[0基础]_[使用putty备份远程数据]
场景: 1. putty是windows上訪问linux服务的免费client之中的一个.用它来ssh到远程server备份数据是常见的做法(在没做好自己主动备份机制前), 通过putty界面尽管也不 ...
随机推荐
- OD: Vulnerability Detection
终于看完第二篇漏洞利用原理高级篇,内容虽然看懂了,但深入理解不够,这部分内容以后还要不断强化. 第三篇是漏洞挖掘技术,篇首的话中,提到程序的不可计算性(图灵机的停机问题).希伯尔数学纲领的失败,结尾说 ...
- CSS3 背景
CSS3包含多个新的背景属性,他们提供了对背景更强大的控制. 本章将学到一下背景属性: background-size background-origin 你也将学到如何使用多重背景图片. 浏览器支持 ...
- 最全的ORACLE-SQL笔记
-- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...
- eclipse在当前项目里面批量替换所有单词
ctrl+f里面只能单个文件用,要整个项目批量替换. 1. 先选中你要替换字符串, 2. 再菜单栏中找到Search→Text→Project,这样就会在整个项目中查找单词. 3. 然后在Search ...
- Css透明度
全透明代码:{background:transparent} 半透明代码:{filter:alpha(opacity=80);-moz-opacity:0.8;width:auto !importan ...
- Spark运行问题备忘一(网络搜集)
问题一 ERROR storage.DiskBlockObjectWriter: Uncaught exception -9ca8//shuffle_1_1562_27 java.io.FileNot ...
- DotNET知识点总结四(笔记整合)
1.枚举:本质是类 如果为第一个枚举赋了一个int值,那么后面的枚举项依次递增 可以将枚举强转成他所代表的int值 C#的枚举项都是常量(可以用Reflector查看literal的IL源码) 因为枚 ...
- D3js初探及数据可视化案例设计实战
摘要:本文以本人目前所做项目为基础,从设计的角度探讨数据可视化的设计的方法.过程和结果,起抛砖引玉之效.在技术方案上,我们采用通用web架构和d3js作为主要技术手段:考虑到项目需求,这里所做的可视化 ...
- ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片
解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...
- Android 之 Spinner
1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...