精确输出位数与位数不够时的补全
要在一个字符串中找到是否有与另一个字符串相同的部分,可以使用`find`函数。`find`函数可以在一个字符串中查找另一个字符串第一次出现的位置。如果返回的位置不是`string::npos`,则说明找到了相同的部分。
下面是一个示例代码,演示了如何在字符串`str1`中查找是否有与字符串`str2`相同的部分:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "Hello, World!";
string str2 = "World";
size_t found = str1.find(str2);
if (found != string::npos) {
cout << "Found match at position: " << found << endl;
} else {
cout << "No match found." << endl;
}
return 0;
}
```
在上面的示例中,如果`str2`存在于`str1`中,程序将输出匹配的位置;否则,将输出"No match found."。