iterators of a swapped std::list

Mihai Vasilian grayasm@gmail.com
Wed May 1 16:45:00 GMT 2013


Hi,
I have a question related to iterators of a swapped list.

#include <list>
#include <stdio.h>
int main()
{
    int t[2]={-1,-2};
    std::list<int> l1(t, t+2), l2;
    std::list<int>::iterator beg=l1.begin(), end=l1.end();
    l1.swap(l2);
    for(; beg!=end; ++beg)
        printf("\n%d", *beg);
    return 0;
}

$ gcc --version
gcc (GCC) 4.8.0 20130411 (prerelease)

The program's output is:
-1
-2
4195008
-1
-2
4195008
-1
-2
4195008
and it doesn't stop.

I also tested GCC 4.7.3 with the same output.
Am I doing it wrong ?

Regards,
Mihai.



More information about the Gcc-help mailing list