I'm new to C++ and I noticed some inconsistency while working with the size()
method.
Why in the example code below I have 2 different results, one when the size is parsed and other when don't. I'm missing something?
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> arr = { 2, 3, 1, -4, -4, 2 };
cout<< arr.size() << endl; // 6
cout<< (-1 % arr.size()) << endl; // 3, incorrect without parsing
cout<< (-1 % 6) << endl; // -1
cout<< (-1 % (int)arr.size()) << endl; // -1 UPDATE, correct answer onlye when parsed
}
Update:
Yes, I do have a typo in the comment the correct value for the last answer is -1
again, it's different than the second output 3
.
I actually noticed the issue, while doing a code challenge. The solution in the code example was parsed, so I was curious "If I don't parse it will I have a different result" and In fact I did. So I open a online editor to test this beahivor. This is the online code