Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When will STL iterator be equal to zero?

Tags:

c++

iterator

stl

I have a program which is like this

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

So here i am comparing iterator with zero. but after deleting the last element the compiler is showing this error.

*** 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

If I want the iterator to be zero if the queue/list is empty.. what i should do? Because in my project I need to compare this iterator with zero only and not with alist.end().. What may be the probable solution to this...?

like image 671
Chaithra Avatar asked Jan 22 '26 13:01

Chaithra


1 Answers

Why do you think the iterator will ever "be zero"? Iterators are not pointers or indexes. If you need to check if a container is empty, use the empty() member function.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!