I have a particular use case where I have to initialize i value in for loop to -1 and write the exiting condition based on vector size. But the problem is the loop is not getting executed at all. This is the code.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> vec = { 1, 2, 3, 4, 5 };
for(int i=-1;i<vec.size();i++) cout << i << " ";
return 0;
}
But I am able to do like this.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> vec = { 1, 2, 3, 4, 5 };
int size = vec.size();
for(int i=-1;i<size;i++) cout << i << " ";
return 0;
}
Can anyone explain this weird behavior or am I doing something wrong there?