c++中find_first_of()
时间: 2023-09-05 20:13:45 浏览: 58
在 C++ 中,`find_first_of()` 是一个字符串函数,用于查找一个字符串中的任何字符是否出现在另一个字符串中。
函数原型如下:
```c++
size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
size_t find_first_of (const char* s, size_t pos, size_t n) const;
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_first_of (char c, size_t pos = 0) const noexcept;
```
其中,第一个参数 `str` 是要查找的字符串;第二个参数 `pos` 是从哪个位置开始查找,默认值为 0。
如果找到了匹配的字符,则函数返回该字符在字符串中的位置,否则返回 `string::npos`。
示例代码:
```c++
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello World";
std::string str2 = "abcde";
size_t found = str1.find_first_of(str2);
if (found != std::string::npos) {
std::cout << "Found character " << str1[found] << " at position " << found << '\n';
} else {
std::cout << "No match found.\n";
}
return 0;
}
```
输出结果:
```
Found character l at position 2
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)