简体   繁体   English

STL矢量vs地图擦除

[英]STL vector vs map erase

In the STL almost all containers have an erase function. 在STL中,几乎所有容器都具有擦除功能。 The question I have is in a vector, the erase function returns an iterator pointing to the next element in the vector. 我的问题是在向量中,erase函数返回一个指向向量中下一个元素的迭代器。 The map container does not do this. 地图容器不会这样做。 Instead it returns a void. 相反,它返回一个空白。 Anyone know why there is this inconsistancy? 有人知道为什么会有这种不一致吗?

See http://www.sgi.com/tech/stl/Map.html http://www.sgi.com/tech/stl/Map.html

Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. Map具有重要的属性,即将新元素插入到映射中不会使指向现有元素的迭代器无效。 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. 如果擦除项目不会使现有迭代器无效,则无需执行此操作。

erase returns an iterator in C++11. erase在C ++ 11中返回一个iterator This is due to defect report 130 : 这是由于缺陷报告130

Table 67 (23.1.1) says that container::erase(iterator) returns an iterator. 表67(23.1.1)表示container :: erase(iterator)返回一个迭代器。 Table 69 (23.1.2) says that in addition to this requirement, associative containers also say that container::erase(iterator) returns void. 表69(23.1.2)表示除了这个要求之外,关联容器还说容器:: erase(iterator)返回void。 That's not an addition; 这不是一个补充; it's a change to the requirements, which has the effect of making associative containers fail to meet the requirements for containers. 它是对需求的更改,其结果是使关联容器无法满足容器的要求。

The standards committee accepted this: 标准委员会接受了这个:

the LWG agrees the return type should be iterator, not void. LWG同意返回类型应该是迭代器,而不是无效。 (Alex Stepanov agrees too.) (Alex Stepanov也同意。)

(LWG = Library Working Group). (LWG =图书馆工作组)。

The inconsistency is due to use. 不一致是由于使用。 vector is a sequence having an ordering over the elements. vector是对元素具有排序的序列。 While it's true that the elements in a map are also ordered according to some comparison criterion, this ordering is non-evident from the structure. 虽然map中的元素也是按照某种比较标准排序的,但这种排序在结构上是不明显的。 There is no efficient way to get from one element to the next (efficient = constant time). 没有有效的方法可以从一个元素到另一个元素(有效=恒定时间)。 In fact, to iterate over the map is quite expensive; 事实上,迭代地图是相当昂贵的; either the creation of the iterator or the iterator itself involves a walk over the complete tree. 迭代器的创建或迭代器本身涉及遍历整个树。 This cannot be done in O ( n ), unless a stack is used, in which case the space required is no longer constant. 这不能在On )中完成,除非使用堆栈,在这种情况下所需的空间不再是恒定的。

All in all, there simply is no cheap way of returning the “next” element after erasing. 总而言之,在擦除之后根本就没有廉价的方法来返回“下一个”元素。 For sequences, there is a way. 对于序列, 一种方法。

Additionally, Rob is right. 另外,罗布是对的。 There's no need for the Map to return an iterator. Map不需要返回迭代器。

Just as an aside, the STL shipped with MS Visual Studio C++ (Dinkumware IIRC) provides a map implementation with an erase function returning an iterator to the next element. 另外,MS Visual Studio C ++(Dinkumware IIRC)附带的STL提供了一个带有erase函数的map实现,它将迭代器返回到下一个元素。

They do note it's not standards conforming. 他们确实注意到它不符合标准。

I have no idea if this is the answer, but one reason might be with the cost of locating the next element. 我不知道这是否是答案,但一个原因可能是定位下一个元素的成本。 Iterating through a map is inherently "slow". 迭代地图本身就是“慢”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM