John Smith
John Smith

Reputation: 27

Erasing from a pointer to a vector of pointers

For the following code:

vector<int*> x;
vector<int*>* p;

// say I initiated x with a couple of integers
p  = &x;

//erases the indicie of the given integer
void erase(vector<int*> &x, int n){
  int i = 0;
  while (*(x[i]) != n){
    i++;
  }
  delete x[i];
  x.erase(x.begin() + i);
}

If I call the code erase(*p, 2); I want to now set p to this address of this vector that has been erased ... I'm trying p = &(*p); .. but that doesn't work and I get a segmentation fault, any ideas?

Upvotes: 0

Views: 96

Answers (1)

John Kugelman
John Kugelman

Reputation: 361565

You shouldn't have to do anything. p still points to &x just as it did before you called erase(). Removing an element from a vector doesn't change the address of the vector.

Upvotes: 2

Related Questions