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 ...
随机推荐
- HDFS租约机制
https://www.cnblogs.com/cssdongl/p/6699919.html
- python常见模块之time模块
一.模块简介 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type(tim ...
- git学习------>如何汉化GitLab?
在上一篇博客中,已经正常安装好了GitLab,然而全部界面都是纯英文的,为了照顾整个团队的英文水平,因此这篇博客的目的是将纯英文的GitLab进行汉化. 纯英文界面 第一步: 确认GitLab版本号 ...
- 003-maven简介
1.1简介 Maven,只是的积累,专家或内行 Maven是优秀的构建工具,依赖管理工具,项目信息管理工具,跨平台.提供了中央仓库,自动下载构件. 1.通过坐标系统定位每一个构件(artifact), ...
- java script 的工具
1.Jsbeautifier 这个微型的美化器可以重新调整 bookmarklet 和丑陋的JavaScript的格式和缩进,也可以对使用流行的 Dean Edward 的 Packer 打包的脚本进 ...
- Python学习笔记(一)数据类型
一.整型和浮点型 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样 age=10 num=-35 score=98.8 二.布尔类型 布尔值和布尔代数的表示 ...
- Angular 笔记系列(一)项目组织与命名规范
其实使用 Angular.js 做项目已经很久了,也遇到过许多问题.其中很多问题的出现都是因为没有按照规范或者最佳实践来做,大部分原因是学的不够细,很多 tips 没 get 到,用到项目中就会出现各 ...
- opencart 单入口文件简单分析
opencart 单入口文件简单分析 opencart是基于mvcl的商城系统,据说是一个外国有人单独开发.比较牛叉.但是又不大符合国人习惯,目前国内opencart社区也是不少. 简单分析了下单 ...
- wget 用法
wget -r -p -np -k http://xxx.com/xxx
- Debian中安装使用sudo命令
Debian中安装使用sudo命令 sudo可以让非root用户具有管理员的权限,安装好的Debian后还不能使用sudo,需要使用root用户登陆后安装sudo命令.#apt-get insta ...