刷题笔记

111 阅读5分钟

字符串

getline

输出带空格的字符串 getline

getline(cin,YourStringName);

size_t

size_t 是一种无符号整数类型,用于表示大小、计数或索引,通常用于数组、字符串长度等。它确保足够大,避免负数错误。常见于 sizeof 操作符和字符串操作。

例如:

size_t start = s.find_first_not_of('0');

size_t 用来存储字符串中第一个非 '0' 字符的位置。

find

1. find

用于查找子字符串或字符在字符串中首次出现的位置。

std::string s = "hello world";
size_t pos = s.find('o');  // 返回 'o' 第一次出现的位置

2. find_first_of

查找字符串中任意字符第一次出现的位置。与 find_first_not_of 相对,用于查找是否包含某些字符。

std::string s = "hello world";
size_t pos = s.find_first_of("aeiou");  // 查找第一个元音字母的位置

3. find_last_of

查找字符串中任意字符最后一次出现的位置。类似于 find_first_of,但是从字符串的末尾开始搜索。

std::string s = "hello world";
size_t pos = s.find_last_of("aeiou");  // 查找最后一个元音字母的位置

4. find_first_not_of

查找字符串中第一个不属于指定字符集的字符的位置。用于判断字符串中第一个不符合条件的字符。

示例

std::string s = "000123";
size_t pos = s.find_first_not_of('0');  // 返回 '1' 的位置

5. find_last_not_of

查找字符串中最后一个不属于指定字符集的字符的位置。类似于 find_first_not_of,但是从字符串的末尾开始搜索。


### getline

输出带空格的字符串  getline

```cpp
getline(cin,YourStringName);

size_t

size_t 是一种无符号整数类型,用于表示大小、计数或索引,通常用于数组、字符串长度等。它确保足够大,避免负数错误。常见于 sizeof 操作符和字符串操作。

例如:

size_t start = s.find_first_not_of('0');

size_t 用来存储字符串中第一个非 '0' 字符的位置。

find

1. find

用于查找子字符串或字符在字符串中首次出现的位置。

std::string s = "hello world";
size_t pos = s.find('o');  // 返回 'o' 第一次出现的位置

2. find_first_of

查找字符串中任意字符第一次出现的位置。与 find_first_not_of 相对,用于查找是否包含某些字符。

std::string s = "hello world";
size_t pos = s.find_first_of("aeiou");  // 查找第一个元音字母的位置

3. find_last_of

查找字符串中任意字符最后一次出现的位置。类似于 find_first_of,但是从字符串的末尾开始搜索。

std::string s = "hello world";
size_t pos = s.find_last_of("aeiou");  // 查找最后一个元音字母的位置

4. find_first_not_of

查找字符串中第一个不属于指定字符集的字符的位置。用于判断字符串中第一个不符合条件的字符。

示例

std::string s = "000123";
size_t pos = s.find_first_not_of('0');  // 返回 '1' 的位置

5. find_last_not_of

查找字符串中最后一个不属于指定字符集的字符的位置。类似于 find_first_not_of,但是从字符串的末尾开始搜索。

std::string s = "000123000";
size_t pos = s.find_last_not_of('0');  // 返回 '3' 的位置

6. rfind

查找子字符串或字符在字符串中最后一次出现的位置。

std::string s = "hello world hello";
size_t pos = s.rfind("hello");  // 返回第二次 "hello" 出现的位置

stoi,stol,stof

这些是 C++ 标准库中用于将字符串转换为其他数据类型的函数。stoi 将字符串转换为整数,stof 将字符串转换为浮点数,stol 将字符串转换为长整型。

用法:

#include <iostream>
#include <string>


int main() {
    std::string str = "123";
    int num = std::stoi(str);  // 将字符串转换为整数
    std::cout << "Converted number: " << num << std::endl;
    return 0;
}
  • stoistolstof 等函数用于从字符串中提取整数、长整型或浮点数等类型。如果字符串无法转换为目标类型,会抛出 std::invalid_argumentstd::out_of_range 异常。

to_string

to_string 是 C++11 引入的一个函数,用于将数值类型(如 int, double 等)转换为字符串。

用法:

cpp


复制代码
#include <iostream>
#include <string>


int main() {
    int num = 42;
    std::string str = std::to_string(num);  // 将整数转换为字符串
    std::cout << "String representation: " << str << std::endl;
    return 0;
}
  • to_string 将整数、浮点数等基本数据类型转换为字符串。

stoi,stol,stoll,stoul,stoull,stof,stod,to_string组合使用

stoi 和其他类似函数不仅可以用来将字符串转换为数值,还可以根据字符串中的位置获取剩余的部分。

用法:

#include <iostream>
#include <string>


int main() {
    std::string str = "123 abc";
    int num = std::stoi(str);  // 将字符串转换为整数
    std::string rest = str.substr(str.find_first_of(" ") + 1);  // 获取空格后的部分
    std::cout << "Number: " << num << ", Rest: " << rest << std::endl;
    return 0;
}

sort

sort<algorithm> 库中的一个函数,用于对容器(如 vector, array, deque 等)中的元素进行排序。

用法:

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


int main() {
    std::vector<int> vec = {5, 3, 8, 1, 2};
    std::sort(vec.begin(), vec.end());  // 对 vector 中的元素进行排序
    for (int v : vec) {
        std::cout << v << " ";
    }
    std::cout << std::endl;
    return 0;
}
  • sort 会默认按照升序对容器中的元素进行排序。如果需要降序排序,可以使用 std::greater<>() 比较器。

reverse

reverse<algorithm> 库中的另一个函数,用于反转容器中的元素顺序。

用法:

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


int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    std::reverse(vec.begin(), vec.end());  // 反转 vector 中的元素
    for (int v : vec) {
        std::cout << v << " ";
    }
    std::cout << std::endl;
    return 0;
}

accumulate

accumulate<numeric> 库中的一个函数,用于对容器中的元素进行累加(或其他操作)。

用法:

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


int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    int sum = std::accumulate(vec.begin(), vec.end(), 0);  // 计算 vec 中所有元素的和
    std::cout << "Sum: " << sum << std::endl;
    return 0;
}
  • accumulate 接受容器的开始迭代器、结束迭代器和一个初始值,然后对容器中的元素进行累加。

std::string s = "000123000"; size_t pos = s.find_last_not_of('0'); // 返回 '3' 的位置

#### 6. `rfind`

查找子字符串或字符在字符串中最后一次出现的位置。


```cpp
std::string s = "hello world hello";
size_t pos = s.rfind("hello");  // 返回第二次 "hello" 出现的位置