avatar
T*r
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
avatar
p*o
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

avatar
T*B
3
The iterator is invalidated after the erase operation.
avatar
b*n
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.
avatar
B*e
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。