#include <string>
#include <iostream>
using namespace std;
int main()
{
string haystack = "aaa";
string needle = "aaaa";
int i = 0;
if(i <= haystack.size() - needle.size())
cout<<"why 0<-1?"<<endl;
return 0;
}输出:为什么0<-1?按任意键继续...
为什么我们会得到这样的输出?我认为"if“语句不应该被执行,因为0大于3-4=-1。谢谢!
发布于 2016-01-23 02:05:10
std::string::size的返回类型为std::size_t。
这是一个无符号类型。因此,由于您的减法结果为负值,因此基于two's complement的it will wrap around将导致一个非常大的数字,远远大于0。
https://stackoverflow.com/questions/34953152
复制相似问题