下面的代碼打印計數爲0.但是s.length()是11,所以不應該計數爲10011?(C++)for循環正在被跳過
int main() {
clock_t start_time = clock();
string s = "hello world";
int count = 0;
for (int i = -10000; i < s.length(); i++) {
count++;
}
cout << count << endl;
cout << clock() - start_time;
return 0;
}
嘗試'unsigned int i'而不是'int i'。 –
@barakmanos仍然給出0 – wannabe
['std :: string :: length'](http://en.cppreference.com/w/cpp/string/basic_string/size)返回的類型是* unsigned *。您對已簽名和未簽名整數的比較可能無法按預期工作。 –