473,899 Members | 2,952 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
11 24435
Steve Edwards wrote:
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.btinter net.com...
[..]
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);
}
[..]


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


Of course it does. 'm->erase(iter)' returns 'void'. Mike was too hasty
to suggest that solution. It would work for a 'list', for example. It
just doesn't work for 'map'.

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

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:VJ******** ********@newsre ad1.mlpsca01.us .to.verio.net.. .
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.


OK I did. I see I wasn't thinking before. :-)

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

iter = m->erase(iter);


You've already pointed out my error with this line elsethread.

My excuse is ... we've got a new dog, she ate my attention. :-)

-Mike
Feb 23 '06 #12

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

Similar topics

11
39962
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
4214
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
1427
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
1479
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
6201
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
4342
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
3511
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
11742
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
12341
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
9997
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
11276
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...
1
10976
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
9671
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...
1
8043
isladogs
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...
0
7204
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
5891
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...
0
6082
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.