C++,leecode字符串常见API

发布于:2025-03-05 ⋅ 阅读:(125) ⋅ 点赞:(0)

在LeetCode上刷C++题目时,熟练掌握字符串相关的常见API可以大大提高代码效率和可读性。以下是C++标准库(<string>)中常用的字符串操作API:


1. 初始化和赋值

std::string s1 = "hello";  // 直接初始化
std::string s2("world");   // 另一种初始化方式
std::string s3 = s1;       // 拷贝初始化
std::string s4(5, 'a');    // 生成 "aaaaa"
s4 = "new";                // 赋值

2. 拼接(++=

std::string s1 = "hello";
std::string s2 = " world";
std::string s3 = s1 + s2;  // "hello world"
s1 += s2;                  // "hello world"

3. 字符串长度

std::string s = "hello";
std::cout << s.size();   // 5
std::cout << s.length(); // 5

4. 访问字符

std::string s = "hello";
std::cout << s[1];       // 'e'
std::cout << s.at(1);    // 'e',带边界检查
s[0] = 'H';              // 修改字符串

5. 子串(substr()

std::string s = "abcdef";
std::string sub1 = s.substr(2, 3);  // "cde"
std::string sub2 = s.substr(4);     // "ef"

6. 查找(find()rfind()

std::string s = "hello world";
size_t pos = s.find("world");  // 返回索引 6
if (pos != std::string::npos) {
    std::cout << "Found at: " << pos << std::endl;
}

size_t pos2 = s.find("abc");  // 没找到,返回 std::string::npos
std::cout << pos2 << std::endl;  // 4294967295(无符号整数最大值)

size_t rpos = s.rfind("o");  // 逆向查找 'o',返回 7

7. 查找字符(find_first_of()find_last_of()

std::string s = "abcdefg";
size_t pos = s.find_first_of("cd"); // 2,第一个匹配 'c'
size_t pos2 = s.find_last_of("cd"); // 3,最后一个匹配 'd'

8. 替换(replace()

std::string s = "hello world";
s.replace(6, 5, "C++"); // 替换 "world" 为 "C++",结果 "hello C++"

9. 插入(insert()

std::string s = "hello";
s.insert(5, " world"); // "hello world"

10. 删除(erase()

std::string s = "hello world";
s.erase(5, 6);  // 删除 " world",变成 "hello"

11. 分割字符串(find() + substr()

std::string s = "apple,banana,orange";
std::vector<std::string> result;
size_t start = 0, end;
while ((end = s.find(',', start)) != std::string::npos) {
    result.push_back(s.substr(start, end - start));
    start = end + 1;
}
result.push_back(s.substr(start)); // 最后一个部分

for (const auto& str : result) {
    std::cout << str << std::endl;
}
// 输出:
// apple
// banana
// orange

12. 大小写转换

#include <cctype>

std::string s = "Hello";
for (char& c : s) c = std::tolower(c); // 转小写 "hello"

for (char& c : s) c = std::toupper(c); // 转大写 "HELLO"

13. 字符串转换

#include <sstream>

// 数字转字符串
std::string str = std::to_string(1234);  // "1234"

// 字符串转数字
int num = std::stoi("123");      // 123
double d = std::stod("12.34");   // 12.34
long long ll = std::stoll("9876543210"); // 9876543210

14. 去除首尾空格(erase() + find_first_not_of() + find_last_not_of()

std::string trim(std::string s) {
    size_t start = s.find_first_not_of(" \t\n\r");
    if (start == std::string::npos) return ""; // 只有空格的情况
    size_t end = s.find_last_not_of(" \t\n\r");
    return s.substr(start, end - start + 1);
}

std::string s = "   hello world   ";
std::cout << "[" << trim(s) << "]"; // [hello world]

15. 判断前缀和后缀

#include <string_view>

bool startsWith(const std::string& s, const std::string& prefix) {
    return s.compare(0, prefix.size(), prefix) == 0;
}

bool endsWith(const std::string& s, const std::string& suffix) {
    return s.size() >= suffix.size() &&
           s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0;
}

std::string s = "hello world";
std::cout << std::boolalpha << startsWith(s, "hello"); // true
std::cout << std::boolalpha << endsWith(s, "world");   // true

这些API在LeetCode刷题过程中非常常用,尤其是在处理字符串匹配、分割、转换等任务时。如果有更具体的需求或LeetCode题目,可以提供具体问题,我可以帮你优化代码! 🚀


网站公告

今日签到

点亮在社区的每一天
去签到