find_first_of()
时间: 2023-08-17 20:09:08 浏览: 42
`find_first_of()` 是一个 C++ 字符串函数,用于在一个字符串中查找指定字符序列中的任何一个字符的第一个匹配位置。它的声明如下:
```cpp
size_t find_first_of(const string& str, size_t pos = 0) const noexcept;
```
其中,`str` 是要查找的字符序列,`pos` 是开始查找的位置,默认为 0。这个函数返回找到的第一个匹配字符的索引位置,如果没有找到匹配字符,则返回 `string::npos`。
例如,以下代码演示了如何使用 `find_first_of()` 函数:
```cpp
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
std::string chars = "aeiou";
size_t found = str.find_first_of(chars);
if (found != std::string::npos) {
std::cout << "First vowel found at position: " << found << std::endl;
} else {
std::cout << "No vowel found in the string." << std::endl;
}
return 0;
}
```
输出结果为:
```
First vowel found at position: 1
```
这里 `find_first_of()` 函数在字符串 `str` 中查找字符序列 "aeiou" 中的任何一个字符,找到第一个匹配字符 'e' 的位置是 1。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)