Given this example:
std::vector<int> numbers = {5,6,7}; //size is 3 int i = -1; std::cout << i % 3 <<"\n"; // output: -1 std::cout << i % numbers.size() << "\n"; // output: 0
basically in both statements im processes -1% 3, but the compiler prints different numbers. I donโt understand this result, maybe someone can explain it to me.
edit: like @Chris, @Keith Thompson @ AnT suggested snippet
std::cout << std::numeric_limits<std::size_t>::max() % 3 <<"\n";
displays the expected result. Thanks for all the helpful tips!
source share