STL矢量vs地图擦除 [英] STL vector vs map erase

查看:113
本文介绍了STL矢量vs地图擦除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在STL中,几乎所有容器都具有擦除功能。我有一个问题是在一个向量中,擦除函数返回一个迭代器指向向量中的下一个元素。映射容器不会这样做。相反,它返回一个void。任何人都知道为什么会出现这种不一致?

In the STL almost all containers have an erase function. The question I have is in a vector, the erase function returns an iterator pointing to the next element in the vector. The map container does not do this. Instead it returns a void. Anyone know why there is this inconsistancy?

推荐答案

请参阅http://www.sgi.com/tech/stl/Map.html


Map有一个重要的属性
在地图中插入一个新元素
不会使迭代器无效,
指向现有元素。从映射中擦除
元素也不会
使任何迭代器无效,除了
课程,对于实际为
的迭代器,指向正在
的元素。

Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.

在擦除时返回迭代器的原因是,您可以遍历列表擦除元素。如果擦除项目不会使现有的迭代器无效,则无需这样做。

The reason for returning an iterator on erase is so that you can iterate over the list erasing elements as you go. If erasing an item doesn't invalidate existing iterators there is no need to do this.

这篇关于STL矢量vs地图擦除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆