竞赛算法中的常用库以及库函数
标准库以及库函数
- 一、`<string>` 库常用函数
- 1.常用库函数
- 2.示例代码
- 二、`<algorithm>` 库常用函数
- 1.常用库函数
- 2.示例代码
- 三、`<cmath>` 库常用函数
- 1.常用库函数
- 2.示例代码
在蓝桥杯,acm,ccpc等包含C++算法竞赛中,有许多标准库,包含大量可直接调用的函数,能显著提升编程效率。
一、<string>
库常用函数
1.常用库函数
string
类提供了便捷的字符串操作方法,比C风格字符串更安全高效:
-
基础属性
s.size()
/s.length()
:返回字符串长度s.empty()
:判断字符串是否为空(为空返回true
)s.clear()
:清空字符串内容
-
元素访问
s[i]
:访问第i个字符(无越界检查,高效)s.at(i)
:访问第i个字符(有越界检查,抛出异常)s.front()
/s.back()
:直接获取首/尾字符
-
字符串操作
s += t
/s.append(t)
:拼接字符串t到s末尾s.insert(pos, t)
:在位置pos插入字符串ts.erase(pos, len)
:从pos开始删除len个字符(默认删除到末尾)s.substr(pos, len)
:提取从pos开始的len个字符(len默认到末尾)s.find(t)
:查找t首次出现的位置,未找到返回string::npos
s.rfind(t)
:从尾部开始查找t首次出现的位置s.replace(pos, len, t)
:将pos开始的len个字符替换为t
-
类型转换
stoi(s)
/stol(s)
/stoll(s)
:字符串转int/long/long longstof(s)
/stod(s)
:字符串转float/doubleto_string(x)
:将任意数字类型x转为string
2.示例代码
#include <iostream>
#include <string>
using namespace std;int main() {// 字符串初始化string s1 = "Hello";string s2(3, '!'); // 创建包含3个'!'的字符串// 字符串拼接string s3 = s1 + " World" + s2;cout << "拼接结果: " << s3 << endl; // 输出: Hello World!!!// 查找子串size_t pos = s3.find("World");if (pos != string::npos) {cout << "\"World\"的位置: " << pos << endl; // 输出: 6}// 截取子串string sub = s3.substr(0, 5); // 从位置0开始取5个字符cout << "截取子串: " << sub << endl; // 输出: Hello// 替换操作s3.replace(5, 1, ","); // 将位置5的1个字符替换为','cout << "替换后: " << s3 << endl; // 输出: Hello,World!!!// 字符串转数字string numStr = "12345";int num = stoi(numStr);cout << "字符串转数字: " << num + 1 << endl; // 输出: 12346return 0;
}
二、<algorithm>
库常用函数
1.常用库函数
该库提供了通用算法,适用于vector、string等容器,是比赛中的"效率利器":
-
排序与查找
sort(begin, end)
:对[begin, end)区间升序排序(默认less<>
)sort(begin, end, cmp)
:使用自定义比较函数cmp排序(如降序)binary_search(begin, end, val)
:二分查找val是否存在(需先排序)lower_bound(begin, end, val)
:返回首个≥val的元素迭代器upper_bound(begin, end, val)
:返回首个>val的元素迭代器
-
元素操作
swap(a, b)
:交换a和b的值(支持任意可交换类型)reverse(begin, end)
:反转区间内元素(如字符串反转)unique(begin, end)
:去除连续重复元素(需先排序,返回去重后尾迭代器)count(begin, end, val)
:统计val在区间中出现的次数min_element(begin, end)
/max_element(begin, end)
:返回区间最小/大值迭代器
-
排列与组合
next_permutation(begin, end)
:生成下一个字典序排列(用于全排列问题)prev_permutation(begin, end)
:生成上一个字典序排列
2.示例代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;// 自定义比较函数(降序)
bool cmp(int a, int b) {return a > b;
}int main() {vector<int> nums = {3, 1, 4, 1, 5, 9, 2};// 排序(默认升序)sort(nums.begin(), nums.end());cout << "升序排序: ";for (int num : nums) cout << num << " "; // 输出: 1 1 2 3 4 5 9cout << endl;// 自定义排序(降序)sort(nums.begin(), nums.end(), cmp);cout << "降序排序: ";for (int num : nums) cout << num << " "; // 输出: 9 5 4 3 2 1 1cout << endl;// 查找元素int target = 3;bool exists = binary_search(nums.begin(), nums.end(), target, cmp);cout << "是否存在" << target << "?: " << (exists ? "是" : "否") << endl; // 输出: 是// 反转元素reverse(nums.begin(), nums.end());cout << "反转后: ";for (int num : nums) cout << num << " "; // 输出: 1 1 2 3 4 5 9cout << endl;// 统计元素出现次数int cnt = count(nums.begin(), nums.end(), 1);cout << "数字1出现的次数: " << cnt << endl; // 输出: 2return 0;
}
三、<cmath>
库常用函数
1.常用库函数
数学计算必备库,包含各类常用数学函数:
-
基础运算
abs(x)
:返回x的绝对值(x为int/long等整数类型)fabs(x)
:返回x的绝对值(x为double/float等浮点类型)pow(x, y)
:计算x的y次方(x^y)sqrt(x)
:计算x的平方根(x≥0)cbrt(x)
:计算x的立方根
-
取整函数
ceil(x)
:向上取整(如ceil(2.3)=3.0)floor(x)
:向下取整(如floor(2.7)=2.0)round(x)
:四舍五入取整(如round(2.5)=3.0)
-
三角函数(弧度制)
sin(x)
/cos(x)
/tan(x)
:正弦/余弦/正切asin(x)
/acos(x)
/atan(x)
:反正弦/反余弦/反正切
-
其他常用
log(x)
:自然对数(以e为底)log10(x)
:常用对数(以10为底)exp(x)
:计算e的x次方(e^x)hypot(x, y)
:计算√(x² + y²)(直角三角形斜边长度)
2.示例代码
#include <iostream>
#include <cmath>
#include <iomanip> // 用于设置输出精度
using namespace std;int main() {double x = 2.5, y = -3.7;// 绝对值cout << "|" << y << "| = " << fabs(y) << endl; // 输出: 3.7// 幂运算与开方cout << x << "的平方: " << pow(x, 2) << endl; // 输出: 6.25cout << "16的平方根: " << sqrt(16) << endl; // 输出: 4// 取整函数cout << "向上取整(" << x << "): " << ceil(x) << endl; // 输出: 3cout << "向下取整(" << x << "): " << floor(x) << endl; // 输出: 2cout << "四舍五入(" << x << "): " << round(x) << endl; // 输出: 2// 三角函数(弧度制)double pi = acos(-1); // 获取π值cout << "sin(π/2) = " << sin(pi/2) << endl; // 输出: 1cout << "cos(π) = " << cos(pi) << endl; // 输出: -1// 对数运算cout << "自然对数ln(e) = " << log(exp(1)) << endl; // 输出: 1cout << "常用对数log10(100) = " << log10(100) << endl; // 输出: 2return 0;
}