由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - A C++ STL question
相关主题
C++ vector 一边遍历一边删What is wrong in this array declaration.
说c++不难的欢迎来看看这个Remove elements from multiple vectors in C++
一个C++的问题关于inserter
stl container erase in a loopdeque的pointer和reference是怎么回事?
don't understand this list (C++ STL)c++ string 一问
也问个STL的问题, 谢谢[合集] some file operation questions
修改map的键值boost intrusive unordered_set erase_and_dispose出错
stl 源代码疑问Interview question: is the following code OK?
相关话题的讨论汇总
话题: hash话题: table话题: stl话题: map话题: erase
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
Interview question: is the following code OK?don't understand this list (C++ STL)
来看一个实际问题吧也问个STL的问题, 谢谢
stl的问题,为啥swap可能会invalidate end iterator?修改map的键值
有否可以O(1)remove 一个元素的java LinkedList推荐?stl 源代码疑问
C++ vector 一边遍历一边删What is wrong in this array declaration.
说c++不难的欢迎来看看这个Remove elements from multiple vectors in C++
一个C++的问题关于inserter
stl container erase in a loopdeque的pointer和reference是怎么回事?
相关话题的讨论汇总
话题: hash话题: table话题: stl话题: map话题: erase