find() 会返回-1吗
find() 能返回-1吗?我搜索到的标准答案是:在使用string 类的find 成员函数来检索主串中是否含有指定的子串
find() 能返回-1吗?
我搜索到的标准答案是:在使用string 类的find 成员函数来检索主串中是否含有指定的子串时,若在主串中不含指定的子串,find 函数的返回值是(-1)
但是我测试的结果为什么返回了“4294967295”?这是个空地址吗?怎样返回-1?
#include <iostream>
#include <string>
using namespace std;
void main()
{
string str("abcd");
cout<<str.find("e");
}
[解决办法]str.find("e")返回值是string::size_type。因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别.
找不时,返回值是string::npos
npos是这样定义的:
static const size_type npos = -1;
但输出是做无符号整数型别输出,即-1的补码4294967295
[解决办法]cout<<(int)str.find("e");