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 ...
随机推荐
- Centos 7 搭建蓝鲸V4.1.16稳定社区版
在本地用VMware模拟了三台主机 准备至少3台 CentOS 7 以上操作系统的机器,保证三台虚拟机都可以上网 最低配置:2核4G(我用的是这个) 建议配置: 4核12G 以上 192.168.16 ...
- c 判断一个字符是否为字母数字
#include <stdio.h> #include <wctype.h> int main () { int i; wchar_t str[] = L"c3po. ...
- codeforces#1187E. Tree Painting(树换根)
题目链接: http://codeforces.com/contest/1187/problem/E 题意: 给出一颗树,找到一个根节点,使所有节点的子节点数之和最大 数据范围: $2 \le n \ ...
- Linux 网络通信命令之 netstat
定义 Linux netstat命令用于显示网络状态. 利用netstat指令可让你得知整个Linux系统的网络情况. 语法 netstat [-acCeFghilMnNoprstuvVwx][-A& ...
- mysql 日期字符串互转
字符串转日期select str_to_date('2008-4-2 15:3:28','%Y-%m-%d %H:%i:%s');select str_to_date('2008-08-09 08:9 ...
- LeetCode 128. 最长连续序列(Longest Consecutive Sequence)
题目描述 给定一个未排序的整数数组,找出最长连续序列的长度. 要求算法的时间复杂度为 O(n). 示例: 输入: [100, 4, 200, 1, 3, 2] 输出: 4 解释: 最长连续序列是 [1 ...
- moogdb操作
本文转载自 https://my.oschina.net/kakakaka/blog/347954 首先,下载mongdb对JAVA的支持,点击这里下载驱动包,这里博主下载的是2.10.1版. mon ...
- git分布式版本管理系统
Git是分布式版本管理系统Svn是集中式版本管理系统 git速度快,适合大规模协同开发 什么是分布式版本管理系统 假如有10个人,每个人的代码库都是独立的,自己想进行代码提交回滚都可以,无需链接中央服 ...
- [转]SQL server 2008R2 中 C#Winfoirm 使用 SqlDependency 机制实现 数据库中某一张表的监视
转自:https://blog.csdn.net/u012183487/article/details/77776930 System.Data.SqlClient命名空间下的 sqlDependen ...
- 用Python计算三角函数之acos()方法的使用
用Python计算三角函数之acos()方法的使用 acos()方法返回x的反余弦值,以弧度表示. 语法 以下是acos()方法的语法: acos(x) 注意:此函数是无法直接访问的,所以我们 ...