Блог пользователя luka25

Автор luka25, история, 8 лет назад, По-английски

I just tried to delete some members from set while iterating with this code:

while(*it<=b){ if(*it!=x){m[*it]=x;s.erase(it);} else it++; }

I found out the right one was this, but can't figure out why

while(*it<=b){ if(*it!=x){m[*it]=x;s.erase(it++);} else it++; }

could anybody explain why it++ instead of it please?

  • Проголосовать: нравится
  • +6
  • Проголосовать: не нравится

»
8 лет назад, # |
Rev. 4   Проголосовать: нравится +4 Проголосовать: не нравится

Because if you look into set documentation erase method you'll find the following:

Iterators, pointers and references referring to elements removed by the function are invalidated. All other iterators, pointers and references keep their validity.

so s.erase(it) invalides it.

s.erase(it++) works, because it++ returns copy of iterator. For example,++it wouldn't work.

  • »
    »
    8 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +9 Проголосовать: не нравится

    any code with postfix operators is undefined behavior waiting to happen. The only proper way to do it is
    it = s.erase(it)

    • »
      »
      »
      8 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      how it=s.erase(it)?I can't write that

    • »
      »
      »
      8 лет назад, # ^ |
        Проголосовать: нравится +1 Проголосовать: не нравится

      Are you sure?

      All function parameters must be fully evaluated before function call — this is one of few rules you can be sure of in expression evaluation.

      So it++ must be fully evaluated before function call. Which means that there should be two copies of it — one incremented, which becomes it going forward and one copy not incremented, which is provided to the function and afterwards deleted.

      What can cause troubles?

      Also it = s.erase(it) is only C++11.

  • »
    »
    8 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks for reply, I get it now, but would be more logical first way to me.