c++ - string::npos 是什么意思?

标签 c++

<分区>

下面的语句是什么意思?

    string s="Joe Alan Smith"";
    cout << (s.find("Allen") == string::npos) << endl; 

最佳答案

实际上string::find()返回找到的字符串的位置,但是如果没有找到给定的字符串,则返回string::npos,其中 npos 表示没有位置

npos是一个无符号整数值,标准定义为-1(有符号表示)表示没有位置。

//npos is unsigned, that is why cast is needed to make it signed!
cout << (signed int) string::npos <<endl; 

输出:

-1

参见 Ideone:http://www.ideone.com/VRHUj

关于c++ - string::npos 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5096243/

相关文章:

c++ - (C++) 删除矩阵错误中重复的行

C++异常安全

c++ - 使用 Magick++ 加载 ICC 配置文件

c++ - 起始 block 存储地址

c++ - 如何在 C++ 中仅解析、读取 .CSV 文件的一列并将其存储到数组中

C++模板 "deferred instantiation"

c++ - 如何确定 OpenGL 窗口是否为事件窗口?

c++ - 在 Visual Studio 中将字符串转换为 wstring 失败

c++ - 写入和读取随机数状态到文件

c++ - typename 语句中的 class 后跟 _t(下划线-t)代表什么?