STL的其他用法(adjacent_find, find_first_of, sort_heap, merge, binary_search)总结
2017-08-20 17:26:07
writer:pprp
1、adjacent_find()
下面是源码实现:
template <class ForwardIterator>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last)
{
if (first != last)
{
ForwardIterator next=first; ++next;
while (next != last) {
if (*first == *next) // or: if (pred(*first,*next)), for version (2)
return first;
++first; ++next;
}
}
return last;
}
测试:
/*
name : STL的其他用法
writer : pprp
declare : null
date ; 2017/8/20
*/
#include <bits/stdc++.h> using namespace std;
typedef list<int> LIST; bool Equal(int a, int b)
{
return (a - b)% == ?:;
} void print(LIST&q)
{
LIST::iterator it;
for(it = q.begin() ; it != q.end(); it++)
{
cout << *it << " ";
}
cout << endl;
} int main()
{
//adjacent_find 查找相邻元素
LIST l; for(int i = ; i < ; i++)
{
l.push_back(i);
l.push_back(i+);
l.push_back(i+);
l.push_back(i+);
}
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back();
l.push_back(); print(l); LIST::iterator it = adjacent_find(l.begin(),l.end()); if(it != l.end())
{
cout << "两个相邻元素相等" << *it << " ";
it++;
cout << *it << endl;
} list <int>::iterator ii = adjacent_find(l.begin(), l.end(), Equal);
if(ii != l.end())
{
cout <<"找出首个两个相邻元素相同"<< *ii << " ";
ii++;
cout << *ii << endl;
} return ;
}
2、find_first_of查找第一个匹配字符串(不推荐使用,查看源代码采用最高复杂度的算法)
/*
name : STL的其他用法:find_first_of
writer : pprp
declare : null
date ; 2017/8/20
*/
#include <bits/stdc++.h> using namespace std; int main()
{
char * s1 = "abcdefu7ghijklmn";
char * s2 = "zyx3yu7ys";
char * i = find_first_of(s1,s1 + strlen(s1),s2,s2 + strlen(s2));
//第一个出现在s2中的字符为 *i
cout << * i << endl;
return ;
}
3、堆排序(有点慢)
/*
name : STL中的堆排序
writer : pprp
declare : 复杂度为 nlogn
date ; 2017/8/20
*/
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector> using namespace std; void print(int x)
{
cout << x << " ";
} int main()
{
vector<int> v;
int tmp;
//每次执行的种子不同,生成的随机数才不相同
srand((int)time(NULL));
//产生10个随机数,记录在vector中
for(int i = ; i < ; i++)
{
tmp = rand() % ;
v.push_back(tmp);
} for_each(v.begin(), v.end(),print);
cout << endl; make_heap(v.begin(),v.end());
sort_heap(v.begin(),v.end()); for_each(v.begin(),v.end(),print);
cout << endl; return ;
}
4、归并算法(合并两个有序的序列)
/*
name : 归并排序,合并两个有序序列
writer : pprp
declare : 复杂度为 nlogn
date ; 2017/8/20
*/
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <ctime> using namespace std;
const int maxn = ;
int a[maxn];
int b[maxn]; void print(int *x,int n)
{
for(int i = ; i < n ; i++)
{
cout << x[i] << " ";
}
cout << endl;
} int main()
{
srand((int)time(NULL));
//产生10个随机数,记录在vector中
for(int i = ; i < ; i++)
{
a[i] = rand()%;
b[i] = rand()%;
}
//升序
sort(a,a+);
sort(b,b+); print(a,);
print(b,); int result[maxn*]; merge(a,a+,b,b+,result,less<int>()); print(result,); //降序
sort(a,a+,greater<int>());
sort(b,b+,greater<int>()); merge(a,a+,b,b+,result,greater<int>()); print(result,); return ;
}
5、binary_search折半查找(用在有序区间中)
bool binary_search(a,a+size,key)
6、includes判断集合包含关系
int a[] = {,,,,};
int b[] = {,,,,,,,,,};
if(includes(a,a+,b,b+))
{
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
7、最值
max(,);
min(,);
//查找线性容器中的最大最小
list <int> :: iterator it = min_element(l.begin(), l.end());
list <int> :: iterator it = max_element(l.begin(), l.end());
//字典序比较大小
lexicographical_compare(s1,s1+len1,s2,s2+len2);
8、组合数生成
/*
name : STL中的堆排序
writer : pprp
declare : 复杂度为 nlogn
date ; 2017/8/20
*/
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <ctime> using namespace std; void print(int a[])
{
for(int i = ; i < ; i++)
{
cout << a[i] << " ";
}
cout << endl;
} int main()
{
int a[] = {,,,,};
while(next_permutation(a,a+))
{
print(a);
}
cout << endl; while(prev_permutation(a,a+))
{
print(a);
} return ;
}
STL的其他用法(adjacent_find, find_first_of, sort_heap, merge, binary_search)总结的更多相关文章
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- C++-STL:vector用法总结
目录 简介 用法 1. 头文件 2. vector的声明及初始化 3. vector基本操作 简介 vector,是同一类型的对象的集合,这一集合可看作可变大小的数组,是顺序容器的一种.相比于数组,应 ...
- C++ STL算法系列2---find ,find_first_of , find_if , adjacent_find的使用
一.find运算 假设有一个int型的vector对象,名为vec,我们想知道其中是否包含某个特定值. 解决这个问题最简单的方法时使用标准库提供的find运算: 1 // value we'll lo ...
- 各种STL的基本用法
目录 STL及一些常用函数的基本用法 1.vector(向量)的基本用法 2.queue(队列)的基本用法 3.stack(栈)的基本操作 4.set(集合)的基本用法 5.map(映射)的基本用法 ...
- STL中map用法
Map是 STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于 这个特性,它完成有可能在我们处理一对一数据的 ...
- [STL] SET实用用法
背景 今天考试深受平衡树之害,可以参见上一篇博客,想到了set却苦于实用的不熟练.同时QTY询问set的具体用法,所以写这篇博客,同时留作自用. 分类 参看了一下网上其他set博客,上来都是长篇大论概 ...
- STL:字符串用法详解
字符串是程序设计中最复杂的变成内容之一.STL string类提供了强大的功能,使得许多繁琐的编程内容用简单的语句就可完成.string字符串类减少了C语言编程中三种最常见且最具破坏性的错误:超越数组 ...
- C++中的STL中map用法详解
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...
- C++ STL sort()函数用法
C++STL提供的在里的排序函数,有以下两种形式 此外还提供有稳定排序版本stable_sort(),用法类似. 第一种形式: template <class RandomAccessItera ...
随机推荐
- Storm-源码分析- Messaging (backtype.storm.messaging)
先定义两个接口和一个类 TaskMessage类本身比较好理解, 抽象storm的message格式 对于IContext, 注释也说了, 定义messaging plugin, 通过什么渠道去发送m ...
- Python 之RabbitMQ使用
1. IO 多路复用 # select 模拟socket server # server 端 import select import socket import sys import queue s ...
- d3.js:数据可视化利器之 修改文档:DOM操作符
style: CSS样式操作符 style()操作符用来设置或获取选择集中各DOM元素的CSS样式: selection.style(name[,value[,priority]]) style()操 ...
- Python之配置文件读写
ConfigParser模块 一.创建配置文件 在D盘建立一个配置文件,名字为:test.ini 内容如下: [baseconf] host=127.0.0.1 port=3306 user=root ...
- (0.2.6)Mysql安装——编译安装
(0.2.6)Mysql安装——编译安装 待完善
- PHP SPL使用方法和他的威力
什么是SPL,如何使用,他有什么作用,下面我我们就讲讲PHP SPL的用法 SPL,PHP 标准库(Standard PHP Library) ,此从 PHP 5.0 起内置的组件和接口,并且从 PH ...
- Nginx+tomcat配置负载均衡集群
操作系统版本:Centos 6.4 Nginx版本:nginx-1.3.15.tar.gz wget http://nginx.org/download/nginx-1.5.9.tar.gz JDK版 ...
- PHP 基础篇 - PHP 的 BC MATH 系列数学函数
一.常见问题 用 PHP 做计算时经常会遇到精度带来的问题,下面来看两个常见的例子: 1. 运算比较 下面表达式输出的结果不是相等: <?php echo 2.01 - 0.01 == 2 ? ...
- 注释的HALCON的程序
*关闭窗口 dev_close_window () dev_close_window () *打开指定大小.颜色背景的窗口 dev_open_window (0, 0, 768/2, 576/2, ' ...
- 51Nod 1079
题目大意: 一个正整数K,给出K Mod一些质数的结果,求符合条件的最小的K.例如,K%2=1,K%3=2,K%5=3符合条件的最小的K=23. Input 第1行:1个数N表示后面输入的质数及模的数 ...