473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

does STL hash_multiset erase invalidate the iterator?

Is the following code guaranteed to work? I have a hashed set of pointers,
which I would like to selectively delete from. Can I iterate through the
set, deleting certain ones, without invalidating the iterator? This
ominous statement in the STL "set" documentation makes me worried. I saw
no similar statement on the "hash_multiset" docs.

"Erasing an element from a set also does not invalidate any iterators,
except, of course, for iterators that actually point to the element that is
being erased."

hash_multiset<mytype*> myset;
// insert a bunch of mytype pointers into myset

for (hash_multiset<mytype*>::iterator i=myset.begin(); i!=myset.end(); i++)
if (mytype->someproperty() == 1) {
mytype* p = *i;
myset.erase(i);
delete p;
}
Thanks.

Jul 22 '05 #1
3 3065

"Kenneth Massey" <ke******@vt.edu> wrote in message
news:2gwLc.4988$_K2.2563@lakeread02...
Is the following code guaranteed to work? I have a hashed set of pointers, which I would like to selectively delete from. Can I iterate through the
set, deleting certain ones, without invalidating the iterator? This
ominous statement in the STL "set" documentation makes me worried. I saw
no similar statement on the "hash_multiset" docs.

"Erasing an element from a set also does not invalidate any iterators,
except, of course, for iterators that actually point to the element that is being erased."

hash_multiset<mytype*> myset;
// insert a bunch of mytype pointers into myset

for (hash_multiset<mytype*>::iterator i=myset.begin(); i!=myset.end(); i++) if (mytype->someproperty() == 1) {
mytype* p = *i;
myset.erase(i);
delete p;
}


Well you ARE invalidating the iterator, because it does point to the element
being erased. You erase i and then you do i++, that is using an invalidated
iterator.

Here's how to do it

hash_multiset<mytype*>::iterator i=myset.begin();
while (i!=myset.end())
{
if (mytype->someproperty() == 1)
{
mytype* p = *i;
myset.erase(i++);
delete p;
}
else
{
++i;
}
}

By saying 'myset.erase(i++);' you make sure that the iterator is incremented
before you erase not afterwards.

john

Jul 22 '05 #2
> mytype* p = *i;
myset.erase(i++);
delete p;


Incidentally there is no need for the variable p.

delete *i;
myset.erase(i++);

john
Jul 22 '05 #3
John Harrison wrote:
mytype* p = *i;
myset.erase(i++);
delete p;


Incidentally there is no need for the variable p.

delete *i;
myset.erase(i++);

john


Thanks for the help.
Actually I want to make sure the pointer is deleted *after* the set entry
has been erased. My hash and equal functions reference the pointer through
->, and I don't want to risk them being called on a deleted pointer.
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Paras | last post by:
Hi What is the correct way to delete an element from STL list while iterating through the list list<A*> _ls; A * a; list<A*>::iterator si1;
9
by: BCC | last post by:
I have the following code, where good_list is a vector of CUnits: int high_cutoff = 10; vector<CUnit>::iterator it; for (it = good_list.end(); it != good_list.begin(); --it) { CUnit* ccu = it;...
5
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator...
3
by: jose luis fernandez diaz | last post by:
Hi, Erase elements while iterating on a map don't invalidate the iterator except the erased one, so the program below: (1) #include <map> int main()
4
by: Nick Keighley | last post by:
Hi, I've checked out various documentation for multimap but can't find anywhere it explicitly stated that insert() invalidates multimap iterators. consider this pseudo code:- int...
7
by: al.cpwn | last post by:
Is there a reason why erase for vector returns an iterator but erase for set doesn't? Is this something we should keep in mind while designing our own containers (like binary tree for example)?
1
by: Varun Kacholia | last post by:
I apologize if there exists a standard way of deleting multiple elements from a STL hash_multiset (or even multiset for that matter) that I am unaware of. The problem, I see, with multisets is...
10
by: Jim Langston | last post by:
Someone is working on some code, and during the iteration of a vector may delete an element and push_back a new one. I was thinking that this may invalidate the iteration iterator, but in my test...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.