文章目录

  • 1 algorithm
    • 1.1 函数
    • 1.2 代码示例
  • 2 cmath(c++) / math(c)
    • 2.1 函数
    • 2.2 代码示例
  • 3 cstring (c++)/ string(c)
    • 3.1 函数
    • 3.2 代码示例
  • 4 cctype
  • 5 iomanip

1 algorithm

1.1 函数

类型 函数 说明 举例
最大值 Int/double = max( a,b) 结果类型和a、b对应 (int/ double); <cmath>也相同
最小值 Int/double = min( a,b)
绝对值 int = abs(int) 取绝对值(整数)
交换 swap(x, y) 基本数据类型均可:char 、string string str1="abc", str2="def"; swap(str1, str2);
逆转 reverse( it1, it2 ) 数组、string int arr[4] = {1,2,3,4}; reverse(arr,arr+4);
string str = "abc"; reverse(str.begin(),str.end());
赋值 fill( it1, it2, x) 在[t1, t2) 都赋值x int arr[4]; fill(arr, arr+4, 0);
排序 sort( it1, it2, cmp) cmp可省略,默认升序
排列 next_permutation(it1 it2) 下一轮排列数 string str="123"; next_permutation(str.begin(),str.end()); printf("%s", str.c_str());
查找(>=x) * 或 it = lower_bound( it1, it2, x) 在[t1, t2) 中,返回第一个 >= x的元素在的地方(指针 or 迭代器);若不存在,返回假设存在时应该在的地方
查找(>x) *或it = upper_bound( it1, it2, x) 在[t1, t2) 中,返回第一个 > x的元素在的地方(指针 or 迭代器);若不存在,返回假设存在时应该在的地方

1.2 代码示例

#include<cstdio>
#include<algorithm>
#include<string>
#include<functional>using namespace std;struct Node{int x,y;Node(int _x, int _y):x(_x), y(_y){}Node(){}
}nodes[3];// 降序: x, 升序:y
bool node_cmp(Node no1, Node no2){if(no1.x!=no2.x){return no1.x > no2.x;}else{return no1.y < no2.y;}
}int main(){/*fill*/int a[10];int b[5][10];fill(a, a+10, 2);fill(b[0], b[0]+5*10, 2);/*next_permutation*/// 全排列string str="123";while(next_permutation(str.begin(),str.end())){printf("%s\n", str.c_str());}/*sort*/// 1. 自定义 比较函数cmpnodes[0] = Node(1, 2);nodes[1] = Node(1, 0);nodes[2] = Node(-1, 3);sort(nodes,nodes+3,node_cmp);for(int i=0;i<3;i++){printf("%d %d\n", nodes[i].x, nodes[i].y);}// 2. 用greater(降序)/less(升序、默认)  #include<functional>sort(a,a+10,greater<int>());       // int a[10];/*lower_bound、upper_bound*/int arr[4] = {1,2,2,3};int *p = lower_bound(arr, arr+4, 2);        // 第一个>=2int *q = upper_bound(arr, arr+4, 2);        // 第一个>2printf("%d %d\n", p-arr, q-arr);  // 元素位置return 0;
}

2 cmath(c++) / math(c)

2.1 函数

类型 函数
都是浮点数类型
说明 举例
绝对值 double = fabs(double x) 取绝对值(浮点数)
次方 double = pow(double x, double p) x的p次方
算术平方根 double = sqrt(double x) 根号下x
取整 (向下 / 小) double = floor(double x) 相当于 int(double x) 2 = floor(2.2) = int(2.2)
-3 = floor(-2.2)
取整 (向上 / 大) double = ceil(double x)
取整(四舍六入五成双) double = round(double x) 四舍六入五成双(不是四舍五入)
对数 double = log(double x) 以e为底的对数 改写:以a为底的b的对数
double = log(double b)/log(double a)
三角函数 double = sin(double x)
double = asin(double x)
cos() acos()
tan() atan()

2.2 代码示例

#include<cstdio>
#include<cmath>using namespace std;const double PI = acos(-1.0);double round_up_down(double x){return (x>0.0)?floor(x+0.5):ceil(x-0.5);
}int main(){/*四舍五入(不是round函数)*/double db1 = round_up_down(1.5);printf("%f", db1);/*取整*/double x = 2.2;printf("%f %d %f %f\n", ceil(x),int(x),floor(x),round(x));  //  3.000000     2      2.000000    2.000000x = 2.5;printf("%f %d %f %f\n", ceil(x),int(x),floor(x),round(x));  //  3.000000    2      2.000000     3.000000// 整数x = 2.0;printf("%f %d %f %f\n", ceil(x),int(x),floor(x),round(x));  //  2.000000    2       2.000000    2.000000/*三角函数*/double db = sin(PI * 30 / 180);    // db = sin(PI/6)printf("%f",db);return 0;
}

3 cstring (c++)/ string(c)

3.1 函数

类型 函数 说明 举例
字符串数组:长度 strlen(char chs[]) 字符串实际字符数(不包括’\0’)
字符串数组:连接 strcat(char chs2[], char chs1[]) chs2 = chs2 + chs1
字符串数组:比较 int = strcmp(char chs1[], char chs2[]) 返回<0 ,则 chs1 < chs2
字符串数组:复制 strcpy(char chs2[], char chs1[]) chs2 = chs1(包括’\0’)
赋值 memset(arr, 0或-1, sizeof(arr)) 只能赋值0或-1
字符串 ——> 整数/浮点数 sscanf(str, “%d”, &n);
sscanf(str, “%lf”, &db);
str只能是字符串数组char*,不能是string
整数/浮点数 ——> 字符串 sprintf(str, “%d”, n);
sscanf(str, “%f”, db);
str只能是字符串数组char*,不能是string

注:
sscanf 和 sprintf记忆技巧:

scanf("%d", &n);           // 键盘 ——> n变量
sscanf(str, "%d", &n);        // str ——> n变量printf("%d", n);         // n变量 ——> 屏幕
sprintf(str, "%d", n);        // n变量 ——> str

总结: 相当于键盘/屏幕是 str

3.2 代码示例

#include<cstdio>
#include<cstring>using namespace std;int main(){/*memset*/int arr1[4], arr2[4][4];// 一维memset(arr1, 0, sizeof(arr1));  // sizeof(arr1) = 16 : 字节数 = 4*int// 二维memset(arr2, -1, sizeof(arr2));for(int i=0; i<4; i++){printf("%d\n", arr1[i]);}for(int i=0; i<4;i++){for(int j=0; j<4; j++){printf("%d ",arr2[i][j]);}printf("\n");}/*sscanf*/int n;double db;char chs[20] = "222:3.14:hello";char chs2[20];sscanf(chs,"%d:%lf:%s",&n,&db,chs2); //chs 转换为 n、db、chs2printf("%d %f %s\n",n,db,chs2);//sprintf(chs, "%d-%lf-%s",n,db,chs2);printf("%s\n", chs);return 0;
}

4 cctype

类型 函数 说明 举例
判断 数字 bool isdigit(char)
判断 字母 bool isalpha(char) 大写、小写
判断 字母数字 bool isalnum(char) 大写、小写、数字
判断 大写 bool islower(char)
判断 小写 bool isupper(char)
转换为小写 char tolower(char)
转换为大写 char toupper(char)

5 iomanip

cout 控制输出精读(保留小数点几位)

#include<iostream>
#include<iomanip>using namespace std;int main(){// 输出保留小数点 2 位double db = 2.5555;cout<<setiosflags(ios::fixed)<<setprecision(2)<<db;return 0;
}

C++中常用函数总结(头文件)相关推荐

  1. c++常用函数所在头文件一览

    c++常用函数所在头文件一览 函数           包含              类别              功能 _atold             math.h            ...

  2. linux与window中sleep函数的头文件

    windows下的Sleep函数,首字母为大写,声明在windows.h头文件中,其参数usigned long类型,为毫秒数,即Sleep(1)为睡眠1毫秒. linux下的sleep函数,首字母为 ...

  3. c+pow函数的头文件_pow()函数以及C ++中的示例

    c+pow函数的头文件 C ++ pow()函数 (C++ pow() function) pow() function is a library function of cmath header ( ...

  4. c语言设置输出字符大小_C语言中常用的几个头文件及库函数

    点击上方"C语言中文社区",选择"设为星标★" 技术干货第一时间送达! 来源:https://www.jb51.net/article/124594.htm 这 ...

  5. 标准C函数库头文件、POSIX标准库头文件和Windows API函数库头文件说明

    1. 标准C函数库头文件 名字 源自 描述 <assert.h> 包含断言宏,被用来在程序的调试版本中帮助检测逻辑错误以及其他类型的bug. <complex.h> C99 一 ...

  6. c语言中的stdbool.h头文件,【C语言】中的stdbool.h头文件

    C语言中的stdbool.h头文件 一.相关基础知识 二.具体内容 Win7下安装的VS2015中的stdbool.h的位置为: F:\Program Files (x86)\Microsoft Vi ...

  7. 【Android 内存优化】libjpeg-turbo 函数库交叉编译与使用 ( 交叉编译脚本编写 | 函数库头文件拷贝 | 构建脚本配置 | Android Studio 测试函数库 )

    文章目录 一.交叉编译 Shell 脚本参考 二.NDK r16b 版本配置 三.libjpeg-turbo 交叉编译 Shell 脚本 四.执行 libjpeg-turbo 交叉编译 Shell 脚 ...

  8. TF:tensorflow框架中常用函数介绍—tf.Variable()和tf.get_variable()用法及其区别

    TF:tensorflow框架中常用函数介绍-tf.Variable()和tf.get_variable()用法及其区别 目录 tensorflow框架 tensorflow.Variable()函数 ...

  9. oracle中各种函数,oracle中常用函数大全

    1.数值型常用函数 函数 返回值 样例 显示 ceil(n) 大于或等于数值n的最小整数 select ceil(10.6) from dual; 11 floor(n) 小于等于数值n的最大整数 s ...

  10. php开发中常用函数总结,PHP开发中常用函数总结

    PHP开发中常用函数总结 发布于 2014-10-31 08:34:03 | 48 次阅读 | 评论: 0 | 来源: 网友投递 PHP开源脚本语言PHP(外文名: Hypertext Preproc ...

最新文章

  1. 网络编程学习笔记(tcp_listen函数)
  2. mysql 同机做主从
  3. plus 什么是mybais_谈谈自己用mybatis-plus中的一些经验。
  4. docker设置镜像加速器
  5. 这个深度学习Model Zoo,真的有点像动物园? | 来自一只新加坡蓝精灵
  6. 阿里云张献涛:公共云不断向外延伸,一云多态是未来趋势
  7. sqlplus基本使用
  8. python制作圆形按钮_圆形按钮tkinter python
  9. 联想电脑如何关闭/开启windows自动更新
  10. elasticsearch2.2之index映射参数的not_analyzed属性
  11. 免费思维导图mindmanager2020下载安装(附序列号)
  12. 房子装饰风水有哪些讲究和忌讳
  13. H5静态网页设计与制作_川西旅游网设计作品(HTML+CSS+jQuery)
  14. java 6u45 no sni 2_sjscxz.taobao.com
  15. 稳健收益,缺你不可—A股优秀的基金和基金经理
  16. js使用广度优先给树形结构添加level
  17. 要大进步就不能两手抓
  18. 35搜索插入位置之Leecode—《数组篇》(二分法)
  19. Handler运行机制
  20. iOS 镜头变焦,推近或者拉远焦距--ZOOM

热门文章

  1. CentOS 离线安装 postgresql 12
  2. ulua中lua代码使用反射调用c#详解
  3. Python-ppt模板批量下载
  4. Bolt 的 Flutter 路由管理实践(页面解耦,流程控制、功能拓展等)
  5. Java习题集第八章Java的网络程序设计
  6. 005-part3-创世区块链
  7. 推荐几款免费web应用防火墙(云waf)
  8. vue项目之购物流程
  9. php 数组笛卡尔积,多个数组求笛卡尔积
  10. 三星手机通讯录导出 .spb格式转.vcf