当我同时使用 begin() 作为 list.splice() 的第一个和第三个参数时, 我不知道为什么输出有无限循环。
#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 << " ";
}
}
最佳答案
代码表现出未定义的行为:
Exception safety
(...) if (...) position is in the range [first,last) in (3), it causes undefined behavior.
这并不能完全解释为什么会出现无限循环,但它可以解释为什么无限循环是一种可能的行为。
关于c++ - 在 C++ 标准中,当我同时使用 list.begin() 作为 list.splice() 的第一个和第三个参数时,为什么会出现无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41178460/