CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #2
    Join Date
    May 2015
    Posts
    540

    Re: Exception when the second list element size =1

    I also tried to write simple program to check the erase and it is working ok. This one i tried on cygwin. (The above program is in visual studio 19).

    Basically, when the list has only one element, the erase will return the next element . i.e end() . When it-- is done on end () , what happens ?
    In my previous program, i had posted, the erase is returning the begin() . Not sure why ?
    Code:
    int main()
    {
            std::list<int> lst(1, 2);
    
            for(std::list<int>::iterator it = lst.begin(); it != lst.end(); it++)
            {
                    std::cout << *it << "\t";
    
                    if (*it == 2)
                    {
                            it = lst.erase(it);
                            it--;
                    }
             }
    
    
            std::cout << "After : ";
    
            for(std::list<int>::iterator it = lst.begin(); it != lst.end(); it++)
            {
                    std::cout << *it << "\t";
            }
    }
    Last edited by pdk5; June 6th, 2020 at 06:32 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured