CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    249

    Exclamation Erase with iterator

    Hi,
    I heard that erasing elements using iterator is dangerous.
    But I tried the below program in gcc and it is running fine.
    Please explain how it is dangerous

    Code:
    #include<vector>
    #include<iterator>
    #include<iostream>
    using namespace std;
    int main()
    {
        vector<int>vec;
        vec.push_back(123);
        vec.push_back(12);
        vec.push_back(23);
        vec.push_back(123);
        vec.push_back(1123);
        vec.push_back(111);
        vec.push_back(2223);
    
        copy(vec.begin(),vec.end(),ostream_iterator<int>(cout,"---"));
        cout<<endl;
        vector<int>::iterator itr;
        for(itr=vec.begin();itr!=vec.end();itr++)
        {
            if(*itr == 123)
                vec.erase(itr);
        }
    
        copy(vec.begin(),vec.end(),ostream_iterator<int>(cout,"---"));
        cout<<endl;
    }
    output is:
    123---12---23---123---1123---111---2223---
    12---23---1123---111---2223---

  2. #2
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: Erase with iterator

    Read up on erase.
    your humble savant

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Erase with iterator

    Quote Originally Posted by Rajesh1978
    Please explain how it is dangerous
    The problem is that the call to erase invalidates the iterator (and iterators pointing to subsequent elements). You could avoid this by writing:
    Code:
    for (itr = vec.begin(); itr != vec.end();)
    {
        if (*itr == 123)
        {
            itr = vec.erase(itr);
        }
        else
        {
            ++itr;
        }
    }
    But it would be simpler and safer to write:
    Code:
    vec.erase(remove(vec.begin(), vec.end(), 123), vec.end());
    By the way, you forgot to #include <algorithm>.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jul 2007
    Posts
    249

    Re: Erase with iterator

    Thanks
    I understood now.
    But I surprised how my code ran without any problem.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Erase with iterator

    Code:
    #include<vector>
    #include<iterator>
    #include<iostream>
    using namespace std;
    int main()
    {
        vector<int>vec;
        vec.push_back(123);
        vec.push_back(12);
        vec.push_back(23);
        vec.push_back(123);
        vec.push_back(123);
        vec.push_back(1123);
        vec.push_back(111);
        vec.push_back(2223);
    
        copy(vec.begin(),vec.end(),ostream_iterator<int>(cout,"---"));
        cout<<endl;
        vector<int>::iterator itr;
        for(itr=vec.begin();itr!=vec.end();itr++)
        {
            if(*itr == 123)
                vec.erase(itr);
        }
    
        copy(vec.begin(),vec.end(),ostream_iterator<int>(cout,"---"));
        cout<<endl;
    }
    output is:
    Code:
    123---12---23---123---123---1123---111---2223---
    12---23---123---1123---111---2223---
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Jul 2007
    Posts
    249

    Re: Erase with iterator

    Thanks a lot
    You people gave a very vivid explanation and made me understood.

    Regards

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Erase with iterator

    Quote Originally Posted by Rajesh1978 View Post
    Hi,
    I heard that erasing elements using iterator is dangerous.
    But I tried the below program in gcc and it is running fine.
    Please explain how it is dangerous
    When you make a mistake in a C++ program, there is no guarantee how your program will run. It may "work", it may crash, it may work sometimes and crash another time, it may work differently if compiled with a different compiler or different options, etc.

    So writing code, and then seeing that it runs does not mean that the code is OK. If you're told why something in C++ is undefined, don't try and write code to see what happens -- you don't know what will happen. Unlike other computer languages, C++ doesn't automatically tell you that you did something wrong when you run a program.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 26th, 2010 at 03:37 AM.

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