C++ #include<algorithm>
今天下午大致学完了进阶指南中algorithm头文件下的内容,在这里进行一个总结。
reverse翻转
顾名思义,reverse进行的操作就是翻转原来的顺序,理解非常简单,故不赘述。
操作样例:
#include<bits/stdc++.h>
using namespace std;
vector<int>a;
int b[];
int main()
{
int na,nb;
//vector的实现
scanf("%d",&na);
for(int i=;i<na;i++)
{
int x;
scanf("%d",&x);
a.push_back(x);
}
reverse(a.begin(),a.end());
for(int i=;i<na;i++)
printf("%d ",a[i]);
cout<<endl;
//数组下的实现
scanf("%d",&nb);
for(int i=;i<=nb;i++)
scanf("%d",&b[i]);
reverse(b+,b++nb);
for(int i=;i<=nb;i++)
printf("%d ",b[i]);
return ;
}
unique去重
unique的含义仍然很好理解ovo,我也不说太多了,函数返回值可以是去重后的元素个数,比如:
int m=unique(a.begin(),a.end())-a.begin();
int n=unique(b+1,b+1+len)-b-1;
操作样例:
#include<bits/stdc++.h>
using namespace std;
vector<int>a;
int b[];
int na,nb;
int main( )
{
scanf("%d",&na);
for(int i=;i<na;i++)
{
int x;
scanf("%d",&x);
a.push_back(x);
}
int ma=unique(a.begin(),a.end())-a.begin();
for(int i=;i<ma;i++)
printf("%d ",a[i]);
cout<<endl;
scanf("%d",&nb);
for(int i=;i<=nb;i++)
scanf("%d",&b[i]);
int mb=unique(b+,b++nb)-b-;
for(int i=;i<=mb;i++)
printf("%d ",b[i]);
return ;
}
random_shuffle随机打乱
用法和reverse相同,我都懒得写代码了...
sort快速排序
想必sort的一般用法大家都很熟悉了,不再赘述,但vector<struct>我以前倒没有接触过。
我看网上有很多博客介绍,但似乎都不太清晰,所以自己摸索出了一种比较麻烦占空间但简单易懂的方法,希望dalao指点。
操作实例:
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
};
node b[];
vector<node>a;
bool operator <(const node &a,const node &b)
{
return a.x<b.x;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&b[i].x,&b[i].y);
a.push_back(b[i]);
}
sort(a.begin(),a.end());
for(int i=;i<a.size();i++)
printf("%d %d\n",a[i].x,a[i].y);
return ;
}
permutation全排列
组合数学大家一定多多少少都有所了解,全排列指的就是A(n,n)式的所有排列方法,也就是说五选五。
next_permutation()会取得[first,last)所标示之序列的下一个排列组合;
利用next_permutation的返回值,判断是否全排列结束 如果没有下一个排列组合,便返回false;
否则返true; STL提供了两个用来计算排列组合关系的算法; 分别是next_permutation和prev_permutation;
下一个全排列(next_Permutation) 前一个全排列(prev_permutation)
简单来说
next_permutation
按照字典序由小到大的全排列
prev_permutation
按照字典序由大到小的全排列
二者返回值为true/false 用来判断是否还有下一个排列 全排列的输出正常的for循环即可
注:两者为互逆运算
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
int main()
{
int n1,n2;
cout<<"对next_permutation的操作\n";
cin>>n1;
for(int i=;i<=n1;i++)
cin>>a[i];//输入数据应该是一组数据全排列中字典序不为最大的一类
do
{
for(int i=;i<=n1;i++)
cout<<a[i]<<" ";
cout<<endl;
}while(next_permutation(a+,a++n1));
cout<<endl;
cout<<"对prev_permutation的操作\n";
cin>>n2;
for(int i=;i<=n2;i++)
cin>>b[i];//输入数据应该是一组数据全排列中字典序不为最小的一类
do
{
for(int i=;i<=n2;i++)
cout<<b[i]<<" ";
cout<<endl;
}while(prev_permutation(b+,b++n2));
return ;
}
lower_bound与upper_bound
l_b的作用是在一个区间内寻找第一个大于等于x的元素的位置,u_b是查找
第一个大于x的元素的位置,返回值就是其位置。
当然还有其他操作,比如它有一个很重要的作用就是和unique函数配套使用进行离散化,后续我会在STL的总结中具体解释。
#include<bits/stdc++.h>
using namespace std;
vector<int>a;
int b[];
int na,nb,xa,xb;
int main()
{
scanf("%d%d",&na,&xa);
for(int i=;i<na;i++)
{
int x;
scanf("%d",&x);
a.push_back(x);
}
printf("%d\n",lower_bound(a.begin(),a.end(),xa)-a.begin());
scanf("%d%d",&nb,&xb);
for(int i=;i<=nb;i++)
{
scanf("%d",&b[i]);
}
printf("%d\n",upper_bound(b+,b++nb,xb)-b);
return ;
}
我的总结主要以代码为主,algorithm下的函数都简单易懂,没有用太多的文字说明,都是自己手打测试的操作实例,多测试几组数据,自然就明白了。
如果有不对的地方,希望dalao指正。>w<
C++ #include<algorithm>的更多相关文章
- #include <algorithm>
1 adjacent_find 查找重复的元素 2 find_if 查找符合条件的第一个元素 3 find_if_not 查找不符合条件的第一个元素 4 for_each 可以遍历每一个元素 5 pa ...
- #include <algorithm>中sort的一般用法
1.sort函数的时间复杂度为n*log2(n),执行效率较高. 2.sort函数的形式为sort(first,end,method)//其中第三个参数可选. 3.若为两个参数,则sort的排序默认是 ...
- #include<algorithm>里的函数
#include<algorithm>里的函数 #include<algorithm> 非修改性序列操作(12个) 循环 对序列中的每个元素执行某操作 for_ ...
- C++神奇算法库——#include<algorithm>
算法(Algorithm)为一个计算的具体步骤,常用于计算.数据处理和自动推理.C++ 算法库(Algorithms library)为 C++ 程序提供了大量可以用来对容器及其它序列进行算法操作的函 ...
- [Data Structure & Algorithm] Hash那点事儿
哈希表(Hash Table)是一种特殊的数据结构,它最大的特点就是可以快速实现查找.插入和删除.因为它独有的特点,Hash表经常被用来解决大数据问题,也因此被广大的程序员所青睐.为了能够更加灵活地使 ...
- vector algorithm find
本来想着申请了博客园以后 我要写的博客都必须是有深度有内涵的...好吧 结果我只能说我想多了 还是得一步一步慢慢来 最近小学期的任务是要做一个学校食堂餐卡管理系统 有“严重拖延症”的我 果然 ...
- hdu, KMP algorithm, linear string search algorithm, a nice reference provided 分类: hdoj 2015-07-18 13:40 144人阅读 评论(0) 收藏
reference: Rabin-Karp and Knuth-Morris-Pratt Algorithms By TheLlama– TopCoder Member https://www.top ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- 数据结构(DataStructure)与算法(Algorithm)、STL应用
catalogue . 引论 . 数据结构的概念 . 逻辑结构实例 2.1 堆栈 2.2 队列 2.3 树形结构 二叉树 . 物理结构实例 3.1 链表 单向线性链表 单向循环链表 双向线性链表 双向 ...
随机推荐
- .Net C# RSA签名和验签
帮助类 using System; using System.Text; using System.IO; using System.Security.Cryptography; namespace ...
- Asp .Net Core 2.0 登录授权以及前后台多用户登录
用户登录是一个非常常见的应用场景 .net core 2.0 的登录方式发生了点变化,应该是属于是良性的变化,变得更方便,更容易扩展. 配置 打开项目中的Startup.cs文件,找到Configur ...
- Celery 初步使用心得
一. 基本介绍 Celery是一个专注于实时处理和任务调度的分布式任务队列.所谓任务就是消息,消息中的有效载荷中包含要执行任务需要的全部数据. 使用Celery常见场景: Web应用.当用户触发的一个 ...
- 转载:Java:字节流和字符流(输入流和输出流)
本文内容: 什么是流 字节流 字符流 首发日期:2018-07-24 什么是流 流是个抽象的概念,是对输入输出设备的抽象,输入流可以看作一个输入通道,输出流可以看作一个输出通道. 输入流是相对程序而言 ...
- php的三个常用判断函数
<?phperror_reporting(E_ERROR);$a;$b = false;$c = '';$d = 0;$e = null;$f = array(); echo 'empty', ...
- [Abp vNext微服务实践] - 前后端分类
一.前景 abp vNext是ABP 开源 Web应用程序框架,是abp的新一代开源web框架.框架完美的集成.net core.identity server4等开源框架,适用于构建web应用程序和 ...
- ConcurrentDictionary源码概读
ConcurrentDictionary的数据结构主要由Tables和Node组成,其中Tables包括桶(Node,节点)数组.局部锁(Local lock).每个锁保护的元素数量(PerLock) ...
- Ubuntu系统---安装QQ
使用Ubuntu很不方便,如果有什么消息的话,还要回到windows中查看.预想在Ubuntu上直接安装一个QQ,有网友说使用WebQQ发现老是掉线,于是这里安装QQ国际版. 首先,下载安装包. 这里 ...
- 【JOISC2012】fish
Description 有 \(n\) 条鱼,第 \(i\) 条鱼的长度为 \(L_i\),颜色是 \(C_i\)(\(C_i\) 只能是 'R','G','B'). 你需要从中挑出至少一条鱼,要求挑 ...
- Hdu 4661 树上拓扑序计数
#include <bits/stdc++.h> using namespace std; typedef long long ll; ; ; ; ], nxt[MAXM << ...