为什么字符串查找时把结果赋给一个int变量,它的值会等于-1?

string s="abcdefg";
int k=s.find("jkh");
cout<

c++的这个find,如果找不到,返回的值是npos,是个静态常量,定义如下:
static const size_type npos = -1
所以返回的就是-1

std::string::find()函数查找子串,如果找到,则返回子串在字符串中的位置,否则返回std::string::npos,其值是-1

static const size_type npos = -1;

https://en.cppreference.com/w/cpp/string/basic_string/npos
https://en.cppreference.com/w/cpp/string/basic_string/find