c++ - STL 迭代器 : Assertion Error

标签 c++ stl iterator erase

为什么以下代码会产生断言错误:Expression: list iterators incompatible

#include <list>
using namespace std;

int main()
{
    list<int> a;
    a.push_back(1);
    list<int>::iterator iter=a.begin();
    a.erase(iter);

    iter==a.end();
}

最佳答案

你想要做的是这样的:

#include <list>
using namespace std;

int main()
{
    list<int> a;
    a.push_back(1);
    list<int>::iterator iter=a.begin();
    iter = a.erase(iter);
}

关于c++ - STL 迭代器 : Assertion Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13671824/

相关文章:

c++ - 将私有(private)变量添加到 C++ STL 类

c++ - C++ 迭代器是否应该在传递最后一项后递减?

android - 从数组列表中删除对象 - 并发修改异常

c++ - "Observer pattern"上的复制构造函数

c++ - Abseil C++ 模板参数

c++ - 在 Visual Studio 2010 中编译 C++ 程序时出错

c++ - 通用/多态迭代器

c++ - 在C++中使用cuda进行颜色空间转换

c++ - 为什么 std::copy 或 std::swap 不需要 <algorithm>?

c++ - 使用模板参数在模板类中定义一对迭代器