我是C++的新手,从Java转向并想知道这个:
pos = result.find(remove[i]);
if (pos == string::npos)
Run Code Online (Sandbox Code Playgroud)
它是在调用字符串"superclass"吗?我很困惑,如果它正在调用类本身访问常量"npos"如果我在我的函数中声明了几个字符串变量,它如何知道它是哪个类的实例?
npos不是绑定到实例,而是绑定到类本身.它是static会员.staticJava中也有成员.
[...]
namespace std {
template<class charT, class traits = char_traits<charT>,
class Allocator = allocator<charT> >
class basic_string {
public:
//...
static const size_type npos = -1;
//...
};
Run Code Online (Sandbox Code Playgroud)
std::string是一个专业化的basic_string.