Vector subscript out of range error
Why am i getting this error message.
1 2 3 4 5
|
for (int i = 0; i < a.size(); i++){
if (a[i] == y)
a.erase(a.begin()+i);
cout << a[i] << " ";
}
|
What if that is preceeded by:
1 2 3
|
int y { 42 };
vector<int> a { y };
|
y==42
a.size()==1
a[0]==42==y |
The loop will do one round.
The first and only element of the vector is erased.
==>
There is no a[0]. out-of-range
Topic archived. No new replies allowed.