C++中常用函数总结(头文件)

这篇博客总结了C++中常用的头文件函数,包括algorithm的常用操作,cmath库的数学函数,以及cstring中字符串处理的函数。还提到了记忆技巧,如sscanf和sprintf,以及iostream中的cout控制输出精度。
摘要由CSDN通过智能技术生成

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
  • 15
    点赞
  • 143
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值