string.find()函数没有找到对应字符串就返回string::npos,它一个是32无符号整数的最大值,即全是32位二进制全是1。
对应赋值给有符号整数int型的变量就是“-1”。 所以有 " -1 == string::npos"
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int N = 1010;
typedef long long LL;
int main()
{
string s = "bc";
int i = s.find('a');
size_t j = s.find('a');
size_t k = string::npos;
cout << "int i = s.find('a'), i = " << i << endl;
cout << "size_t j = s.find('a'); j = " << j << endl;
cout << "size_t k = string::npos; k = " << k << endl;
cout << "32位无符号整数最大值 = " << (LL)pow(2, 32) - 1 << endl;
if (i == j && j == k && -1 == string::npos)
{
cout << "true" << endl;
}
return 0;
}
程序运行结果