c++ - 在 C++ 标准中,当我同时使用 list.begin() 作为 list.splice() 的第一个和第三个参数时,为什么会出现无限循环

标签 c++ list splice

当我同时使用 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/

相关文章:

c# - 使用 CLR C++ 包装器从 C# 应用程序在 C++ 中加载 SDL 窗口不起作用

c++ - 编译如何选择调用哪个构造函数?

Python,两个列表相同的 "for"

javascript - this.students.splice(index,1) 在 UI 中没有改变 [Angular]

java - 从 int 创建时钟格式

javascript - 单击后函数运行两次

c++ - 是否可以将一对 <Key, Value> 转换为一对 <const Key, Value>?

c++ - 跨 dll 强制转换 void 指针

python "or"当其中一个变量可能不存在时?

python - 如何从 python 中的列表中提取某些值?