STL学习笔记(已序区间算法)
针对已序区间执行的算法,执行前提是源区间必须在某个排序准则下已序。
搜寻元素(Searching)
1.检查某个元素是否存在
bool
binary_search(ForwardIterator beg,ForwardIterator end,
const T& value)
bool
binary_search(ForwardIterator beg,ForwardIterator end,
const T& value,
BinaryPredicate op)
以下示范binary_search()的用法
#include "algostuff.hpp"
using namespace std; int main()
{
list<int> coll;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll);
if(binary_search(coll.begin(),coll.end(),))
cout<<"5 is present"<<endl;
else
cout<<"5 is not present"<<endl;
if(binary_search(coll.begin(),coll.end(),))
cout<<"42 is present"<<endl;
else
cout<<"42 is not present"<<endl;
}
2.检查若干值是否存在
bool
includes(InputIterator beg,
InputIterator end,
InputIterator searchBeg,
InputIterator searchEnd)
bool
includes(InputIterator beg,
InputIterator end,
InputIterator searchBeg,
InputIterator searchEnd,
BinaryPredicate op)
两种形式都用来判断已序区间[beg,end)是否包含另一已序区间[searchBeg,searchEnd)的全部元素
以下程序示范inlcudes()的用法
#include "algostuff.hpp"
using namespace std; int main()
{
list<int> coll;
vector<int> search;
INSERT_ELEMENTS(coll,,);
PRINT_ELEMENTS(coll,"coll: ");
search.push_back();
search.push_back();
search.push_back();
PRINT_ELEMENTS(search,"search: ");
if(includes(coll.begin(),coll.end(),search.begin(),search.end()))
cout<<"all elements of search are also in coll"<<endl;
else
cout<<"not all elements of search are also in coll"<<endl;
}
3.搜寻第一个或最后一个可能位置
ForwardIterator
lower_bound(ForwardIterator beg,ForwardIterator end,const T& value)
ForwardIterator
lower_bound(ForwardIterator beg,ForwardIterator end,const T& value,
BinaryPredicate op)
ForwardIterator
upper_bound(ForwardIterator beg,ForwardIterator end,const T& value)
ForwardIterator
upper_bound(ForwardIterator beg,ForwardIterator end,const T& value,
BinaryPredicate op)
1.lower_bound()返回第一个“大于等于value”的元素位置。
2.upper_bound()返回第一个“大于value”元素的位置
以下程序示范lower_bound()和upper_bound()的用法
#include "algostuff.hpp"
using namespace std; int main()
{
list<int> coll;
INSERT_ELEMENTS(coll,,);
INSERT_ELEMENTS(coll,,);
coll.sort();
PRINT_ELEMENTS(coll);
list<int>::iterator pos1,pos2;
pos1=lower_bound(coll.begin(),coll.end(),);
pos2=upper_bound(coll.begin(),coll.end(),);
cout<<"5 could get position "
<<distance(coll.begin(),pos1)+
<<" up to "
<<distance(coll.begin(),pos2)+
<<" without breaking the sorting"<<endl;
coll.insert(lower_bound(coll.begin(),coll.end(),),);
coll.insert(upper_bound(coll.begin(),coll.end(),),);
PRINT_ELEMENTS(coll);
}
3.搜寻第一个和最后一个可能位置
pair<ForwardIterator,ForwardIterator>
equal_range(ForwardIterator beg,ForwardIterator end,const T& value)
pair<ForwardIterator,ForwardIterator>
equal_range(ForwardIterator beg,ForwardIterator end,const T& value,
BinaryPredicate op)
返回值与下式等效: make_pair(lower_bound(...),upper_bound(...))
以下程序展示equal_range()的用法
#include "algostuff.hpp"
using namespace std; bool bothEvenOrOdd(int elem1,int elem2)
{
return elem1%==elem2%;
} int main()
{
vector<int> coll1;
list<int> coll2;
INSERT_ELEMENTS(coll1,,);
INSERT_ELEMENTS(coll2,,);
PRINT_ELEMENTS(coll1,"coll1: ");
PRINT_ELEMENTS(coll2,"coll2: ");
if(equal(coll1.begin(),coll1.end(),coll2.begin()))
cout<<"coll1==coll2"<<endl;
else
cout<<"coll1!=coll2"<<endl;
if(equal(coll1.begin(),coll1.end(),coll2.begin(),bothEvenOrOdd))
cout<<"even and odd elements correspond"<<endl;
else
cout<<"even and odd elements do not correspond"<<endl;
}
合并元素(Merging)
1.两个已序集合的总和
OutputIterator
merge(InputIterator source1Beg,InputIterator source1End,
InputIterator source2Beg,InputIterator source2End,
OutputIterator destBeg)
OutputIterator
merge(InputIterator source1Beg,InputIterator source1End,
InputIterator source2Beg,InputIterator source2End,
OutputIterator destBeg,BinaryPredicate op)
1.两者都是将两个源区间的元素合并,使得“以destBeg起始的目标区间”内含两个源区间所有元素。
2.目标区间内的所有元素都将按顺序排序
下面这个例子展示merge()的用法
#include <iterator>
#include "algostuff.hpp"
using namespace std; int main()
{
list<int> coll1;
set<int> coll2;
INSERT_ELEMENTS(coll1,,);
INSERT_ELEMENTS(coll2,,);
PRINT_ELEMENTS(coll1,"coll1: ");
PRINT_ELEMENTS(coll2,"coll2: ");
cout<<"merged: ";
merge(coll1.begin(),coll1.end(),coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));
cout<<endl;
}
2.两个已序集合的并集
OutputIterator
set_union(InputIterator source1Beg,InputIterator source1End,
InputIterator source2Beg,InputIterator source2End,
OutputIterator destBeg)
OutputIterator
set_union(InputIterator source1Beg,InputIterator source1End,
InputIterator source2Beg,InputIterator source2End,
OutputIterator destBeg,BinaryPredicate op)
与merge不一样的是,目标区间的元素要不来自第一源区间,要不就来自第二源区间,或是同时来自两个源区间。例如:
source1:1 2 2 4 6 7 7 9
source2:2 2 2 3 6 6 8 9
dest:1 2 2 2 3 4 6 6 7 7 8 9
3.两个已序集合的交集
OutputIterator
set_intersection(InputIterator source1Beg,InputIterator source1End,
InputIterator source2Beg,InputIterator source2End,
OutputIterator destBeg)
OutputIterator
set_intersection(InputIterator source1Beg,InputIterator source1End,
InputIterator source2Beg,InputIterator source2End,
OutputIterator destBeg,BinaryPredicate op)
4.两个已序集合的差集
OutputIterator
set_difference(InputIterator source1Beg,InputIterator source1End,
InputIterator source2Beg,InputIterator source2End,
OutputIterator destBeg)
OutputIterator
set_difference(InputIterator source1Beg,InputIterator source1End,
InputIterator source2Beg,InputIterator source2End,
OutputIterator destBeg,BinaryPredicate op)
目标区间的元素只存在于第一源区间,不存在与第二源区间。
STL学习笔记(已序区间算法)的更多相关文章
- Effective STL 学习笔记 31:排序算法
Effective STL 学习笔记 31:排序算法 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- STL学习笔记7 ---- algorithm(算法)
STL中算可以分为三种, 1.变序型队列算法,可以改变容器内的数据: 2.非变序型队列算法,处理容器内的数据而不改变他们 : 3.通用数值算法,这涉及到很多专业领域的算术操作,这里不做介绍. 第一是变 ...
- STL学习笔记(非变动性算法)
辅助函数 本节跟以后几节将对所有STL算法逐一详细讨论.为了简化这些例子,我们使用了一些辅助函数,分别用于对容器进行输出跟插入操作. #ifndef ALGOSTUFF_HPP #define ALG ...
- STL学习笔记--算法
关于STL算法需要注意的是: (1) 所有STL算法被设计用来处理一个或多个迭代器区间.第一个区间通常以起点和终点表示,至于其他区间,多数情况下只需提供起点即可,其终点可自动以第一区间的元素数推导出来 ...
- STL学习笔记(算法概述)
算法头文件 要运用C++标准程序库的算法,首先必须包含头文件<algorithm> 使用STL算法时,经常需要用到仿函数以及函数配接器.它们定义域<functional>头文件 ...
- C++ STL 已序区间查找算法
#include <iostream>#include <algorithm>#include <list>#include <functional># ...
- Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据
Effective STL 学习笔记 Item 34: 了解哪些算法希望输入有序数据 */--> div.org-src-container { font-size: 85%; font-fam ...
- Effective STL 学习笔记 Item 30: 保证目标区间足够大
Effective STL 学习笔记 Item 30: 保证目标区间足够大 */--> div.org-src-container { font-size: 85%; font-family: ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
随机推荐
- 神奇的幻方(NOIP2015)(真·纯模拟)
原题传送门 这是道SB模拟题,NOIP--难度 直接贴代码 #include<iostream> #include<cstdio> using namespace std; , ...
- tcpreplay 流量拆分算法研究
1.1 算法目的 现在网络架构一般是Client-Server架构,所以网络流量一般是分 C-S 和 S-C 两个方向.tcpdump等抓包工具获取的pcap包,两个流向的数据没有被区分.流量方向的 ...
- Git-添加或删除文件
添加文件 $ git add blash $ git commit "add blash file" $ git push -u origin master 删除文件 $ gi ...
- 通过hover修改其他元素
hover,我们都知道,是监听组件“悬停状态”的一个伪类. 我们一般通过hover来修改组件的背景什么的,很少涉及到太复杂的操作.也就是说我们一般只是对加了hover伪类的元素自身的样式进行改变,比如 ...
- dict和set背后的实现原理
# 先说结论 ''' dict的性能远大于list 在list中,随着数据的增大,时间也会增大 在dict中,随着数据的增大,时间没有变化 ''' # 目的:我们研究为什么dict的性能远大于list ...
- 使用log4j2分离系统日志与业务日志
前一篇文章介绍了log4j2 扩展日志级别,支持将系统日志与业务处理日志拆分,现在介绍一下通过日志级别将系统日志与业务日志分类,要达到这个目的很容易,只需要配置一下log4j的xml文件: <? ...
- appium+python自动化24-滑动方法封装(swipe)【转载】
swipe介绍 1.查看源码语法,起点和终点四个坐标参数,duration是滑动屏幕持续的时间,时间越短速度越快.默认为None可不填,一般设置500-1000毫秒比较合适. swipe(self, ...
- 在vc6里头文件sys/timeb.h里struct timeb各变量的具体含义?
timeb的定义:struct _timeb{ time_t time; unsigned short millitm; short timezon ...
- (1)oracle安装、卸载、启动、关闭、登陆以及同时遇到的问题
数据库概念 在oracle里数据库是一个静态的概念,数据库的资料保存在硬盘上,一个数据库可以有多个实例 数据库实例 数据库实例是一个动态的概念,它是进程+这个进程的内存块.就把它当成个指针吧,这个指针 ...
- unittest (python标准库-开发工具-单元测试框架)
unittest官方文档摘录 翻译 reffer to: https://docs.python.org/3/library/unittest.html#unittest.TextTestRunner ...