T********r 发帖数: 6210 | 1 Supposedly I want to delete multiple entries in a STL hash map. The code
looks like the following:
for (it = hash_table.begin(); it != hash_table.end(); it ++)
{
if (some_condition)
hash_table.erase (it);
}
My questions is, after erasing an entry referenced by 'it', will 'it' still
be valid such that you can use 'it++' to find the next entry in the hash map
? Or will 'it' automatically points to the next entry in the hash map
because the total number of entries already reduced by one |
p***o 发帖数: 1252 | 2 No. You have to compute the next iterator before erasing.
still
map
【在 T********r 的大作中提到】 : Supposedly I want to delete multiple entries in a STL hash map. The code : looks like the following: : for (it = hash_table.begin(); it != hash_table.end(); it ++) : { : if (some_condition) : hash_table.erase (it); : } : My questions is, after erasing an entry referenced by 'it', will 'it' still : be valid such that you can use 'it++' to find the next entry in the hash map : ? Or will 'it' automatically points to the next entry in the hash map
|
T***B 发帖数: 137 | 3 The iterator is invalidated after the erase operation. |
b********n 发帖数: 609 | 4 咱能不能不这麽死心眼啊?
it = hash_table.begin();
while (it != hash_table.end())
{
hash_table::iterator it1 = it+1;
if (some_condition)
hash_table.erase (it);
it = it1;
}
【在 T***B 的大作中提到】 : The iterator is invalidated after the erase operation.
|
B******e 发帖数: 48 | 5 i think hash_table.erase(it++) should do.
but in this case, you need to adjust for loop to something like:
for (it =..; it != ..; )
{
if ( ...)
hash_table.erase(it++);
else
++it;
}
still
map
【在 T********r 的大作中提到】 : Supposedly I want to delete multiple entries in a STL hash map. The code : looks like the following: : for (it = hash_table.begin(); it != hash_table.end(); it ++) : { : if (some_condition) : hash_table.erase (it); : } : My questions is, after erasing an entry referenced by 'it', will 'it' still : be valid such that you can use 'it++' to find the next entry in the hash map : ? Or will 'it' automatically points to the next entry in the hash map
|