当前位置: 首页 > article >正文

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: asdfghjkl
    string 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   world
    string 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.kler.cn/news/325320.html

相关文章:

  • Jenkins提示Host key verification failed的解决办法
  • Linux Reverse(1)-LD_PRELOAD
  • workerman 接入文心一言的流式输出
  • 解决错误Cloning failed using an ssh key for authentication
  • android system_server进程
  • 【QT 开发日志】QT 基础控件详解:按钮、文本框与标签的使用
  • AlmaLinux 安裝JDK8
  • 嵌入式学习--LinuxDay04
  • 设计模式之模版方法模式
  • 低代码可视化-uniapp蓝牙标签打印-代码生成器
  • 天龙八部怀旧单机微改人面桃花+安装教程+GM工具+虚拟机一键端
  • @overload实际并无作用
  • C# 调用虚拟打印,尝试隐藏进度窗体
  • AfuseKt1.3.6-10110功能强大的安卓网络视频播放器,支持多种在线存储和媒体管理平台!
  • The First项目报告:解读跨链互操作性平台Wormhole
  • 点餐小程序实战教程13餐桌管理
  • 雷池 WAF 如何配置才能正确获取到源 IP
  • GAMES101(作业8)
  • MySQL 加字段锁表怎么解决??
  • 情感短视频素材上哪里找?推荐几个热门情感视频素材资源网站
  • CEPH的写入流程
  • @JsonFormat与@DateTimeFormat的区别
  • 智能监控,守护绿色能源:EasyCVR在电站视频监控中心的一站式解决方案
  • PostgreSQL数据库与PostGIS在Windows中的部署与运行
  • 25基于python的文本冒险岛游戏(源码+游戏简介+python代码学习攻略)校园招聘面试
  • 解决错误:Failed to add the host to the list of known hosts
  • node节点使用:
  • windows下tp5创建定时任务
  • SSH连接Vscode
  • 解决Qt每次修改代码后首次运行崩溃,后几次不崩溃问题