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 ...
随机推荐
- PAT L2-001 紧急救援 —— (多参数最短路)
和天梯中的直捣黄龙差不多.但是,通过这个问题,我对多参数最短路又有了更深一层的了解. 这题因为点数比较多,所以如果直接用大力学长的在G上dfs找最短路径的条数的话,会TLE,所以需要剪枝.剪枝方法是, ...
- 手把手教你设置MongoDB密码
mongodb密码和传统数据如mysql等有些区别: mongodb的用户名和密码是基于特定数据库的,而不是基于整个系统的.所有所有数据库db都需要设置密码. 1. 查看所有数据库(在mongodb新 ...
- LVS之NAT模式
目录: 网络环境 LVS服务器配置 LVS服务器添加ipvs规则 RS服务器配置 访问验证 抓包分析 注意事项 [网络环境] 服务器类型 IP 说明 lvs_vip 192.168.2.130 vip ...
- VMware Workstation 与 Device/Credential Guard 不兼容
之前在本机搭建Docker for Windows的时候,启用了win10自带的虚拟Hyper-V,但是win10的虚拟与VMware Workstation的虚拟有冲突,运行VMware Works ...
- 简易总结react-hook三大基础
react-hook的最重要的三大基础 import { useEffect, useState, useContext } from 'react' 1.每一个简单的一句话总结 useEffect ...
- 括号序列模型--序列dp--U86873 小Y的精灵国机房之旅
括号序列模型及解法 >Codeforces314E◦给定一个长度为n的仅包含左右括号和问号的字符串,将问号变成左括号或右括号使得该括号序列合法,求方案总数.◦例如(())与()()都是合法的括号 ...
- 运行python程序不显示cmd方法
运行python程序不显示cmd方法 Pythonw xxx.py 将*.py改成*.pyw,然后执行*.pyw Python.exe和pythonw.exe不同: 执行时没有控制台窗口 所有向原有的 ...
- 使用Mock 测试 controller层
package action; import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import ...
- lumen中间件中设置响应header
<?php namespace App\Http\Middleware; use Closure; class BeforeMiddleware { public function handle ...
- [Http] Difference between POST and GET?
What is the difference between POST and GET HTTP requests? GET and POST are two different types of H ...