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_multi set" 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<m ytype*> myset;
// insert a bunch of mytype pointers into myset
for (hash_multiset< mytype*>::itera tor i=myset.begin() ; i!=myset.end(); i++)
if (mytype->someproperty () == 1) {
mytype* p = *i;
myset.erase(i);
delete p;
}
Thanks. 3 3083
"Kenneth Massey" <ke******@vt.ed u> wrote in message
news:2gwLc.4988 $_K2.2563@laker ead02... 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_multi set" 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<m ytype*> myset; // insert a bunch of mytype pointers into myset
for (hash_multiset< mytype*>::itera tor 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<m ytype*>::iterat or 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
> mytype* p = *i; myset.erase(i++ ); delete p;
Incidentally there is no need for the variable p.
delete *i;
myset.erase(i++ );
john
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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;
if (ccu->GetCutoff() >= high_cutoff) {
good_list.erase(it);
}
}
|
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 erase(iterator first, iterator last);
size_type erase(const Key& keyval);
Ie, the first two functions above have the same interface as
|
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()
|
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 DataItem::genDerived ()
{
| |
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)?
|
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 that you cannot obtain info to
uniquely identify
an element (eg: indices in a vector, or key in set/hash_set). Hence
what I'd like
to do is iterate over a multiset and delete elements while doing so.
Now I did read somewhere that deleting an...
|
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 it doesn't seem to. Here
is my test with the expected output of:
0 1 2 3 4 5
0 1 3 4 5 6
0 1 4 5 6 7
0 1 5 6 7 8
0 1 6 7 8 9
|
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 = new CTestClientSocket(this, ip, port);
pTemp->Connect();
m_collClients.push_back(pTemp);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |