1346 字
7 分钟
C++中常用库函数
2025-03-28

1. 字符串操作#

C++ 提供了强大的字符串处理功能,尤其是在 std::string 类中,提供了许多非常方便的成员函数。下面是常见的字符串操作方法。

1.1. 创建和初始化字符串

std::string str1 = "Hello"; // 使用字符常量初始化
std::string str2 = str1; // 拷贝构造
std::string str3(10, 'A'); // 创建一个长度为10,字符为 'A' 的字符串 "AAAAAAAAAA"

1.2. 获取字符串长度

std::string str = "Hello";
size_t len = str.size(); // 获取字符串长度,等价于 str.length()
std::cout << len; // 输出 5

1.3. 子字符串操作

• std::string::substr(start_index, length):返回一个子字符串,起始位置 start_index,长度 length。

std::string str = "Hello World!";
std::string sub = str.substr(6, 5); // 从第6个字符开始,取5个字符,结果是 "World"

1.4. 查找字符或子字符串

• std::string::find(substring):返回子字符串首次出现的位置,如果没有找到返回 std::string::npos。

std::string str = "Hello World!";
size_t pos = str.find("World"); // 查找 "World" 的位置,返回 6
if (pos != std::string::npos) {
std::cout << "Found at position " << pos << std::endl; // 输出: Found at position 6
}

• std::string::rfind(substring):返回子字符串最后一次出现的位置。

std::string str = "Hello World! World!";
size_t pos = str.rfind("World"); // 查找最后一个 "World" 的位置,返回 13

1.5. 替换字符或子字符串

• std::string::replace(start_index, length, new_substring):将指定位置的部分内容替换为新的子字符串。

std::string str = "Hello World!";
str.replace(6, 5, "Universe"); // 将 "World" 替换为 "Universe"
std::cout << str; // 输出: "Hello Universe!"

1.6. 拼接字符串

• 使用 + 运算符或者 std::string::append() 方法。

std::string str1 = "Hello";
std::string str2 = " World!";
std::string str3 = str1 + str2; // 拼接 "Hello" 和 " World!",结果为 "Hello World!"
str1.append(" Universe!"); // 追加 " Universe!" 到 str1
std::cout << str1; // 输出: "Hello Universe!"

1.7. 转换为 C 风格字符串

• std::string::c_str():返回 const char* 类型的 C 风格字符串。

std::string str = "Hello";
const char* cstr = str.c_str(); // 获取 C 风格字符串 "Hello"

1.8. 字符串比较

• std::string::compare():比较两个字符串,返回值:0 相等,负数小于,正数大于。

std::string str1 = "abc";
std::string str2 = "abc";
if (str1.compare(str2) == 0) {
std::cout << "Strings are equal" << std::endl;
}

2. 数学相关#

C++ 提供了大量的数学函数,很多函数都在 cmath 头文件中,以下是一些常用的数学函数和操作。

2.1. 数学基本函数

• std::abs(x):返回 x 的绝对值。

int abs_val = std::abs(-5); // 返回 5

• std::pow(base, exponent):计算 base 的 exponent 次幂。

double result = std::pow(2, 3); // 2 的 3 次方,结果为 8

• std::sqrt(x):返回 x 的平方根。

double root = std::sqrt(16); // 返回 4

• std::ceil(x):返回不小于 x 的最小整数。

double ceil_val = std::ceil(2.3); // 返回 3

• std::floor(x):返回不大于 x 的最大整数。

double floor_val = std::floor(2.7); // 返回 2

2.2. 三角函数

• std::sin(x):计算角度 x 的正弦值(弧度制)。

• std::cos(x):计算角度 x 的余弦值(弧度制)。

• std::tan(x):计算角度 x 的正切值(弧度制)。

2.3. 最大最小值

• std::max(x, y):返回 x 和 y 中的较大值。

int max_val = std::max(10, 20); // 返回 20

• std::min(x, y):返回 x 和 y 中的较小值。

int min_val = std::min(10, 20); // 返回 10

2.4. 最大公约数与最小公倍数

• std::gcd(a, b):返回 a 和 b 的最大公约数。

int gcd_val = std::gcd(24, 36); // 返回 12

• std::lcm(a, b):返回 a 和 b 的最小公倍数(C++20 新增)。

int lcm_val = std::lcm(24, 36); // 返回 72

2.5. 随机数生成

• std::rand():生成随机整数。

• std::srand(seed):设置随机数种子。

std::srand(std::time(0)); // 设置随机数种子
int random_num = std::rand(); // 获取一个随机数

3. 容器相关#

C++ 提供了丰富的标准容器类(在  等头文件中定义)。这些容器广泛应用于各种算法题和实际编程中。

3.1. 向量 std::vector

std::vector 是 C++ 中的动态数组,支持随机访问元素,能够动态扩展。

• 创建和初始化

std::vector<int> vec = {1, 2, 3, 4}; // 初始化
std::vector<int> vec2(5, 0); // 初始化长度为 5 的向量,元素为 0

• 常用方法

• vec.size():返回向量的元素个数。

• vec.push_back(x):在向量末尾添加元素 x。

• vec.pop_back():删除向量末尾的元素。

• vec[index]:访问元素。

• vec.begin() 和 vec.end():返回向量的起始和结束迭代器。

vec.push_back(10); // 向 vec 添加元素 10
std::cout << vec.size(); // 输出 5

3.2. 双端队列 std::deque

std::deque 是一个双端队列,支持从两端进行高效的插入和删除。

• 创建和初始化

std::deque<int> dq = {1, 2, 3, 4};

• 常用方法

• dq.push_front(x):将元素 x 添加到队列前端。

• dq.push_back(x):将元素 x 添加到队列后端。

• dq.pop_front():从队列前端删除元素。

• dq.pop_back():从队列后端删除元素。

3.3. 链表 std::list

std::list 是一个双向链表,适用于频繁插入和删除操作的场景。

• 创建和初始化

std::list<int> lst = {1, 2, 3, 4};

• 常用方法

• lst.push_front(x):将元素 x 添加到链表前端。

• lst.push_back(x):将元素 x 添加到链表后端。

• lst.pop_front():删除链表前端的元素。

• lst.pop_back():删除链表后端的元素。

3.4. 集合和映射 std::set 和 std::map

std::set 和 std::map 都是基于红黑树实现的有序集合和映射,元素按顺序排列,自动去重。

• 创建和初始化

std::set<int> s = {1, 2, 3, 4};
std::map<int, std::string> m = {{1, "one"}, {2, "two"}};

• 常用方法

• s.insert(x):插入元素。

• s.find(x):查找元素是否存在,返回迭代器。

• m[key] = value:向映射中插入元素。

3.5. 哈希集合和哈希映射 std::unordered_set 和 std::unordered_map

std::unordered_set 和 std::unordered_map 是基于哈希表实现的容器,提供平均常数时间复杂度的查找、插入和删除操作。


原文发布于 CSDN:C++中常用库函数

C++中常用库函数
https://www.tommywutong.cn/posts/csdn-import/csdn-146485441-c/
作者
TommyWu
发布于
2025-03-28
许可协议
CC BY-NC-SA 4.0