c++ - STL迭代器什么时候会等于零?

标签 c++ stl iterator

我有一个这样的程序

list<int>:: iterator n = alist.begin();
while(n!= (list<int>::iterator)0)
{
    printf("Element is %d\n",*n);
    n = alist.erase(n);
}

所以在这里我将迭代器与零进行比较。 但在删除最后一个元素后,编译器显示此错误。

*** glibc detected *** ./new: free(): invalid pointer: 0xbf99cb10 ***
======= Backtrace: =========
/lib/libc.so.6[0xb7d956e1]
/lib/libc.so.6(cfree+0x89)[0xb7d96d79]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7f3ff81]
./new[0x8048c81]
./new[0x8048ca6]
./new[0x8048d07]
./new[0x8048d39]
./new(__gxx_personality_v0+0x216)[0x804888e]
/lib/libc.so.6(__libc_start_main+0xdc)[0xb7d46f9c]
./new(__gxx_personality_v0+0x49)[0x80486c1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:09 3704751    /home/sathya/chaithra/archivesthrash/new

如果队列/列表为空时我希望迭代器为零.. 我应该怎么做? 因为在我的项目中,我需要将此迭代器与零进行比较,而不是与 alist.end().. 可能的解决方案是什么...?

最佳答案

为什么您认为迭代器永远“为零”?迭代器不是指针或索引。如果需要检查容器是否为空,请使用 empty() 成员函数。

关于c++ - STL迭代器什么时候会等于零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/708948/

相关文章:

c++ - 使用 'lower_bound'

c# - Windows Mobile - 停止主手机应用程序

尝试呈现 SDL_Texture : Invalid texture 时出现 C++ SDL2 错误

c++ - 如何为 gcc-trunk 报告这个 ICE

c++ - 使用 STL/Boost 初始化硬编码集<vector<int>>

c++ - 重载求和函数,将列表/vector 与 map 混淆

c++ - Boost.Iostreams 与 iostream/streambuf 重载比特流 I/O

c++ - 比较迭代器和 const_iterators

python - python3中的可迭代类

python - python 中是否有像 C++11 中那样的多重集的 equal_range?