vendredi 27 septembre 2019

Removing element from std::map

This code is leading to unexpected behavior.

Ideally, I should get 1000 for all the values (key-value) but sometimes it gives me 0 for the next key value after deletion.

For ex: Initially map ['a', 0] ['b',0] ['c',0] ['d',0] ['e',0] ['f',0]

After deletion ['a', 1000] ['b',1000] ['d',0] ['e',1000] ['f',1000]

std::map<char, uint_32> pool;

void remove_entry(char key) {
     std::map<char, int>::iterator it = pool.find(key);

     pool.erase(it)
}

std::map<char, int>& get_pool() {
     return pool;
}

void process() {

     for (auto& kv : get_pool()) {

          if (kv.first == 'c') {
              remove_entry(kv.first);

              continue;
          }

          kv.second = 1000;
     }

}

Any help is appreciated. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire