Reputation: 19
When I use begin() as list.splice()'s first and third parameters at the same time, I don't know why output has infinite loop.
#include <list>
#include <iostream>
#include <iterator>
using namespace std;
int main() {
list<int> mylist;
for (int i = 1; i < 10; i++)
mylist.push_back(i);
mylist.splice(mylist.begin(), mylist, mylist.begin(), mylist.end());
for (auto it = mylist.begin(); it != mylist.end(); it++) {
cout << *it << " ";
}
}
Upvotes: 0
Views: 525
Reputation: 13438
The code exhibits undefined behavior:
Exception safety
(...) if (...) position is in the range [first,last) in (3), it causes undefined behavior.
That doesn't exactly explain why the infinite loop occurs but it explains why an infinite loop is a possible behavior.
Upvotes: 1