I'd like to use a vector of iterators to link to the contents of a vector< int>.
When I try to fill said vector of iterators via a single iterator "it" I get a segmentation fault when I try to dereference it.
vector <int> vec;
vector <int>::iterator it = vec.begin();
vector<vector <int>::iterator> vecIt(3);
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vecIt[0] = it;
cout << *vecIt[0] << endl; //this line causes seg. fault
However, if I do not use my "it" iterator, it works just fine:
vector <int> vec;
vector<vector <int>::iterator> vecIt(3);
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
vecIt[0] = vec.begin();
cout << *vecIt[0] << endl;
Could someone explain this behavior to me? What causes the seg. fault when using an extra iterator?
Also, is there a better way of going through my vector< int> and saving iterators (or pointers, if that is more advisable) to the vector of iterators?