我以前从未见过的代码行

dis*_*ney 0 c++

这是我正在浏览的一段C++代码,但我以前从未见过它.有人能告诉我这是什么意思吗?如果找到searchText,它只是将bool设置为true吗?

size_t startPos = searchString.find("searchText");
bool found = startPos != std::string::npos;
Run Code Online (Sandbox Code Playgroud)

ere*_*eOn 7

std::string::find()std::string::npos如果找不到子字符串,则返回给定字符串中搜索的子字符串的位置或(常量).

如果以这样的方式编写代码,也许你会更好地阅读代码:

size_t startPos = searchString.find("searchText");

// In the next line, '(' and ')' are not mandatory, but make this easier to read.
bool found = (startPos != std::string::npos);
Run Code Online (Sandbox Code Playgroud)

也就是说,如果startPos不同,std::string::npos则找到子串.