容器(container)的erase()函数
本文地址: http://blog.csdn.net/caroline_wendy/article/details/23996013
容器(container)的erase()函数, 是删除一个元素, 会移动迭代器的指针,
指向下一个元素;
迭代器(iterator)是一种指针, 赋值是传递的地址, 指向同样的元素;
代码:
/* * test.cpp * * Created on: 2014.04.18 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #include <vector> using namespace std; int main() { std::vector<int> vi = {1, 6, 6, 3}; std::vector<int>::iterator iter1; std::vector<int>::iterator iter2; //少删除了一个6 for (iter1 = vi.begin(); iter1 != vi.end(); ++iter1) { if (6 == *iter1) { iter2 = iter1; vi.erase(iter2); //--iter1; } } std::cout << "vector size is : " << vi.size() << std::endl; for (iter1 = vi.begin(); iter1 != vi.end(); ++iter1) { std::cout << *iter1 << " "; } std::cout << std::endl; return 0; }
输出:
vector size is : 3 1 6 3
C++ - 容器(container)的erase()函数,布布扣,bubuko.com
时间: 2024-12-15 06:42:58