簡體   English   中英

從指針向指針向量中刪除

[英]Erasing from a pointer to a vector of pointers

對於以下代碼:

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);
}

如果我調用代碼erase(*p, 2); 我想現在將p設置為已被刪除的此向量的地址...我正在嘗試p = &(*p); ..但這不起作用,我得到一個分段錯誤,任何想法?

你不應該做任何事情。 p仍然指向&x就像調用erase() 從向量中刪除元素不會更改向量的地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM