giaminh8368's blog

By giaminh8368, 2 years ago, In English

Hello, Does C++ have a way to delete map key automatically when the mapped value is equal to zero? I'm tired to erase them by an if statement.

Sorry for my bad English :)

  • Vote: I like it
  • +1
  • Vote: I do not like it

»
2 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

Nope. Use erase.

P.S. To expose answer. map doesn't know about value changing (operator[] gives reference to value to user and map doesn't have knowledge about what happens with value, so any logic there must be implemented by user of map). But you always can write helper which erases key if associated value is zero.