if (currIndex < 0) {
cout << currIndex << " % " << array.size() << endl;
currIndex = currIndex % array.size();
cout << currIndex << endl;
}
Output:
-1 % 3
0
-1 % 3 = -1 in C++ so why is 0 being returned?
Full Snippet: https://ideone.com/leWqhi
size() returns unsigned integer type, so this computation is done by casting -1 to unsigned and performing unsigned modulo.
Cast size() to signed integer type to get correct result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.