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

Автор giaminh8368, 2 года назад, По-английски

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 :)

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

»
2 года назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

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.