Why does using vector.erase(vector.end())
produces a
Segmentation fault (core dumped)
when using this code:
#include <iostream>
#include <vector>
using namespace std;
void printMe(vector<int>& v){ for(auto &i:v) cout<<i<<" "; cout<<"\n"; }
int main() {
vector<int> c = { 1,2,3,4,5,6,7,8};
printMe(c);
c.erase(c.begin());
printMe(c);
c.erase(c.begin());
printMe(c);
// c.erase(c.end()); //will produce segmentation fault
// printMe(c);
return 0;
}
I'm a bit new to these iterators , so this caught me off guard. While I know there exists vector.pop_back()
. I'm curious to know what exactly causes this.
A link to the program.