# vector中使用erase操作删除迭代器的问题（避免野指针）以及end的地址变化

17 篇文章 2 订阅

### vector的erase

vector::erase - C++ Reference http://www.cplusplus.com/reference/vector/vector/erase/

//c++98
iterator erase (iterator position);
iterator erase (iterator first, iterator last);

//c++11
iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);

Erase elements
Removes from the vector either a single element (position) or a range of elements ([first,last)).
This effectively reduces the container size by the number of elements removed, which are destroyed.

position: 在vector中删除的元素的位置
first, last: 在vector中的范围[first, last)，即 >= first, < last

demo

// erasing from vector
#include <iostream>
#include <vector>
using namespace std;

int main ()
{
vector<int> myvector;

// set some values (from 1 to 10)
for (int i=1; i<=10; i++){
myvector.push_back(i);
}

// erase the 6th element
myvector.erase (myvector.begin()+5);

// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);

cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); ++i){
cout << ' ' << myvector[i];
}

return 0;
}

### 对iterator使用erase

#include <iostream>
#include <vector>
using namespace std;

int main(){
vector<int> vecInt;

//vecInt: 0, 1, 2, 3, 4, 444
for(int i = 0; i != 5; i++){
vecInt.push_back(i);
}
vecInt.push_back(444);

//erase the element 444
for(vector<int>::iterator iter = vecInt.begin(); iter != vecInt.end(); iter++){
if(*iter == 444){
vecInt.erase(iter);
}
}

for(int i : vecInt){//c++11
cout << i << " ";
}

return 0;
}

#include <iostream>
#include <vector>
using namespace std;

int main(){
vector<int> vecInt;

//vecInt: 0, 1, 2, 3, 4, 444
for(int i = 0; i != 5; i++){
vecInt.push_back(i);
}
vecInt.push_back(444);

for(int i : vecInt){//c++11
cout << i << " ";
}
//vecSize should be 6
cout << endl << "size of vector: " << vecInt.size() << endl;

//erase the element 444
for(vector<int>::iterator iter = vecInt.begin(); iter != vecInt.end(); iter++){
if(*iter == 444){
iter = vecInt.erase(iter);
iter--;
}
}

for(int i : vecInt){//c++11
cout << i << " ";
}
//vecSize should be 5
cout << endl << "size of vector: " << vecInt.size() << endl;

return 0;
}

### vector中erase时，iterator的地址变化

Demo

#include <iostream>
#include <vector>
using namespace std;

int main(){
vector<int> vecInt;
//vecInt: 10, 11, 12, 13, 14
for(int i = 10; i != 15; i++){
vecInt.push_back(i);
}

//show all elements
cout << "show all elements: " << endl;
for(int i : vecInt){
cout << i << " ";
}
cout << "\n===============================\n" << endl;

cout << "address of vecInt: " << endl
<< "begin: \t" << &(*vecInt.begin()) << ", value: " << *vecInt.begin() << endl	//使用&*iter来读取iter的地址
<< "end: \t" << &(*vecInt.end()) << endl;
cout << "\n===============================\n" << endl;

//address of iter and end with erase
vector<int>::iterator iter = vecInt.begin();
while(iter != vecInt.end()){
iter = vecInt.erase(iter);
cout << "add of iter: " << &*iter << ", add of end: " << &*vecInt.end() << endl;
}

return 0;
}

（参考文档：如何获取vector的一个迭代器iterator的地址 - Zero’s Zone - CSDN博客 https://blog.csdn.net/hl_zzl/article/details/84575713

• 14
点赞
• 24
收藏
觉得还不错? 一键收藏
• 6
评论
06-16 1485
07-26 1188
10-17 7613
02-14 334
11-27 7923
11-27 1247
08-30 280
12-26 2811
11-25 476
09-15 3267

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。