<p>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?</p>

[{'answer_id': 52733, 'author': 'Torlack', 'author_id': 5243, 'author_profile': 'https://Stackoverflow.com/users/5243', 'pm_score': 1, 'selected': False, 'text': '<p>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".</p>\n'}, {'answer_id': 52734, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 6, 'selected': True, 'text': '<p>See <a href="http://www.sgi.com/tech/stl/Map.html" rel="noreferrer">http://www.sgi.com/tech/stl/Map.html</a></p>\n\n<blockquote>\n  <p>Map has the important property that\n  inserting a new element into a map\n  does not invalidate iterators that\n  point to existing elements. Erasing an\n  element from a map also does not\n  invalidate any iterators, except, of\n  course, for iterators that actually\n  point to the element that is being\n  erased.</p>\n</blockquote>\n\n<p>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.</p>\n'}, {'answer_id': 52735, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 3, 'selected': False, 'text': "<p>The inconsistency is due to use. <code>vector</code> is a sequence having an ordering over the elements. While it's true that the elements in a <code>map</code> are also ordered according to some comparison criterion, this ordering is non-evident from the structure. 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 <em>O</em>(<em>n</em>), unless a stack is used, in which case the space required is no longer constant.</p>\n\n<p>All in all, there simply is no cheap way of returning the “next” element after erasing. For sequences, there <em>is</em> a way.</p>\n\n<p>Additionally, Rob is right. There's no need for the Map to return an iterator.</p>\n"}, {'answer_id': 52761, 'author': 'Henk', 'author_id': 4613, 'author_profile': 'https://Stackoverflow.com/users/4613', 'pm_score': 2, 'selected': False, 'text': "<p>Just as an aside, the STL shipped with MS Visual Studio C++ (Dinkumware IIRC) provides a map implementation with an <code>erase</code> function returning an iterator to the next element.</p>\n\n<p>They do note it's not standards conforming.</p>\n"}, {'answer_id': 104372, 'author': 'Daniel James', 'author_id': 2434, 'author_profile': 'https://Stackoverflow.com/users/2434', 'pm_score': 4, 'selected': False, 'text': '<p><code>erase</code> returns an <code>iterator</code> in C++11. This is due to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2728.html#130" rel="noreferrer">defect report 130</a>:</p>\n\n<blockquote>\n  <p>Table 67 (23.1.1) says that container::erase(iterator) returns an iterator. Table 69 (23.1.2) says that in addition to this requirement, associative containers also say that container::erase(iterator) returns 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.</p>\n</blockquote>\n\n<p>The standards committee accepted this:</p>\n\n<blockquote>\n  <p>the LWG agrees the return type should be iterator, not void. (Alex Stepanov agrees too.)</p>\n</blockquote>\n\n<p>(LWG = Library Working Group).</p>\n'}]
['https://Stackoverflow.com/questions/52714', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2328/']