从指针擦除指向指针矢量
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()
之前一样。从矢量中删除元素不会更改矢量的地址。
相关文章:
- 引用一个已擦除类型(void*)的指针
- 擦除while循环中迭代的元素
- 在运行时处理类型擦除的数据-如何不重新发明轮子
- C++擦除(如果存在)
- 在映射擦除c++期间执行循环的次数
- 键入擦除成员函数指针的"right"方法是什么?
- 从地图擦除后访问指针
- free():实现矢量擦除()时的指针无效
- c++ std map 的擦除功能是否释放了指针键的内存?
- STL矢量通过指针擦除
- 从指针擦除指向指针矢量
- 从对象指针的 STD 列表中擦除对象
- 无法删除和擦除矢量上的对象指针
- 在元素自己的析构函数中擦除指向静态向量中元素的指针
- 如何用指针擦除矢量元素
- 传递指针向量并擦除重复项
- 从中间擦除,指针丢失
- 使用智能指针的擦除习语
- 矢量中的擦除功能不删除指针?
- 删除指针后未擦除内存内容(在一个简单的示例中)