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 ...
随机推荐
- Python 3.5 中的异步HTTP请求写法
Python 3.5 增加了对async def and await的支持,同样的异步代码看起来干净了很多,也更易读. import aiohttp import asyncio async def ...
- python重建二叉树
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None ...
- Json与字符串互相转换
jQuery插件支持的转换方式: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 浏览器 ...
- redis之持久化操作
简介 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(d ...
- LayoutInflater的动态增加控件
在实际开发中LayoutInflater这个类是非常有用的,它的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout下xml布局文件. 而findView ...
- 0605-Zuul构建API Gateway-使用Sidecar支持异构平台的微服务
使用非jvm语言 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_poly ...
- redis实现cache系统实践(六)
1. 介绍 rails中就自带有cache功能,不过它默认是用文件来存储数据的.我们要改为使用redis来存储.而且我们也需要把sessions也存放到redis中.关于rails实现cache功能的 ...
- 创建发布Webservice以及wsimport工具
一. 通过wsimport生成本地代理调用WebService 1.推荐的访问服务方式 WebService已纳入w3c规范,其他的平台都支持该规范 :J2EE\Php\.NET都支持wsimport ...
- 浅谈WLAN干扰与抗干扰技术
一. 无线干扰的分类和来源 无线干扰按照类型可划分为WLAN干扰和非WLAN干扰.WLAN干扰是指干扰源发送的RF信号也符合802.11标准,除此之外都是非WLAN干扰.对WLAN干扰,可进一步按照频 ...
- NGUI基本事件
You can add the following functions to your scripts placed on widgets or in-game objects with a coll ...