std::string::find 空字符串 返回结果不是 string::npos

string sPacket = "hello, this is a testing string!";

string sKey = "";

在 sPacket 中find sKey,返回的结果并不是 string::npos,而是0;就是说,它找到了,但位于sPacket的起始处

测试代码:

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
        string sPacket = "Hello, this is a test string!";
        string skey = "";
        size_t pos = sPacket.find(skey);
        cout << "pos:" << pos << endl;
        if(pos == string::npos)
        {
                cout << "pos is string::npos" << endl;
        }
        else
        {
                cout << "pos is not string::npos" << endl;
        }

        return 0;
}

运行结果:

pos:0
pos is not string::npos

你可能感兴趣的:(std::string::find 空字符串 返回结果不是 string::npos)