문제

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