c++stringfind
时间: 2023-12-07 11:28:01 浏览: 35
在C++中,使用string类的find()函数可以查找字符串中的子串。该函数的语法是:
int find(const string& str, size_t pos = 0) const;
其中,str是要查找的子串,pos是查找的起始位置,默认为0。
该函数会返回子串在字符串中的第一个字符的位置,如果找不到则返回string::npos,即-1或4294967295。
例如,在字符串str中查找子串"a",可以使用str.find("a")。如果找到了,则返回子串的起始位置,否则返回string::npos。
相关问题
c++string find
C++中的string类提供了find函数用于查找子字符串在原字符串中的位置。该函数的语法如下:
```c++
size_t find(const string& str, size_t pos = 0) const noexcept;
```
其中,str为要查找的子字符串,pos为查找的起始位置,默认值为0。如果找到了子字符串,则返回其在原字符串中的位置,否则返回string::npos。
例如,下面的代码演示了如何使用find函数查找子字符串在原字符串中的位置:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "hello world";
string sub_str = "world";
size_t pos = str.find(sub_str);
if (pos != string::npos) {
cout << "sub_str found at position " << pos << endl;
} else {
cout << "sub_str not found" << endl;
}
return 0;
}
```
输出结果为:
```
sub_str found at position 6
```
C++string find
C++中的字符串查找函数string find()用于在一个字符串中查找指定的子字符串,并返回子字符串的起始位置。如果找到了子字符串,返回子字符串的起始位置;如果没有找到,返回一个特殊值npos。
这个函数有两种常见的用法。第一种用法是不指定起始位置,直接在整个字符串中查找子字符串。例如,在字符串"abcabc"中查找子字符串"ab",可以使用以下代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "abcabc";
int pos = str.find("ab");
if (pos != str.npos) {
cout << pos << endl; // 运行结果是0
} else {
cout << "not find!";
}
cout << endl;
return 0;
}
```
第二种用法是指定起始位置,在指定的位置之后开始查找子字符串。例如,在字符串"aabc"中从下标为1的位置开始查找"abc",可以使用以下代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "aabc";
int pos = str.find("abc", 1);
if (pos != str.npos) {
cout << pos << endl; // 运行结果是1
} else {
cout << "not find!";
}
cout << endl;
return 0;
}
```
如果找不到子字符串,find()函数返回npos的特殊值,即4294967295(无符号32位整数的最大值)。例如,在字符串"aabc"中查找"d"时,可以使用以下代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "aabc";
int pos = str.find("d");
if (pos != str.npos) {
cout << pos << endl;
} else {
cout << "not find!"; // 运行结果是not find!
}
cout << endl;
return 0;
}
```
以上就是C++中的字符串查找函数string find()的用法和示例。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [C++ string中的find() 函数](https://blog.csdn.net/gentleman_97/article/details/122339565)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)