3

Is there a defined behavior for container.erase(first,last) when first == last in the STL, or is it undefined?

Example:

std::vector<int> v(1,1);
v.erase(v.begin(),v.begin());
std::cout << v.size(); // 1 or 0?

If there is a Standard Library specification document that has this information I would appreciate a reference to it.

Andrew Hundt
  • 2,551
  • 2
  • 32
  • 64
  • 2
    Do you mean the STL? or do you mean the standard library? – PlasmaHH Oct 13 '11 at 15:41
  • @PlasmaHH: The subset of the library that deals with templates, I guess. – Sebastian Mach Oct 13 '11 at 15:45
  • You can find exact documentation in the standard: http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents – Martin York Oct 13 '11 at 15:49
  • More descriptive documentation can be found at SGI by the original author of the STL: http://www.sgi.com/tech/stl – Martin York Oct 13 '11 at 15:50
  • 2
    PlasmaHH: in this context the difference is irrelevant. – rubenvb Oct 13 '11 at 15:57
  • possible duplicate of [Is it defined to provide an empty range to C++ standard algorithms?](http://stackoverflow.com/questions/7505267/is-it-defined-to-provide-an-empty-range-to-c-standard-algorithms) – Lightness Races in Orbit Oct 13 '11 at 16:10
  • Tomalak: *sigh*: search for "erase" on this page: http://www.sgi.com/tech/stl/Vector.html (it's the original STL documentation). Please stop being childish about this. – rubenvb Oct 13 '11 at 18:24

4 Answers4

5

The behavior is well defined.

It is a No-op(No-Operation). It does not perform any erase operation on the container as end is same as begin.

The relevant Quote from the Standard are as follows:

C++03 Standard: 24.1 Iterator requirements and
C++11 Standard: 24.2.1 Iterator requirements

Para 6 & 7 for both:

An iterator j is called reachable from an iterator i if and only if there is a finite sequence of applications of the expression ++i that makes i == j. If j is reachable from i, they refer to the same container.

Most of the library’s algorithmic templates that operate on data structures have interfaces that use ranges.A range is a pair of iterators that designate the beginning and end of the computation. A range [i, i) is an empty range; in general, a range [i, j) refers to the elements in the data structure starting with the one pointed to by i and up to but not including the one pointed to by j. Range [i, j) is valid if and only if j is reachable from i. The result of the application of functions in the library to invalid ranges is undefined.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    It does at least a comparison, though. – Sebastian Mach Oct 13 '11 at 15:46
  • @phresnel: Ofcourse NO-OP is always w.r.t caller. It performs No visible operation for the user. – Alok Save Oct 13 '11 at 15:52
  • It may become visible with iterator traits. (However, as this is on the borderline of pedantry (but is not pedantry as for the possibility of becoming visible), I don't have any thought of downvoting you and I won't commit doing so) (and my definition of no-op slightly varies from yours, but that's for the fact that nobody standardized that term :/) – Sebastian Mach Oct 13 '11 at 16:08
4

That would erase nothing at all, just like other algorithms that operate on [, ) ranges.

Even if the container is empty I think that would still work because begin() == end().

Mark B
  • 95,107
  • 10
  • 109
  • 188
1

It is perfectly defined. It removes all elements from first to last, including first and excluding last. If there are no elements in this range (when first == last), then how much are removed? You guessed it, none.

Though I'm not sure what happens if first comes after last, I suppose this will invoke undefined behaviour.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
1

Conceptually, there is an ordinary loop from begin to end, with a simple loop condition that checks if the iterator is end already, like this:

void erase (iterator from, iterator to) {
    ...
    while (from != to) erase (from++);
    ...
}

(however, implementations may vary). As you see, if from==to, then there is no single iteration of the loop body.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130