遍历std::list过程中删除元素后继续遍历过程
std::list::erase
Removes from the list container 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.
Unlike other standard sequence containers, list and forward_list objects are specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence.
返回值
An iterator pointing to the element that followed the last element erased by the function call. This is the container end if the operation erased the last element in the sequence.
] = { , , , , , , , , , };
list<);
vector<int> iVec;
for(list<int>::iterator it = iList.begin();
it != iList.end(); it++) {
iVec.push_back(*it);
) {
it = iList.erase(it);
it--;
}
}
cout << "iVec: ";
for(auto& i : iVec) {
cout << i << " ";
}
cout << endl;
cout << "iList: ";
for(auto& i : iList) {
cout << i << " ";
}
cout << endl;
Output:
iVec: 2 3 6 4 1 17 4 9 25 4
iList: 2 3 6 1 17 9 25
或者:
for(list<int>::iterator it = iList.begin();
it != iList.end();) {
iVec.push_back(*it);
) {
it = iList.erase(it);
} else {
it++;
}
}
遍历std::list过程中删除元素后继续遍历过程的更多相关文章
- 遍历List过程中删除元素的正确做法(转)
遍历List过程中删除元素的正确做法 public class ListRemoveTest { 3 public static void main(String[] args) { 4 ...
- map在遍历数据的过程中删除数据不出错
// Iterator<Map.Entry<String,Long>> entries = Map.entrySet().iterator(); ...
- /编写一个函数,要求从给定的向量A中删除元素值在x到y之间的所有元素(向量要求各个元素之间不能有间断), 函数原型为int del(int A ,int n , int x , int y),其中n为输入向量的维数,返回值为删除元素后的维数
/** * @author:(LiberHome) * @date:Created in 2019/2/28 19:39 * @description: * @version:$ */ /* 编写一个 ...
- javascript在数组的循环中删除元素
在开发JavaScript应用的过程中,经常会遇到在循环中移除指定元素的需求. 按照常规的思路,就是对数组进行一个for循环,然后在循环里面进行if判断,在判断中删除掉指定元素即可. 但是实际情况往往 ...
- 遍历并remove HashMap中的元素时,遇到ConcurrentModificationException
遍历并remove HashMap中的元素时,遇到ConcurrentModificationException for (Map.Entry<ImageView, UserConcise> ...
- 如何从List中删除元素
从List中删除元素,不能通过索引的方式遍历后删除,只能使用迭代器. 错误的实现 错误的实现方法 public class Demo { public static void main(Str ...
- Java中list在循环中删除元素的坑
JAVA中循环遍历list有三种方式for循环.增强for循环(也就是常说的foreach循环).iterator遍历. 1.for循环遍历list for(int i=0;i<list.siz ...
- Jquery中删除元素方法
empty用来删除指定元素的子元素,remove用来删除元素,或者设定细化条件执行删除 语法: empty() remove(expr); empty用来删除指定元素的子元素,remove用来删除元素 ...
- [译]如何在迭代字典的过程中删除其中的某些item(Python)
最好不要在迭代的过程中删除.你可以使用解析式和filter过滤. 比方说: {key:my_dict[key] for key in my_dict if key !="deleted&qu ...
随机推荐
- 转:Mongodb中随机的查询文档记录
简述,摘要:在实际应用场景中,几乎都会有随机获取数据记录的需求.而这个需求在Mongodb却不是很好实现,就目前而言,大致上有三种解决方案:1. 先计算出一个从0到记录总数之间的随机数,然后采用ski ...
- 给 chorme Developer Tool F12 开发者工具.加入更酷的代码着色
地址:https://github.com/mauricecruz/chrome-devtools-zerodarkmatrix-theme 默认样式 替换目录为: mac ~/Library/App ...
- 萬用表檢測MOS管好壞的簡便方法
在開發LED驅動電源時難免不會接觸到MOS管,它又是一個相當脆弱的器件.往往有時故障就是因為它罷工了.以下的一點經驗希望對大家有所幫助. 1.用黑表筆接在D極上 ,紅表筆接在S極上 , 一般有一個5 ...
- 霍布森选择效应(Hobson choice Effect)
1631年,英国剑桥商人霍布森从事马匹生意,他说,你们买我的马.租我的马,随你的便,价格都便宜.霍布森的马圈大大的.马匹多多的,然而马圈只有一个小门,高头大马出不去,能出来的都是瘦马.赖马.小马,来买 ...
- pl/sql 在一个程序块里打印日志输出到表格
declare v_number NUMBER; v_number2 NUMBER; begin execute immediate 'truncate table t2'; insert into ...
- Hadoop MapReduce中压缩技术的使用
Compression and Input Splits 当我们使用压缩数据作为MapReduce的输入时,需要确认数据的压缩格式是否支持切片? 假设HDFS中有一个未经压缩的大小为1GB的文 ...
- LU分解(2)
接着上次LU分解的讲解,这次给出使用不同的计算LU分解的方法,这种方法称为基于GaxPy的计算方法.这里需要了解lapapck中的一些函数.lapack中有一个函数名为gaxpy,所对应的矩阵计算公式 ...
- Best Time to Buy and Sell Stock——LeetCode
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 关于if语句后面的花括号
两种写法.之前我比较2.总喜欢写了if语句后 不带 花括号.总感觉这样节省空间. 最后偶然看到google推荐的 才 顿悟到 这样虽然可以 但可读性不太好. 参考:https://source.and ...
- 关于fork函数
这篇文章说得非常好.做个记录: 链接:http://coolshell.cn/articles/7965.html