C++常用库函数

c++常用库函数

1.输入输出流库(iostream)

见:C++的输入输出(ACM模式)

2.字符串库(string)

substr

返回子字符串
substr(pos, len)
返回从pos开始len长度的子字符串
pos:子字符串的起始位置(默认为0)。
len:子字符串的长度(默认为 npos,表示到字符串末尾)。

例:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    std::string sub1 = str.substr(7, 5); // "World"
    std::string sub2 = str.substr(0, 5); // "Hello"
    std::string sub3 = str.substr(7);    // "World!"

    std::cout << sub1 << std::endl;
    std::cout << sub2 << std::endl;
    std::cout << sub3 << std::endl;

    return 0;
}

find

用于查找子字符串在当前字符串中第一次出现的位置。如果找到,返回子字符串的起始位置;如果未找到,返回 std::string::npos

size_t find(const std::string& str, size_t pos = 0) const noexcept;
str:要查找的子字符串。
pos:开始查找的位置(默认为0)

例:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    size_t pos = str.find("World");

    if (pos != std::string::npos) {
        std::cout << "Found at position: " << pos << std::endl; // 输出: Found at position: 7
    } else {
        std::cout << "Not found" << std::endl;
    }

    return 0;
}

replace

用于将字符串中的某部分替换为另一个字符串。

std::string& replace(size_t pos, size_t len, const std::string& str);
pos:替换的起始位置。
len:替换的长度。
str:要替换成的字符串。

例:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    str.replace(str.find("World"), 5, "C++"); // 替换 "World" 为 "C++"

    std::cout << str << std::endl; // 输出: Hello, C++!

    return 0;
}

stoi

将字符串转换为整数

int stoi(const std::string& str, size_t* pos = 0, int base = 10);
str:要转换的字符串。
pos:指向一个 size_t 类型的对象,存储转换后未处理的第一个字符的位置(可选)。
base:进制,默认为10。

例:

#include <iostream>
#include <string>

int main() {
    std::string str = "12345";
    int num = std::stoi(str);

    std::cout << "Number: " << num << std::endl; // 输出: Number: 12345

    return 0;
}

stod

将字符串转换为双精度浮点数

double stod(const std::string& str, size_t* pos = 0);
str:要转换的字符串。
pos:指向一个 size_t 类型的对象,存储转换后未处理的第一个字符的位置(可选)。

例:

#include <iostream>
#include <string>

int main() {
    std::string str = "123.45";
    double num = std::stod(str);

    std::cout << "Number: " << num << std::endl; // 输出: Number: 123.45

    return 0;
}

3.容器库(container)

见:C++的STL简介

4.算法库(algorithm)

sort

排序

 - sort(数组首指针,数组尾指针)//默认从小到大排列
 - sort(数组首指针,数组尾指针, greater<int>()) //从大到小排序
 - sort(数组首指针,数组尾指针, less<int>()) //从小到大排序

例:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {4, 2, 5, 1, 3};
    
    // 默认升序排序
    std::sort(vec.begin(), vec.end());
    
    // 输出排序后的结果
    for (int val : vec) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    // 自定义降序排序
    std::sort(vec.begin(), vec.end(), std::greater<int>());

    // 输出排序后的结果
    for (int val : vec) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

reverse

反转

reverse(数组首指针,数组尾指针)

例:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    
    // 反转元素顺序
    std::reverse(vec.begin(), vec.end());

    // 输出反转后的结果
    for (int val : vec) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

find

查找:用于在容器中查找第一个匹配的元素,返回一个指向该元素的迭代器。

find(数组首指针,数组尾指针,待查找元素)

例:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    
    // 查找元素 3
    auto it = std::find(vec.begin(), vec.end(), 3);

    if (it != vec.end()) {
        std::cout << "Element found: " << *it << std::endl;
    } else {
        std::cout << "Element not found" << std::endl;
    }

    return 0;
}

copy

复制:用于将一个范围内的元素复制到另一个范围。

copy(vec1.begin(), vec1.end(), vec2.begin());//将vec1的元素复制到vec2

例:

#include <iostream>
#include <vector>
#include <algorithm>
int main() {
    std::vector<int> vec1 = {1, 2, 3, 4, 5};
    std::vector<int> vec2(5);
    
    // 将 vec1 的元素复制到 vec2
    std::copy(vec1.begin(), vec1.end(), vec2.begin());

    // 输出 vec2 的结果
    for (int val : vec2) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

transform

变换: 用于对范围内的元素应用一个操作,并将结果存储到另一个范围。

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec1 = {1, 2, 3, 4, 5};
    std::vector<int> vec2(5);
    
    // 将 vec1 的元素乘以 2 后存储到 vec2
    std::transform(vec1.begin(), vec1.end(), vec2.begin(), [](int val) { return val * 2; });

    // 输出 vec2 的结果
    for (int val : vec2) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

accumulate

累加:用于计算范围内所有元素的累加值。

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    
    // 计算累加值,初始值为 0
    int sum = std::accumulate(vec.begin(), vec.end(), 0);

    std::cout << "Sum: " << sum << std::endl;

    return 0;
}

count

计数:用于计算范围内等于给定值的元素个数。

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5, 3, 3};
    
    // 计算值为 3 的元素个数
    int count = std::count(vec.begin(), vec.end(), 3);

    std::cout << "Number of 3s: " << count << std::endl;

    return 0;
}

5.数学库(cmath)

pow:幂运算。
sqrt:平方根。
sin:正弦。
cos:余弦。
tan:正切。
log:自然对数。
exp:指数

6.时间库(chrono)

system_clock:系统时钟。
steady_clock:稳定时钟。
high_resolution_clock:高分辨率时钟。
sleep_for:线程休眠。

7.线程库(thread)

thread:创建线程。
mutex:互斥锁。
lock_guard:锁保护。
unique_lock:独占锁。
condition_variable:条件变量。

8.文件系统库(filesystem)

exists:检查文件或目录是否存在。
copy:复制文件或目录。
remove:删除文件或目录。
rename:重命名文件或目录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值