0

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?

4
  • You need to explain it better. It is completely unclear at this point. "i want to now set p to this address of this vector that has been erased" - what is that supposed to mean? No vector is erased in your code and there's no reason to do anything with p. Commented Nov 20, 2012 at 19:59
  • basically want p to be the address of this new vector, since erase took that vector and removed a certain indicie. Commented Nov 20, 2012 at 20:17
  • @JohnSmith, there is no new vector. The old vector is the only vector. It persists through the call to erase. Commented Nov 20, 2012 at 20:18
  • 1
    @JohnSmith - Consider this program program and its output. Commented Nov 20, 2012 at 20:26

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

so after i call erase (*p, 2), p will now be the address of the vector of a shorter vector. ?
p will be unchanged, and will continue to point to x. You only have one vector, there is no "shorter vector".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.