STL algorithm 头文件下的常用函数
algorithm 头文件下的常用函数
1. max(), min()和abs()
//max(x,y)和min(x,y)分别返回x和y中的最大值和最小值,且参数必须时两个(可以是浮点数)
//返回3个数的最大数值可以使用max(x,max(y,z))
//abs(x)返回x的绝对值。
//浮点型的绝对值请用math头文件下的fabs
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int x = 1, y = -2;
printf("%d %d\n", max(x,y), min(x,y));
printf("%d %d\n", abs(x), abs(y));
return 0;
}
2. swap()
//swap(x,y)用来交换x和y的值
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int x = 1, y = 2;
swap(x, y);
printf("%d %d\n", x, y);
return 0;
}
3. reverse()
//reverse(it, it2)可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内的元素进行反转
//对元素进行反转
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int a[10] = {10, 11, 12, 13, 14, 15};
reverse(a, a + 4); //将a[0] ~ a[3]进行反转
for(int i = 0; i < 6; i++) {
printf("%d ", a[i]);
}
return 0;
}
//对容器中的元素进行反转
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main() {
string str = "abcdefghi";
reverse(str.begin() + 2, str.begin() + 6); //对str[2] ~ str[5]反转
for(int i = 0; i < str.length(); i++) {
printf("%c", str[i]);
}
return 0;
}
4. next_permutation()
//next_permutation()给出一个序列在全排列中的下一个序列
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int a[10] = {1, 2, 3};
//a[0] ~ a[2]之间的序列需要求解next_permutation
do {
printf("%d%d%d\n", a[0], a[1], a[2]);
} while(next_permutation(a, a + 3));
return 0;
}
//使用循环是因为next_permutation在已经到达全排列的最后一个时会返回false
5. fill()
//fill()可以把数组或容器中的某一段区间赋为某个相同的值。
//和memset不同,这里的赋值可以是数组类型对应范围中的任意值。
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int a[5] = {1, 2, 3, 4, 5};
fill(a, a + 5, 233); //将a[0] ~ a[4]均赋值为233
for(int i = 0; i < 5; i++) {
pritnf("%d ", a[i]);
}
return 0;
}
6. sort()
//sort就是用来排序的函数,实际复杂度退化到O(n^2)
(1) 使用sort排序
sort(首元素地址(必填), 尾元素地址的下一个地址(必填), 比较算法(非必填));
//sort的参数有三个,其中前两个是必填的,而比较函数则可以根据需要填写
//如果不写比较函数,则默认对前面给出的区间进行递增排序
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int a[6] = {9, 4, 2, 5, 6, -1);
//将a[0] ~ a[3]从小到大排序
sort(a, a + 4);
for(int i = 0; i < 6; i++) {
printf("%d ", a[i]);
}
printf("\n");
//将a[0] ~ a[5]从小到大排序
sort(a, a + 6);
for(int i = 0; i < 6; i++) {
printf("%d ", a[i]);
}
return 0;
}
//double型数组排序
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
double a[] = {1.4, -2.1, 9};
sort(a, a + 3);
for(int i = 0; i < 3; i++) {
pritnf("%.lf", a[i]);
}
return 0;
}
//char型数组排序(默认字典序)
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
char c[] = {'T', 'W', 'A', 'K'};
sort(c, c + 4);
for(int i = 0; i < 4; i++) {
printf("%c", c[i]);
}
return 0;
}
(2) 如何实现比较函数cmp
//<1> 基本数据类型数据的排序
//对int型数组的排序
//默认从小到大排序
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int a[5] = {3, 1, 4, 2};
sort(a, a + 4);
for(int i = 0; i < 4; i++) {
printf("%d ", a[i]);
}
return 0;
}
//想要从大到小排序,则要使用比较函数cmp来告诉sort
//何时要交换元素(让元素的大小比较关系反过来)
#include <stdio.h>
#include <algorithm>
using namespace std;
bool cmp(int a, int b) { return a > b; //当a > b时把a放在b前面}
int main() {
int a[] = {3, 1, 4, 2};
sort(a, a + 4, cmp);
for(int i = 0; i < 4; i++) {
printf("%d ", a[i]); //输出4 3 2 1
}
return 0;
}
//对doble型数组从大到小排序
#include <stdio.h>
#include <algorithm>
using namespace std;
bool cmp(double a, double b) {
return a > b;
}
int main() {
double a[] = {1.4, -2.1, 9};
sort(a, a + 3, cmp);
for(int i = 0; i < 3; i++) {
printf("%.lf ", a[i]);
}
return 0;
}
//char型数组从大到小排序
#include <stdio.h>
#include <algorithm>
using namespace std;
bool cmp(char a, char b) {
return a > b;
}
int main() {
char c[] = {'T', 'W', 'A', 'K'};
sort(c, c + 4, cmp);
for(int i = 0; i < 4; i++) {
printf("%c", c[i]);
}
return 0;
}
//<2> 结构体数组的排序
//定义结构体:
struct node {
int x, y;
} ssd[10];
//如果想将ssd数组按照x从大到小排序(即进行一级排序),那么可以这样写cmp函数
bool cmp(node a, node b) {
return a.x > b.x;
}
//示例:
#include <stdio.h>
#include <algorithm>
using namespace std;
struct node {
int x, y;
} ssd[10];
bool cmp(node a, node b) {
return a.x > b.x; //按x值从大到小对结构体数组进行排序
}
int main() {
ssd[0].x = 2; //{2, 2}
ssd[0].y = 2;
ssd[1].x = 1; //{1, 3}
ssd[1].y = 3;
ssd[2].x = 3; //{3, 1}
ssd[2].y = 1;
sort(ssd, ssd + 3; cmp); //排序
for(int i = 0; i < 3; i++) {
printf("%d %d\n", ssd[i].x, ssd[i].y);
}
return 0;
}
//按x从大到小排序,但当x相等的情况下,按照y的大小从小到大排序(即进行耳机排序)
//cmp的写法:
bool cmp(node a, node b) {
if (a.x != b.x) return a.x > b.x;
else return a.y < b.y;
}
//cmp函数首先判断结构体内的x元素是否相等,如果不相等,则直接按照x的大小排序
//否则,比较两个结构体中y的大小,并按y从小到大排序
//示例:
#include <stdio.h>
#include <algorithm>
using namespace std;
struct node {
int x, y;
} ssd[10];
bool cmp(node a, node b) {
if(a.x != b.x) return a.x > b.x; //x不等时按x从大到小排序
else return a.y < b.y; //x相等时按y从小到大排序
}
int main() {
ssd[0].x = 2; //{2, 2}
ssd[0].y = 2;
ssd[1].x = 1; //{1, 3}
ssd[1].y = 3;
ssd[2].x = 2; //{2, 1}
ssd[2].y = 1;
sort(ssd, ssd + 3, cmp); //排序
for(int i = 0; i < 3; i++) {
printf("%d %d\n", ssd[i].x, ssd[i].y);
}
return 0;
}
//<3> 容器的排序
//在STL标准容器中,只有vector, string, deque是可以使用sort的。
//set, map这种容器时用红黑树实现的,元素本身有序,故不允许使用sort排序
//示例:
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(int a, int b) { //因为vector中的元素为int型,因此仍然时int的比较
return a > b;
}
int main() {
vector<int> vi;
vi.push_back(3);
vi.push_back(1);
vi.push_back(2);
sort(vi.begin(), vi.end(), cmp); //对整个vector进行排序
for(int i = 0; i < 3; i++) {
printf("%d ", vi[i]);
}
return 0;
}
//string的排序
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str[3] = {"bbbb", "cc", "aaaa"};
sort(str, str + 3); //将string型数组按字典序从小到大输出
for(int i = 0; i < 3; i++) {
cout << str[i] << endl;
}
return 0;
}
//按字符串长度从小到大排序
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool cmp(string str1, string str2) {
return str1.length() < str2.length(); //按string的长度从小到大排序
}
int main() {
string str[3] = {"bbbb", "cc", "aaa"};
sotr(str, str + 3, cmp);
for(int i = 0; i < 3; i++) {
cout << str[i] << endl;
}
return 0;
}
7. lower_bound()和upper_bound()
//lower_bound()和upper_bound()需要在一个有序数组或容器中。
//lower_bound(first, last, val)用来寻找在数组或容器的[first,last)范围内第一个值大于等于val的元素的位置
//如果是数组,则返回该位置的指针;如果是容器,则返回该位置的迭代器
//upper_bound(first, last, val)用来寻找在数组或容器的[first,last)范围内第一个值大于val的元素的位置
//如果是数组,则返回该位置的指针;如果是容器,则返回该位置的迭代器
//lower_bound()和upper_bound的复杂度均为O(log(last - first))
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int a[10] = {1, 2, 2, 3, 3, 3, 5, 5, 5, 5}; //注意数组下标从0开始
//寻找-1
int* lowerPos = lower_bound(a, a + 10, -1);
int* upperPos = upper_bound(a, a + 10, -1);
printf("%d, %d\n", lowerPos - a, upperPos - a);
//寻找1
lowerPos = lower_bound(a, a + 10, 1);
upperPos = upper_bound(a, a + 10, 1);
printf("%d %d\n", lowerPos - a, upperPos - a);
//寻找3
lowerPos = lower_bound(a, a + 10, 3);
upperPos = upper_bound(a, a + 10, 3);
printf("%d %d\n", lowerPos - a, upperPos - a);
//寻找4
lowerPos = lower_bound(a, a + 10, 4);
upperPos = upper_bound(a, a + 10, 4);
printf("%d %d\n", lowerPos - a, upperPos - a);
//寻找6
lowerPos = lower_bound(a, a + 10, 6);
upperPos = upper_bound(a, a + 10, 6);
printf("%d %d\n", lowerPos - a, upperPos - a);
return 0;
}
//如果只是向获得欲查元素的下标,就可以不使用指针,而直接令返回值减去数组首地址即可
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int a[10] = {1, 2, 2, 3, 3, 3, 5, 5, 5, 5};
printf("%d, %d\n", lower_bound(a, a + 10, 3) - a, upper_bound(a, a + 10, 3) - a);
return 0;
}
STL algorithm 头文件下的常用函数的更多相关文章
- algorithm头文件下的常用函数
algorithm头文件常用高效函数 max() max(a, b)返回a和b中的最大值,参数必须是两个(可以是浮点型). 1 #include <iostream> 2 #include ...
- 头文件<cmath>中常用函数
<cmath>里面有很多数学函数,下面说一下常用的一些函数吧:直接把函数原型给了出来,用的时候注意参数 先说一下,c++自身是没有四舍五入函数round()的,若果你要用到的话,可以自己写 ...
- algorithm下的常用函数
algorithm下的常用函数 max(),min(),abs() max(x,y)返回x和y中最小的数字 min(x,y)返回x和y中最大的数字 abs(x)返回x的绝对值,注意x应当是整数,如果是 ...
- linux设备驱动程序该添加哪些头文件以及驱动常用头文件介绍(转)
原文链接:http://blog.chinaunix.net/uid-22609852-id-3506475.html 驱动常用头文件介绍 #include <linux/***.h> 是 ...
- 【Linux 应用编程】文件IO操作 - 常用函数
Linux 系统中的各种输入输出,设计为"一切皆文件".各种各样的IO统一用文件形式访问. 文件类型及基本操作 Linux 系统的大部分系统资源都以文件形式提供给用户读写.这些文件 ...
- pandas 学习(二)—— pandas 下的常用函数
import pandas as pd; 1. 数据处理函数 pd.isnull()/pd.notnull():用于检测缺失数据: 2. 辅助函数 pd.to_datetime() 3. Series ...
- c++标准模板库algorithm头文件中accumulate算法的代码
template <typename T>T algorithm(T* start, T* end, T total)//把[start, end)标记范围内所有元素累加到total中{ ...
- algorithm与numeric的一些常用函数
numeric中的accumulated的基本用法: 来自:https://blog.csdn.net/u011499425/article/details/52756242 #include < ...
- PHP_File文件操作简单常用函数
php测试文件 <?php header("Content-type:text/html;charest=utf-8");$fileDir='Upload/File/cont ...
随机推荐
- 7.20T1
排序(sort) [问题描述] 有 n 个人依次站在小 A 面前.小 A 会依次对这 n 个人进行 m 次操作. 每次操作选择一个位置 k,将这 n 个人中的所有身高小于等于当前 k 位置的 人的身高 ...
- C语言实现简单的哈希表
这是一个简单的哈希表的实现,用c语言做的. 哈希表原理 这里不讲高深理论,只说直观感受.哈希表的目的就是为了根据数据的部分内容(关键字),直接计算出存放完整数据的内存地址. 试想一下,如果从链表中根据 ...
- HDU 3689 Infinite monkey theorem ——(自动机+DP)
这题由于是一个单词,其实直接kmp+dp也无妨.建立自动机当然也是可以的.设dp[i][j]表示匹配到第i个字母的时候,在单词中处于第j个位置的概率,因此最终的答案是dp[0~m][len],m是输入 ...
- nc浏览器的十宗罪
1.收藏夹.nc浏览器收藏夹无法导出或者导出困难,十分恶心.其他的小众软件都有这个简单的功能,某天我突然想到为什么手机nc浏览器连个导出收藏夹的功能都没有,并不是不注重用户体验,或则导功能很难实现不会 ...
- zookeeper系列(九)zookeeper的会话详解
作者:leesf 掌控之中,才会成功:掌控之外,注定失败. 出处:http://www.cnblogs.com/leesf456/p/6103870.html尊重原创,大家共同学习: 一.前言 ...
- java按某个字段对数据分组
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...
- crawler 使用jQuery风格实现
以前写过java版的crawler,最近看了Groovy的XmlSlurper,效果还是不太满意,直到这篇文章启发了我:how-to-make-a-simple-web-crawler-in-java ...
- Android APP切换到后台接收不到推送消息
1. Android端进程被杀死后,目前自带的保护后台接收消息活跃机制.暂时没有什么好的机制保持任何情况下都活跃 android原生系统用home键杀进程可以起来,如果是强行停止就只能用户自己手动 ...
- incredibuild(分布式任务软件)脚本
IncrediBuild 可以在Server段通过修改单个任务的进程上限来实现提升任务执行速度. IncredBuild本机版也可以用来进行本机实现多线程任务分发,这样可以充分利用多核资源. 提交分布 ...
- 记一个微信支付-1错误JSAPI缺少参数app|get_brand_request:Fail
最近公司要做一个H5小游戏里边涉及到微信公众号支付,中间摸爬滚打遇到了很多坑.记录一下,留待后人看. 我们来看一下这个方法 GetJsApiParameters 怎么样,看起来像不像输出了一个JSON ...