Problem with deleting vector items

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christian Bruckhoff

    Problem with deleting vector items

    Hi.

    I got a problem with deleting items of a vector. I did it like this:

    void THIS::bashDelPe rson() {
    cout << "Bitte Suchstring eingeben: ";
    char search[128];
    cin >search;
    vector<Person>: :iterator iter;
    iter = persons.begin() ;
    Person* person;

    while ( iter != persons.end() ) {
    if ( strstr(iter->getFirstName() , search) != 0 ) {
    persons.erase(i ter);
    }
    cout << "Moep\n";
    if(iter != persons.end()){ iter++;}
    }
    }

    But this implementation only works if the Person, which should be deleted is
    the last one. If it is followed by another person it looks like this, if i
    print out the persons:

    Thats the vector i had before:

    Vorname: Hans
    Nachname: Wurst

    Vorname: Alf
    Nachname: Egel

    Vorname: John
    Nachname: Doe

    By deleting Alf it looks like this:

    Vorname: Hans
    Nachname: Wurst

    Vorname: àr
    Nachname: °s

    How can i fix it?

    Regards!
    Christian


  • Victor Bazarov

    #2
    Re: Problem with deleting vector items

    Christian Bruckhoff wrote:
    I got a problem with deleting items of a vector. I did it like this:
    >
    void THIS::bashDelPe rson() {
    cout << "Bitte Suchstring eingeben: ";
    char search[128];
    cin >search;
    vector<Person>: :iterator iter;
    iter = persons.begin() ;
    Person* person;
    >
    while ( iter != persons.end() ) {
    if ( strstr(iter->getFirstName() , search) != 0 ) {
    persons.erase(i ter);
    'erase' invalidates all iterators after and including the one passed
    to it. You cannot use 'iter' after that. RTFM about 'erase'. It
    returns an iterator. Use the return value. Most likely you need to
    do

    iter = persons.erase(i ter);

    and also fix the 'if' statement three lines down.
    }
    cout << "Moep\n";
    if(iter != persons.end()){ iter++;}
    }
    }
    >
    [..]

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Roland Pibinger

      #3
      Re: Problem with deleting vector items

      On Thu, 28 Sep 2006 09:20:34 -0400, "Victor Bazarov"
      <v.Abazarov@com Acast.netwrote:
      >Christian Bruckhoff wrote:
      >I got a problem with deleting items of a vector. I did it like this:
      >>
      >while ( iter != persons.end() ) {
      > if ( strstr(iter->getFirstName() , search) != 0 ) {
      > persons.erase(i ter);
      >
      >'erase' invalidates all iterators after and including the one passed
      >to it. You cannot use 'iter' after that. RTFM about 'erase'. It
      >returns an iterator. Use the return value.
      Usability problems are not so trivial as you seem to assume. Qt eg.
      ditched STL iterators (and therfore all of STL) because of problems
      like the above in favor of Java style(!) iterators:

      (BTW, the link contains the answer to the OP's question).

      Best regards,
      Roland Pibinger

      Comment

      • Christian Bruckhoff

        #4
        Re: Problem with deleting vector items


        "Victor Bazarov" <v.Abazarov@com Acast.netschrie b im Newsbeitrag
        news:efgi73$cdr $1@news.datemas .de...
        Christian Bruckhoff wrote:
        >I got a problem with deleting items of a vector. I did it like this:
        >>
        >void THIS::bashDelPe rson() {
        >cout << "Bitte Suchstring eingeben: ";
        >char search[128];
        >cin >search;
        >vector<Person> ::iterator iter;
        >iter = persons.begin() ;
        >Person* person;
        >>
        >while ( iter != persons.end() ) {
        > if ( strstr(iter->getFirstName() , search) != 0 ) {
        > persons.erase(i ter);
        >
        'erase' invalidates all iterators after and including the one passed
        to it. You cannot use 'iter' after that. RTFM about 'erase'. It
        returns an iterator. Use the return value. Most likely you need to
        do
        >
        iter = persons.erase(i ter);
        >
        Hi.

        Doesn't change anything. :-(

        Regards!
        Christian


        Comment

        • Ron Natalie

          #5
          Re: Problem with deleting vector items

          Christian Bruckhoff wrote:
          >>
          > iter = persons.erase(i ter);
          >>
          Hi.
          >
          Doesn't change anything. :-(
          >
          Make sure that if you set the new iter position with the above
          statement that you don't do an extra ++ operation on it which
          might skip over an element (or push you past the end() location

          >

          Comment

          • Christian Bruckhoff

            #6
            Re: Problem with deleting vector items


            "Ron Natalie" <ron@spamcop.ne tschrieb im Newsbeitrag
            news:451D066E.9 030504@spamcop. net...
            Christian Bruckhoff wrote:
            >
            >>>
            >> iter = persons.erase(i ter);
            >>>
            >Hi.
            >>
            >Doesn't change anything. :-(
            >>
            Make sure that if you set the new iter position with the above
            statement that you don't do an extra ++ operation on it which
            might skip over an element (or push you past the end() location
            >
            Hi.
            I now did it like this:

            if ( iter != persons.end()) {
            iter = vector.ersase(i ter);
            }
            else {
            iter++;
            }

            So, there should be no extra increase of the iterator. But it oesnt work :-(

            Regards!
            Christian


            Comment

            • Ron Natalie

              #7
              Re: Problem with deleting vector items

              Christian Bruckhoff wrote:
              >
              So, there should be no extra increase of the iterator. But it oesnt work :-(
              OK Here was your original code:

              vector<Person>: :iterator iter;
              iter = persons.begin() ;
              Person* person;

              while ( iter != persons.end() ) {
              if ( strstr(iter->getFirstName() , search) != 0 ) {
              persons.erase(i ter);
              }
              cout << "Moep\n";
              if(iter != persons.end()){ iter++;}
              }

              The last test above is spurious. iter is NEVER
              going to be persons.end() at that point.

              Write the loop like this:

              vector<Person>: :iterator iter = persons.begin() ;
              while(iter != persons.end()) {
              if(strstr(iter->getFirstName() , search) != 0) {
              iter = persons.erase(i ter);
              } else {
              ++iter;
              }
              }

              Comment

              • Christian Bruckhoff

                #8
                Re: Problem with deleting vector items

                Write the loop like this:
                >
                vector<Person>: :iterator iter = persons.begin() ;
                while(iter != persons.end()) {
                if(strstr(iter->getFirstName() , search) != 0) {
                iter = persons.erase(i ter);
                } else {
                ++iter;
                }
                }
                Hi.
                Did it like this, but same failure then ever. :-(

                Regards!
                Christian


                Comment

                • Ron Natalie

                  #9
                  Re: Problem with deleting vector items

                  Christian Bruckhoff wrote:
                  >Write the loop like this:
                  >>
                  >vector<Person> ::iterator iter = persons.begin() ;
                  >while(iter != persons.end()) {
                  >if(strstr(it er->getFirstName() , search) != 0) {
                  >iter = persons.erase(i ter);
                  >} else {
                  >++iter;
                  >}
                  >}
                  >
                  Hi.
                  Did it like this, but same failure then ever. :-(
                  >
                  Regards!
                  Christian
                  >
                  >
                  Well you're going to have to give more details.
                  What failure. What are you expecting?

                  Comment

                  Working...