Vector erase dumps core when vector size is 2

Discussion in 'C++' started by Anil, Dec 5, 2007.

  1. Anil

    Anil Guest

    I am facing problem while erasing an elemet from stl vector when its
    size is 2. It works fine when SIZE > 2.
    Can anybody help me in this?? Following is the sample code which i
    tried.

    #include <iostream>
    #include <vector>

    using namespace std;
    #define SIZE 2
    main()
    {
    vector<int> myVect;
    std::vector<int>::iterator iter;

    for(int i =0; i<SIZE; i++)
    myVect.push_back(i);

    cout<< myVect.size() << "\n";
    for( iter = myVect.begin(); iter != myVect.end(); iter++)
    cout<< *iter << " ";

    cout<<"\n";

    for( iter = myVect.begin(); iter != myVect.end(); iter++)
    {
    cout<< *iter << "\n";
    if( *iter == 1)
    myVect.erase(iter);

    }

    return 0;
    }
    Anil, Dec 5, 2007
    #1
    1. Advertising

  2. Anil

    Mark P Guest

    Anil wrote:
    > I am facing problem while erasing an elemet from stl vector when its
    > size is 2. It works fine when SIZE > 2.
    > Can anybody help me in this?? Following is the sample code which i
    > tried.
    >
    > #include <iostream>
    > #include <vector>
    >
    > using namespace std;
    > #define SIZE 2
    > main()
    > {
    > vector<int> myVect;
    > std::vector<int>::iterator iter;
    >
    > for(int i =0; i<SIZE; i++)
    > myVect.push_back(i);
    >
    > cout<< myVect.size() << "\n";
    > for( iter = myVect.begin(); iter != myVect.end(); iter++)
    > cout<< *iter << " ";
    >
    > cout<<"\n";
    >
    > for( iter = myVect.begin(); iter != myVect.end(); iter++)
    > {
    > cout<< *iter << "\n";
    > if( *iter == 1)
    > myVect.erase(iter);
    >
    > }
    >
    > return 0;
    > }


    Calling erase invalidates any iterators pointing at or after the erased
    element. The canonical way to do this is, rather than ++iter in the loop:

    iter = myVect.erase(iter);

    Read footnote 5 here: http://www.sgi.com/tech/stl/Vector.html#5
    Read about erase here: http://www.sgi.com/tech/stl/Sequence.html

    -Mark
    Mark P, Dec 5, 2007
    #2
    1. Advertising

  3. Anil

    Howard Guest

    "Anil" <> wrote in message
    news:...
    >I am facing problem while erasing an elemet from stl vector when its
    > size is 2. It works fine when SIZE > 2.
    > Can anybody help me in this?? Following is the sample code which i
    > tried.
    >
    > #include <iostream>
    > #include <vector>
    >
    > using namespace std;
    > #define SIZE 2
    > main()
    > {
    > vector<int> myVect;
    > std::vector<int>::iterator iter;
    >
    > for(int i =0; i<SIZE; i++)
    > myVect.push_back(i);
    >
    > cout<< myVect.size() << "\n";
    > for( iter = myVect.begin(); iter != myVect.end(); iter++)
    > cout<< *iter << " ";
    >
    > cout<<"\n";
    >
    > for( iter = myVect.begin(); iter != myVect.end(); iter++)
    > {
    > cout<< *iter << "\n";
    > if( *iter == 1)
    > myVect.erase(iter);
    >
    > }
    >
    > return 0;
    > }


    I think that calling erase above causes iter to become invalidated, so it's
    illegal to call iter++ on it afterwards, regardless of whether it seems to
    work on some vectors. The erase functions returns an iterator to the next
    item, so you can set iter to that when calling erase, and only incrementing
    when not calling erase. I believe there's also a std algorithm for this
    (remove_if or something like that, I forget).

    -Howard
    Howard, Dec 5, 2007
    #3
  4. On Wed, 5 Dec 2007 15:01:34 -0800 (PST), Anil wrote:
    > main()
    > {


    Is there some popular course that mistakenly teaches
    declaring the main() function like that? Such line
    seems common in questions lately in this group.

    The proper minimal way to declare main(), is of course:

    int main()

    --
    Joel Yliluoma - http://iki.fi/bisqwit/
    Joel Yliluoma, Dec 17, 2007
    #4
  5. Anil

    red floyd Guest

    Joel Yliluoma wrote:
    > On Wed, 5 Dec 2007 15:01:34 -0800 (PST), Anil wrote:
    >> main()
    >> {

    >
    > Is there some popular course that mistakenly teaches
    > declaring the main() function like that? Such line
    > seems common in questions lately in this group.
    >


    Welcome to India.
    red floyd, Dec 17, 2007
    #5
  6. Anil

    Jim Langston Guest

    Anil wrote:
    > I am facing problem while erasing an elemet from stl vector when its
    > size is 2. It works fine when SIZE > 2.
    > Can anybody help me in this?? Following is the sample code which i
    > tried.
    >
    > #include <iostream>
    > #include <vector>
    >
    > using namespace std;
    > #define SIZE 2
    > main()
    > {
    > vector<int> myVect;
    > std::vector<int>::iterator iter;
    >
    > for(int i =0; i<SIZE; i++)
    > myVect.push_back(i);
    >
    > cout<< myVect.size() << "\n";
    > for( iter = myVect.begin(); iter != myVect.end(); iter++)
    > cout<< *iter << " ";
    >
    > cout<<"\n";
    >

    for( iter = myVect.begin(); iter != myVect.end(); )
    > {
    > cout<< *iter << "\n";
    > if( *iter == 1)

    iter = myVect.erase(iter);
    else
    ++iter;
    >
    > }
    >
    > return 0;
    > }




    --
    Jim Langston
    Jim Langston, Dec 18, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell King

    Analyzing java core dumps

    Russell King, Aug 20, 2004, in forum: Java
    Replies:
    0
    Views:
    2,661
    Russell King
    Aug 20, 2004
  2. Bharat
    Replies:
    0
    Views:
    430
    Bharat
    Oct 6, 2004
  3. Mani
    Replies:
    1
    Views:
    881
    John Harrison
    Oct 21, 2004
  4. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    346
    Pete Becker
    Mar 30, 2006
  5. Victor Kryukov
    Replies:
    8
    Views:
    466
    Gabriel Genellina
    May 17, 2007
Loading...

Share This Page