0

I mean in situation when the iterators point on same element.

On http://www.cplusplus.com/reference/stl/list/erase/ say "Removes from the list container either a single element (position) or a range of elements ([first,last))." and "first, last Iterators specifying a range within the list container to be removed: [first,last). i.e., the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last."

I totally don't know if I do everything wrong but for every part of my code I don't find needed information anywhere and when I want to test it by myself, I end in a situation, when I don't know what happened and after asking here and arguing for long hours I find something like "undefined behavior". So can someone help me faster, what is it now?

And I want to be better programmer and find out better source than cplusplus.com and cppreference.com, because they both suck, is there something better? I am getting crazier every day with this C++ (but I still think it's much better for speedy huge programs than Java or C), please help.

Lukas Salich
  • 959
  • 2
  • 12
  • 30

2 Answers2

9

The Standard's own definition of ranges (24.2.1p7, emphasis mine):

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 element pointed to by i and up to but not including the element pointed to by j.

So assuming it is a valid iterator in or past-the-end of lst, the call lst.erase(it,it) erases an empty set of elements from lst. That is, it does nothing.

aschepler
  • 70,891
  • 9
  • 107
  • 161
2

I think to best answer your question you should think about how iterators work and why everything is passed in as [first, last) and not something else.

There are two core rules about iterators that you need to keep in mind. You can always increment one (that is to say, first++) and two iterators that point to the same element will always be equal. Knowing this you can loop over ANY range of iterators with the logic:

for(; first != last; first++)
{
}

So, if first and last are equal, nothing will happen. So if you call list.erase(it, it) nothing will be erased.

To put it in a more general form. Any range in STL where first == last is effectively empty.

Fox Cutter
  • 655
  • 4
  • 11