I don't enter the loop using size_t. Why? If I use int I enter the loop and the output is : 11, 11

int main()
{
   int j = 11; 
   for (size_t i = 11; i > - 1; --j, i -=12)
   {

       std::cout << i << ", " << j << std::endl; 

   }
   return 0;
}
有帮助吗?

解决方案

size_t is typically an unsigned value. In your for loop condition you're saying i > -1. Since i is an unsigned type the -1 is converted to this type, and will have the maximum value for that type.

Because of the promotion you're effectivly comparing 11 (the initial value of i) with the maximum value for size_t and the result is false, thus the loop is never entered.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top