📅  最后修改于: 2023-12-03 15:35:09.356000             🧑  作者: Mango
在C++中,std::string::find_last_of
是一个字符串操作函数,用于查找字符串中最后一个与给定字符序列中任何一个字符匹配的字符位置。
size_t find_last_of(const char* str, size_t pos = npos) const noexcept;
size_t find_last_of(const char* str, size_t pos, size_t n) const noexcept;
size_t find_last_of(char c, size_t pos = npos) const noexcept;
str
:要查找的字符序列。pos
:查找的起始位置,如果未指定默认为字符串的末尾。c
:要查找的字符。如果字符被找到,则该函数返回该字符在字符串中的位置,否则返回std::string::npos
。
#include <iostream>
#include <string>
int main() {
std::string str = "hello world";
size_t last_space = str.find_last_of(" ");
std::cout << "Last space is at position " << last_space << '\n';
return 0;
}
输出:
Last space is at position 5
#include <iostream>
#include <string>
int main() {
std::string str = "hello world";
size_t last_char = str.find_last_of("ldr");
std::cout << "Last 'l', 'd' or 'r' is at position " << last_char << '\n';
return 0;
}
输出:
Last 'l', 'd' or 'r' is at position 9
#include <iostream>
#include <string>
int main() {
std::string str = "hello world";
size_t last_l = str.find_last_of('l');
std::cout << "Last 'l' is at position " << last_l << '\n';
return 0;
}
输出:
Last 'l' is at position 9
std::string::npos
是 size_t
类型的常量,表示未找到匹配字符的情况。 find_last_of
函数。