记录cpp相关的字符串操作
#include <string>
C++中的std::string
可以视为std::vector<char>
类型的向量,因此可以用.size()
函数返回字符串的字符长度(不含'\n'结尾符号)。
以下是string
相关的常见操作
- 反转
- 字符串相加
- 转为int型:利用
stoi
将字符串转为整型(https://www.geeksforgeeks.org/stdstoi-function-in-cpp/)
- 同float
- C++ 字符串split
int idx = str.find(' ')
如有多个字符该如何,返回第一次出现的位置吗
str.substr(idx, length)
起始座标+子串长度
字符子串
用到的函数
#include <string>
idx = str.find_last_of
str.substr(strIdx, length)
旧电脑上应该保存一些code snippet