一个链表list中,要删除掉其中等于某个值的节点,代码怎样写,运行效率最快?

好啊之 2014-03-28 09:39:37
例如,要删除掉所有等于1的节点。求解。





...全文
127 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
好啊之 2014-03-28
  • 打赏
  • 举报
回复
原来如此
引用 3 楼 neustar1 的回复:
看清楚是pIndex=a.erase(pIndex);函数会自动返回下一个节点的iterator [quote=引用 2 楼 u014399446 的回复:] 在遍历过程中,删除当前元素,确保不会出错?迭代器还有效? a.erase(pIndex);后,当前的pIndex,还是erase前的pIndex? [quote=引用 1 楼 neustar1 的回复:] [quote=引用 楼主 u014399446 的回复:] 例如,要删除掉所有等于1的节点。求解。
遍历一遍,删掉符合的节点呀,难道还有别的办法? list<int>::iterator pIndex = a.begin(); while(pIndex!=a.end()) { if(*pIndex==1)pIndex=a.erase(pIndex); else ++pIndex; }[/quote][/quote][/quote]
还差得远呢 2014-03-28
  • 打赏
  • 举报
回复
二楼正解啊,C++primer里有
__cc__ 2014-03-28
  • 打赏
  • 举报
回复
看清楚是pIndex=a.erase(pIndex);函数会自动返回下一个节点的iterator
引用 2 楼 u014399446 的回复:
在遍历过程中,删除当前元素,确保不会出错?迭代器还有效? a.erase(pIndex);后,当前的pIndex,还是erase前的pIndex? [quote=引用 1 楼 neustar1 的回复:] [quote=引用 楼主 u014399446 的回复:] 例如,要删除掉所有等于1的节点。求解。
遍历一遍,删掉符合的节点呀,难道还有别的办法? list<int>::iterator pIndex = a.begin(); while(pIndex!=a.end()) { if(*pIndex==1)pIndex=a.erase(pIndex); else ++pIndex; }[/quote][/quote]
好啊之 2014-03-28
  • 打赏
  • 举报
回复
在遍历过程中,删除当前元素,确保不会出错?迭代器还有效? a.erase(pIndex);后,当前的pIndex,还是erase前的pIndex?
引用 1 楼 neustar1 的回复:
[quote=引用 楼主 u014399446 的回复:] 例如,要删除掉所有等于1的节点。求解。
遍历一遍,删掉符合的节点呀,难道还有别的办法? list<int>::iterator pIndex = a.begin(); while(pIndex!=a.end()) { if(*pIndex==1)pIndex=a.erase(pIndex); else ++pIndex; }[/quote]
__cc__ 2014-03-28
  • 打赏
  • 举报
回复
引用 楼主 u014399446 的回复:
例如,要删除掉所有等于1的节点。求解。
遍历一遍,删掉符合的节点呀,难道还有别的办法? list<int>::iterator pIndex = a.begin(); while(pIndex!=a.end()) { if(*pIndex==1)pIndex=a.erase(pIndex); else ++pIndex; }

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧