C++ string find_first_of C++查找任意字符集合位置【函数】

7次阅读

find_first_of查不到字符时返回std::String::npos,即无符号极大值(如18446744073709551615),不可与-1直接比较,须用== std::string::npos判断。

C++ string find_first_of C++查找任意字符集合位置【函数】

find_first_of 查不到字符时返回什么

find_first_of 找不到匹配字符时,返回 std::string::npos —— 这是个无符号整型常量,值为 static_cast(-1)。它不是 -1,而是极大值(比如 18446744073709551615)。直接跟 -1 比较会隐式转换出错,必须用 == std::string::npos 判断。

常见错误写法:if (pos == -1)if (pos —— 因为 size_t 是无符号类型,pos 永远为假,编译器可能报警告。

find_first_of 和 find_first_not_of 的区别

find_first_of字符串中找「任意一个」出现在给定字符集合里的字符;而 find_first_not_of 找的是「第一个不在集合里」的字符。

  • 对字符串 "abc123" 调用 s.find_first_of("0123456789") → 返回位置 3(’1’)
  • 同样字符串调用 s.find_first_not_of("0123456789") → 返回位置 0(’a’)
  • 如果字符串全是数字,比如 "123"find_first_not_of("0123456789") 就返回 npos

传入 C 风格字符串或 string 对象都行,但注意空字符处理

find_first_of 有多个重载:支持 const char*std::string、甚至带长度的 const char*, size_t。但传 const char* 时,遇到首个 '' 就停止解析 —— 这是 C 字符串规则,不是函数 bug

立即学习C++免费学习笔记(深入)”;

比如:s.find_first_of("a") 实际只查 '',因为 "a" 被当作空字符串看待;想查 '''a' 两个字符,得用 std::string{"a", 2} 或手动指定长度:

std::string s = "helloworld"; size_t pos = s.find_first_of(std::string{"a", 2}); // 正确:显式长度为 2

性能影响:内部是 O(M×N) 暴力扫描,别在热路径反复调用

find_first_of 不做预处理,每次调用都从头遍历目标字符串,对每个字符再遍历查找集。如果查找集合固定(比如“所有空白符”),建议提前建 std::unordered_set 或用 std::any_of + Lambda 自定义逻辑,避免重复构造临时 string隐式转换

尤其注意:传入 std::string 字面量如 s.find_first_of(" tnrfv"),每次都会构造临时 string 对象 —— 在循环里调用就是隐形性能坑。

更轻量的替代写法(c++17 起):

auto is_ws = [](char c) { return std::isspace(static_cast(c)); }; auto it = std::find_if(s.begin(), s.end(), is_ws);

真正容易被忽略的是:当查找集合含 '' 或非 ASCII 字符时,find_first_of 仍按字节比较,不考虑编码;若字符串是 UTF-8,它无法识别多字节字符边界,结果可能不符合语义预期。

text=ZqhQzanResources