So incrementing or decrementing the end() iterator is defined in the standard? On linux, the begin() is implemented as end()++.
#include <list>
#include <iostream>
int main()
{
std::list<int> numbers;
for (int i = 0; i < 10; i++)
numbers.push_back(i);
auto it = numbers.begin();
int count = 3;
while (count)
{
std::cout << *it++;
if (it == numbers.end())
{
++it; // is this ok ???
--count;
std::cout << '\n';
}
}
}
So the output always the same on every platform?
Output:
0123456789
0123456789
0123456789
Incrementing the iterator returned from end()
of any of the standard C++ library containers results in undefined behavior. Due to an implementation detail common to most implementations of std::list<T>
it may work to increment list.end()
but there is no guarantee that it does.
No, this is not ok. The std::list
iterator is a BidirectionalIterator , which is a refinement of ForwardIterator . The precondition for both ++i
and i++
for a ForwardIterator states:
i
is dereferenceable
which does not hold for end()
as it points past the last item of the list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.