C++不同的头文件中各种函数的操作使用(长期更新,找到新的就补充进来)

一、万能头文件

#include <bits/stdc++.h>

万能头文件中包含的内容

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif 

缺点

  • 不是GNU C++库的标准头文件,在部分情况下会编译失败;

  • 包含了很多不必要的东西,会大大增加编译时间,但不会影响运行时间。

二、 C++对数据的输入输出格式控制

2.1 C语言输出的控制(对齐+占位)
#include<stdio.h>
int main()
{int a;scanf("%d",&a);// 表示占8个字符的宽度,负号表示左对齐print("%-8d",a);            return 0;
}   
2.2 C++语言输出的控制(对齐+占位)
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{int a;cin>>a;// 表示占8个字符的宽度,left表示左对齐cout<<setw(8)<<left<<a<<endl;// 表示占8个字符的宽度,right表示右对齐cout<<setw(8)<<right<<a<<endl;return 0;
}

输出样例:八个字符的宽度,右对齐。

三、C++对浮点类型数据的操作

3.1 保留小数问题

#include<iomanip>

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{	double number=3.1415926535;// 四舍五入保留两位小数cout<<fixed<<setprecision(2)<<number<<endl;// 取整样例double number1 = 2.8;double number2 =-2.8;// 向下取整cout<<floor(number1)<<" "<<floor(number2)<<endl;// 向上取整cout<<ceil(number1)<<" "<<ceil(number2)<<endl;// 四舍五入cout<<round(number1)<<" "<<round(number2)<<endl;return 0;
}

输出样例:保留两位小数

 

3.2 浮点数的精度操作
函数名称函数说明2.8-2.8
floor不大于变量的最大整数(向下取整)2-3
ceil不小于变量的最小整数(向上取整)3-2
round四舍五入3-3

四、C++中string的处理

4.1 普通的string读取是到空格结束的
#include<iostream>
#include<string>
using namespace std;
int main()
{   // 只能读取连续的字符串// eg: asdfghjklstring str;cin>>str;cout<<str<<endl;return 0;
}

输出样例:输入Hello World,只输出Hello

 

4.2 getline读取一行

头文件

#include<string>

功能

1. getline(cin, str);   // 读取一行,包括空格

2. getline(cin, str,':');  // 指定用":"作为界定符,读取":"之前的内容,默认的界定符是"\n"

3. getline可以用于检测空行,并作出处理。

#include<iostream>
#include<string>
using namespace std;
int main()
{// 按行读取,有空格也能读入// eg: hello   worldstring str;getline(cin, str);cout << str << endl;return 0;
}

输出样例:输入Hello World,输出Hello World

 

4.3 大小写转换
#include<iostream>
#include<cctype>
using namespace std;
int main()
{string str;cin >> str;for (int i = 0; i < str.length(); i++) {if (str[i] >= 'A' && str[i] <= 'Z')str[i] = tolower(str[i]);else if (str[i] >= 'a' && str[i] <= 'z')str[i] = toupper(str[i]);}cout << str << endl;
}

输出样例:将输入的字符串中的内容,大写转换为小写,小写转换为大写。

五、算法头文件

#include<algorithm>

5.1 sort() 排序

sort(arr,arr+size);                               // 默认为升序

sort(zrr,arr+size,greater<int>());        // 为降序

#include<iostream>
#include<algorithm>
using namespace std;
bool compare1(int a, int b);
bool compare2(int a, int b);
void Print(int arr[], int size);
int main()
{int arr[] = { 1,3,5,7,9,8,6,4,2 };int size = sizeof(arr) / sizeof(arr[0]);cout << "升序排列:" << endl;sort(arr,arr+size);Print(arr, size);cout << "降序排列:" << endl;sort(arr, arr + size, greater<int>());Print(arr, size);cout << "========================================" << endl;cout << "用自定义比较器来做比较" << endl;sort(arr, arr + size, compare1);cout << "升序排列:" << endl;Print(arr, size);sort(arr, arr + size, compare2);cout << "降序排列:" << endl;Print(arr, size);return 0;
}
bool compare1(int a, int b)
{return a < b;
}
bool compare2(int a, int b)
{return a > b;
}
void Print(int arr[],int size)
{for (int i = 0; i < size; i++){cout << arr[i] << " ";}cout << endl;
}

输出样例:输入一个int类型的数组,用algorithm库中的sort函数实现升序和降序的排列,默认是升序。通过自定义比较器,同理可比较,进行升序降序排列。 

用vector容器同理

vector容器的用法可以参考蓝字文章。

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool compare1(int a, int b);
bool compare2(int a, int b);
void Print(vector<int> arr);
int main()
{vector<int> arr = { 1,3,5,7,9,8,6,4,2 };	cout << "升序排列:" << endl;// 用迭代器进行访问sort(arr.begin(),arr.end());Print(arr);cout << "降序排列:" << endl;sort(arr.begin(), arr.end(), greater<int>());Print(arr);cout << "========================================" << endl;cout << "用自定义比较器来做比较" << endl;sort(arr.begin(), arr.end(), compare1);cout << "升序排列:" << endl;Print(arr);sort(arr.begin(), arr.end(), compare2);cout << "降序排列:" << endl;Print(arr);return 0;
}
bool compare1(int a, int b)
{return a < b;
}
bool compare2(int a, int b)
{return a > b;
}
void Print(vector<int> arr)
{for (vector<int>::iterator it = arr.begin(); it != arr.end(); it++){cout << *it << " ";}cout << endl;
}

输出样例:

5.2 max() 最大值,min() 最小值

5.3 swap() 交换

六、数学函数头文件

#include<math.h>

#include<cmath>

6.1 pow(底数,幂数)

6.2 sqrt(x) // 计算x的平方根,例如sqrt(16)的平方根是4.0

6.3 abs() 绝对值

6.4 ‌sin(x),cos(x),tan(x) 分别计算x的正弦、余弦、正切值。这些函数接受以弧度为单位的角度作为输入。

6.5 log(x)和log10(x) 分别计算x的自然对数和以10为底的对数。

6.6 exp(x) 计算e的x次幂,其中e是自然对数的底。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1548787.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

leetcode每日一题day17(24.9.27)——每种字符最少取k个

思路&#xff1a;看到题目就想到了搜索&#xff0c; 广搜&#xff1a;满足要求就往后搜&#xff0c;最后返回搜索队列达到过的最大深度&#xff0c; 深搜&#xff1a;一直往一边取&#xff0c;搜索完所有可能&#xff0c;并在此基础上进行剪枝&#xff0c;剪枝方案有如果某一分…

Windows环境部署Oracle 11g

Windows环境部署Oracle 11g 1.安装包下载2. 解压安装包3. 数据库安装3.1 执行安装脚本3.2 电子邮件设置3.3 配置安装选项3.4 配置系统类3.5 选择数据库安装类型3.6 选择安装类型3.7 数据库配置3.8 确认安装信息3.9 设置口令 Oracle常用命令 2023年10月中旬就弄出大致的文章&…

如何在Unity WebGL上实现一套全流程简易的TextureStreaming方案

项目介绍 《云境》是一款使用Unity引擎开发的WebGL产品&#xff0c;有展厅&#xff0c;剧本&#xff0c;Avatar换装&#xff0c;画展&#xff0c;语音聊天等功能&#xff0c;运行在微信小程序和PC&#xff0c;移动端网页&#xff0c;即开即用。 当前问题和现状 当前项目…

【质优价廉】GAP9 AI算力处理器赋能智能可听耳机,超低功耗畅享未来音频体验!

当今世界&#xff0c;智能可听设备已经成为了流行趋势。随后耳机市场的不断成长起来&#xff0c;消费者又对AI-ANC&#xff0c;AI-ENC&#xff08;环境噪音消除&#xff09;降噪的需求逐年增加&#xff0c;但是&#xff0c;用户对于产品体验的需求也从简单的需求&#xff0c;升…

mbedtls错误记录

0x2180 证书格式无效&#xff0c;可以检查证书的格式是否正确&#xff0c;或传入的证书长度是否正确 mbedtls_x509_crt_parse-》mbedtls_x509_crt_parse_der-》x509_crt_parse_der_core-》mbedtls_x509_get_sig_alg-》return( MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG ret ); 所以26…

LampSecurityCTF7 靶机渗透 (sql 注入, 文件上传, 密码喷射)

靶机介绍 LampSecurityCTF7&#xff0c;vulnhub 靶机 主机发现 由于靶机配置问题&#xff0c;扫不到 ip 这里需要特别注意一下&#xff0c;在第一次启动打开靶机的时候&#xff0c;vmware会跳出一个提示框&#xff0c;让你选择我已复制该虚拟机/我已移动该虚拟机&#xff0c…

GIS专业在课余应该学计算机还是遥感?

有网友提问&#xff1a; 绝大数人给出了&#xff0c;强有力的建议&#xff0c;就是冲计算机 1、从学习条件上看本科阶段&#xff0c;学计算机编程&#xff0c;你只需要有台电脑&#xff0c;装一些编程软件&#xff0c;上git上找一些代码&#xff0c;b站找一些教程就可以大学特…

Verilog基础:时序调度中的竞争(四)(描述时序逻辑时使用非阻塞赋值)

相关阅读 Verilog基础https://blog.csdn.net/weixin_45791458/category_12263729.html?spm1001.2014.3001.5482 作为一个硬件描述语言&#xff0c;Verilog HDL常常需要使用语句描述并行执行的电路&#xff0c;但其实在仿真器的底层&#xff0c;这些并行执行的语句是有先后顺序…

AI产品经理面试题详细整理【已拿offer】

面试题整理 以下是我面试过的AI产品经理岗位的精选面试题&#xff0c;供各位同仁参考&#xff1a; &#x1f4bc; 公司概览&#xff1a; 字节跳动、百度、昆仑天工、minimax、彩云、蕞右、粉笔、作业帮、火花、好未来等知名企业。 &#x1f4cd; 方向分类&#xff1a; 模型…

【移植】小型系统平台驱动移植

往期知识点记录&#xff1a; 鸿蒙&#xff08;HarmonyOS&#xff09;应用层开发&#xff08;北向&#xff09;知识点汇总 鸿蒙&#xff08;OpenHarmony&#xff09;南向开发保姆级知识点汇总~ 持续更新中…… 平台驱动移植 在这一步&#xff0c;我们会在源码目录 //device/ve…

【Python】Flask-Admin:构建强大、灵活的后台管理界面

在 Web 应用开发中&#xff0c;构建一个直观且功能丰富的后台管理系统对于处理数据和维护应用至关重要。虽然构建一个完全自定义的管理后台界面非常耗时&#xff0c;但 Flask-Admin 提供了一个简洁、灵活的解决方案&#xff0c;可以让开发者快速集成一个功能齐全的后台管理系统…

防盗智能电子锁的使用

一、防盗智能电子锁的介绍 以宏泰HONGTAI的DJ08产品为例。 功能&#xff1a; 自动补锁、开锁并智能纠正人为错误操作行为&#xff1b;开启方式有门禁电控、钥匙、旋钮等&#xff1b;开门方向&#xff0c;左右、内外通用&#xff1b;带信号反馈&#xff0c;开锁声光提示&#…

数据结构:树的定义及其性质

树的定义 树是一种重要的非线性数据结构&#xff0c;树作为一种逻辑结构&#xff0c;同时也是一种分层结构。具有以下两个特点&#xff1a; 1.树的根结点没有前驱&#xff0c;除根结点意外的节点只有一个前驱 2.树中所有结点都可以有0个或多个后继 树结构在多个领域都有广泛…

【Python】字典 文件操作 生成二维码 多媒体操作

目录 字典 创建字典 查找key 新增键值对 修改键值对 删除键值对 遍历键值对 keys() values() items() 合法的key类型 文件 文件是什么 打开文件 关闭文件 写文件 读文件 *上下文管理器 实现文件查找工具 pip包管理器 生成二维码 安装第三方库 生成二维…

MySql在更新操作时引入“两阶段提交”的必要性

日志模块有两个redo log和binlog&#xff0c;redo log 是引擎层的日志&#xff08;负责存储相关的事&#xff09;&#xff0c;binlog是在Server层&#xff0c;主要做MySQL共嗯那个层面的事情。redo log就像一个缓冲区&#xff0c;可以让当更新操作的时候先放redo log中&#xf…

2024.9.24 作业

My_string类中的所有能重载的运算符全部进行重载、[] 、>、、>) 仿照stack类实现my_stack,实现一个栈的操作 #include <iostream> #include <cstring>using namespace std;class My_string{ private:char *ptr;int size;int len;public://无参构造My_strin…

Miniforge详细安装教程(macOs和Windows)

(注&#xff1a;主要是解决商业应用anaconda收费问题&#xff0c;这是轻量级的代替&#xff0c;个人完全可以使用anaconda和miniconda) Miniforge 是一个轻量级的包管理器&#xff0c;类似于 Anaconda 和 Miniconda。它主要用于安装基于 conda 的 Python 环境&#xff0c;专注于…

IPEmotion 2024 R2现支持Amazon S3和Windows SMB服务器

新版IPEmotion 2024 R2软件推出了许多新功能&#xff0c;其中的一大功能是支持Amazon S3、Windows SMB服务器以及新的IPE-CAM-007 USB摄像头。IPEmotion 2024 R2还支持直接写入TEDS数据和配置可装载电池的新款IPE833记录仪。 — 创新成果一览 — ■ 支持Amazon S3、Windows SM…

IDEA 系列产品 下载

准备工作 下载 下载链接&#xff1a;https://www.123865.com/ps/EF7OTd-mbHnH 仅供参考 环境 演示环境&#xff1a; 操作系统&#xff1a;windows10 产品&#xff1a;IntelliJ IDEA 版本&#xff1a;2024.1.2 注意&#xff1a;如果需要其他产品或者版本可以自行下载&#xff0…

虚幻引擎UE5如何云渲染,教程来了

​步骤一&#xff1a;获取云渲染权限 访问渲染101官网&#xff0c;使用云渲码6666进行注册。 下载并安装渲染客户端。 步骤二&#xff1a;设置渲染环境 确保云渲染环境与您的本地环境一致&#xff0c;避免出错。 步骤三&#xff1a;任务提交 完成环境配置后&#xff0c;解析…