C 字符串:: NPO是在调用字符串类

c++ string :: npos is it calling the string class?

本文关键字:字符串 调用 NPO      更新时间:2023-10-16

我是C 的新手,从Java移动并想知道:

        pos = result.find(remove[i]);  
        if (pos == string::npos)

它将字符串称为"超类"?如果我在调用类本身来访问常数的" npos",我很困惑它如何知道我的函数中有几个字符串变量的类别?

npos不绑定到一个实例,而是与类本身绑定。这是static成员。Java中也有static成员。

21.4类模板basic_string [basic.String]

[...]

namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_string {
  public:
    //...
    static const size_type npos = -1;
    //...
};

std::string basic_string的专业

npos是字符串的静态成员。
C 中的静态成员每个程序都创建一次,并由同一类的所有实例共享。但也可以在不进行班级的情况下访问。