std::list::erase
| (1) | |
|---|---|---|
iterator erase( iterator pos ); | (until C++11) | |
iterator erase( const_iterator pos ); | (since C++11) | |
| (2) | |
iterator erase( iterator first, iterator last ); | (until C++11) | |
iterator erase( const_iterator first, const_iterator last ); | (since C++11) |
从容器中移除指定的元素。
1%29移除pos...
2%29移除范围内的元素。[first; last)...
对擦除元素的引用和迭代器无效。其他引用和迭代器不受影响。
迭代器pos必须是有效的和可撤销的。因此end()迭代器%28有效,但不可取消引用%29不能用作pos...
迭代器first如果first==last:擦除空范围是不操作的。
参数
pos | - | iterator to the element to remove |
|---|---|---|
first, last | - | range of elements to remove |
返回值
最后删除元素之后的Iterator。如果迭代器pos引用最后一个元素,end()返回迭代器。
例外
%280%29
复杂性
1%29常数。
2%29直线在之间的距离first和last...
例
二次
#include <list>
#include <iostream>
#include <iterator>
int main( )
{
std::list<int> c{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (auto &i : c) {
std::cout << i << " ";
}
std::cout << '\n';
c.erase(c.begin());
for (auto &i : c) {
std::cout << i << " ";
}
std::cout << '\n';
std::list<int>::iterator range_begin = c.begin();
std::list<int>::iterator range_end = c.begin();
std::advance(range_begin,2);
std::advance(range_end,5);
c.erase(range_begin, range_end);
for (auto &i : c) {
std::cout << i << " ";
}
std::cout << '\n';
}二次
产出:
二次
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 6 7 8 9二次
另见
clear | clears the contents (public member function) |
|---|
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

