473,748 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to delete stl<map> element from iterator?

Hi,

After making an iterator to a map and stepping through a loop, I want to
delete any entries that satisfy a test from an external function.

MyMapType::cons t_iterator iter;

for(iter = m->begin(); iter != m->end(); ++iter)
{
a = iter->first;
b = iter->second;
if ( Test(a,b) == true)
iter->delete?();
}

How do I actually delete the element that iter is pointing to?

Thanks

Steve
Feb 21 '06 #1
11 24421

"Steve Edwards" <gf*@lineone.ne t> wrote in message
news:gf******** *************** @news.btinterne t.com...
Hi,

After making an iterator to a map and stepping through a loop, I want to
delete any entries that satisfy a test from an external function.

MyMapType::cons t_iterator iter;

for(iter = m->begin(); iter != m->end(); ++iter)
This assumes that 'm' is the name of a pointer to your map,
not of the map itself. I'll keep that assumption below.
{
a = iter->first;
b = iter->second;
if ( Test(a,b) == true)
iter->delete?();
}

How do I actually delete the element that iter is pointing to?


for(iter = m->begin(); iter != m->end(); ++iter)
{
if(condition)
iter = m->erase(iter);
}
An excellent reference for the C++ standard library:
www.josuttis.com/libbook

-Mike

Feb 21 '06 #2
Steve Edwards wrote:
After making an iterator to a map and stepping through a loop, I want to
delete any entries that satisfy a test from an external function.

MyMapType::cons t_iterator iter;

for(iter = m->begin(); iter != m->end(); ++iter)
{
a = iter->first;
b = iter->second;
if ( Test(a,b) == true)
iter->delete?();
}

How do I actually delete the element that iter is pointing to?


You ask your map to erase it.

Something like

m->erase(iter);

Beware, though, that after erasing the 'iter' becomes invalid. You cannot
increment it after that. You might want to rewrite your loop like this:

for (.... ; ) // no increment at the end
{
a =
b =
if (..
m->erase(iter++ );
else
++iter;
}

V
--
Please remove capital As from my address when replying by mail
Feb 21 '06 #3
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:mr******** *********@newsr ead1.mlpsca01.u s.to.verio.net. ..
Steve Edwards wrote:
How do I actually delete the element that iter is pointing to?


You ask your map to erase it.

Something like

m->erase(iter);

Beware, though, that after erasing the 'iter' becomes invalid. You cannot
increment it after that. You might want to rewrite your loop like this:


Well, I think we both botched our replies. I forgot the
'else' part, and yours disregards the invalid iterator. :-)

for (.... ; ) // no increment at the end
{
a =
b =
if (..
m->erase(iter++ );
iter = m->erase(iter);
else
++iter;
}


-Mike
Feb 21 '06 #4
Mike Wahler wrote:
if(condition)
iter = m->erase(iter);
IIRC, 'map::erase' has 'void' return type. Some Standard Library
implementations actually violate that to be consistent with other
(sequential) containers, and probably justifiedly, but generally
speaking your code is ill-formed.
}
An excellent reference for the C++ standard library:
www.josuttis.com/libbook


Yep. Check out table 6.31 in it.

V
--
Please remove capital As from my address when replying by mail
Feb 21 '06 #5
Mike Wahler wrote:
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:mr******** *********@newsr ead1.mlpsca01.u s.to.verio.net. ..
Steve Edwards wrote:
How do I actually delete the element that iter is pointing to?


You ask your map to erase it.

Something like

m->erase(iter);

Beware, though, that after erasing the 'iter' becomes invalid. You cannot
increment it after that. You might want to rewrite your loop like this:

Well, I think we both botched our replies. I forgot the
'else' part, and yours disregards the invalid iterator. :-)


Think again.

for (.... ; ) // no increment at the end
{
a =
b =
if (..
m->erase(iter++ );

iter = m->erase(iter);

else
++iter;
}

-Mike

--
Please remove capital As from my address when replying by mail
Feb 21 '06 #6

Beware, though, that after erasing the 'iter' becomes invalid. You cannot
increment it after that. You might want to rewrite your loop like this:

for (.... ; ) // no increment at the end
{
a =
b =
if (..
m->erase(iter++ );
else
++iter;
}

V


Thanks, but sorry, I don't quite understand. If you say the iterator
becomes invalid, should I break at that point. (in which case I haven't
processed all my elements yet.)
On the other hand, I can't continue if the iterator's invalid, can I?

Steve
Feb 21 '06 #7
In article <0m************ ****@newsread1. news.pas.earthl ink.net>,
"Mike Wahler" <mk******@mkwah ler.net> wrote:
"Steve Edwards" <gf*@lineone.ne t> wrote in message
news:gf******** *************** @news.btinterne t.com...
Hi,

After making an iterator to a map and stepping through a loop, I want to
delete any entries that satisfy a test from an external function.

MyMapType::cons t_iterator iter;

for(iter = m->begin(); iter != m->end(); ++iter)


This assumes that 'm' is the name of a pointer to your map,
not of the map itself. I'll keep that assumption below.
{
a = iter->first;
b = iter->second;
if ( Test(a,b) == true)
iter->delete?();
}

How do I actually delete the element that iter is pointing to?


for(iter = m->begin(); iter != m->end(); ++iter)
{
if(condition)
iter = m->erase(iter);
}
An excellent reference for the C++ standard library:
www.josuttis.com/libbook

-Mike


Thanks for the link, it's just what I need.

My compiler is complaining about "iter = m->erase(iter); "

(error: no match for 'operator = ')
Feb 21 '06 #8
TB
Steve Edwards skrev:
Beware, though, that after erasing the 'iter' becomes invalid. You cannot
increment it after that. You might want to rewrite your loop like this:

for (.... ; ) // no increment at the end
{
a =
b =
if (..
m->erase(iter++ );
else
++iter;
}

V


Thanks, but sorry, I don't quite understand. If you say the iterator
becomes invalid, should I break at that point. (in which case I haven't
processed all my elements yet.)
On the other hand, I can't continue if the iterator's invalid, can I?


The statement:

iter++;

increments the iterator and returns the previous position. Compare with

int x = 8, y = 0;
y = x++;
// y = 8, x == 9

so the following

m->erase(iter++ );

safely erases the desired element and retains a valid iterator pointing
to the next element or m->end().

--
TB @ SWEDEN
Feb 21 '06 #9
Steve Edwards wrote:
Beware, though, that after erasing the 'iter' becomes invalid. You cannot
increment it after that. You might want to rewrite your loop like this:

for (.... ; ) // no increment at the end
{
a =
b =
if (..
m->erase(iter++ );
else
++iter;
}

V

Thanks, but sorry, I don't quite understand. If you say the iterator
becomes invalid, should I break at that point. (in which case I haven't
processed all my elements yet.)


Why should you? By passing the value of the post-incremented iterator you
keep it valid. If the 'iter' isn't post-incremented, after 'erase' does
its job, 'iter' cannot be used -- it's invalid. In the proposed code, the
value of 'iter' is passed to 'erase', but _outside_ the object 'iter' is
already pointing to the next element of the map, and therefore is OK.
On the other hand, I can't continue if the iterator's invalid, can I?


No, if it's invalid, you can't. The whole point of using post-increment
is to keep it valid.

V
--
Please remove capital As from my address when replying by mail
Feb 21 '06 #10

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

Similar topics

11
39937
by: Markus Hämmerli | last post by:
I have seen in the STL that the map is working with one key. Does everyboby know if there is a possibility to have two key. Do you have a little example. Thanks Markus
1
4208
by: Florian Liefers | last post by:
"Hello World\n", i have the following problem: One of my headerfiles for a lib is including <vector>. When i compile the lib, everything is done well. In my application another file is including <map>. By linking my application and the lib, following errors occur: error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>
3
1420
by: Carl Youngblood | last post by:
I recognize that I may not be giving sufficient information with this, but in the interest of not bogging the list down with too much code, I'll try and post just a little information and then if that is insufficient I can post more. I have a C++ project that compiles fine with no warnings or errors. It is relying on STL classes where appropriate. In one of my classes, I needed to create a static member variable that was an STL map. By...
8
1475
by: Pierre Couderc | last post by:
I am looking for a "special" kind of map : - it is read like a map - if the searched element exists, it is given back imediately - if the searched element does not exist, an initialise() is called to do the job. - anyway the map has a limited size, when it is full, the oldest element is dropped ( if recalled later, will need again an initialise()) Is there some STL (or not) to do that?
19
6163
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
5
4334
by: Evgeny | last post by:
Hi, all! I try to convert my existing code written in VC6 to VC7 and have some problem with stl auto pointer template. class CColumn; #include <memory> typedef auto_ptr<CMyBase> CMyBasePtr; class CMyBase {
3
3505
by: Evgeny | last post by:
Hi, all! I didn't find yet solution for this problem! Somebody knows where is a catch? Looks like "operator =" or copy constructor not implemented in one of internal templates.... Thanks in advance class CMyBase;
4
11719
by: lada77 | last post by:
All, Just wondering if one of you very helpful guru's out there could comment on some odd behaviour I am seeing. I'm betting it is something obvious but I am not experienced enough to tell right away. Here is my code snippet and the results that I am seeing: #include <map> #include <iostream> int
4
12222
by: Maikeru | last post by:
I am trying to create a map that contains a string as the key and a list of strings as the value. Essentially it will be a dictionary where the key is an English word and the list will contain all Latin equivalents. My program reads from a file containing Latin words and their English equivalents. I am using the operator from map so that I can add to the list<string> should the Key already exist in my map. I am not sure of the syntax that I need...
0
8984
marktang
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...
0
9530
Oralloy
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...
0
9363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
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,...
0
8237
agi2029
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...
0
6073
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();...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
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
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.