473,748 Members | 3,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Iterate through an empty STL Map

Hi All,

I'm having trouble with iterating through an STL map.
Firstly, for an empty map, will map.begin() be equal to map.end()?
If this is true, the program should not be entering the while loop to
delete the map's contents...
But, it does, thus causing a segmentation fault when I try to delete..

Is this hapening because the map is empty?

I'm running CentOS, using g++ (GCC) 3.4.5 20051201 on linux kernel
2.6.9-34.EL

This is the code:
#include <stdio.h>
#include <map>
using namespace std;

typedef struct
{
char *data;
}data_type;
typedef map<int ,data_type* map_type;

int main()
{
map_type my_map;
map_type::itera tor it = my_map.begin();
data_type *data_item;
while(it != my_map.end());
{
data_item = it->second;
delete data_item->data;
delete data_item;
printf("\nPass" );
it++;
}
my_map.clear();
}

Sep 13 '06 #1
3 8496
On 13 Sep 2006 01:20:38 -0700, Su*****@gmail.c om wrote:
>Hi All,

I'm having trouble with iterating through an STL map.
Firstly, for an empty map, will map.begin() be equal to map.end()?
If this is true, the program should not be entering the while loop to
delete the map's contents...
But, it does, thus causing a segmentation fault when I try to delete..

Is this hapening because the map is empty?
<...>
>
int main()
{
map_type my_map;
map_type::itera tor it = my_map.begin();
data_type *data_item;
while(it != my_map.end());
Look at the semicolon at the end of while: it should not be there!
{
data_item = it->second;
delete data_item->data;
delete data_item;
printf("\nPass" );
it++;
}
my_map.clear();
}
Best regards,

Zara
Sep 13 '06 #2
Su*****@gmail.c om wrote:
#include <stdio.h>
#include <map>
using namespace std;

typedef struct
{
char *data;
}data_type;
typedef map<int ,data_type**map _type;

int main()
{
map_type my_map;
map_type::itera tor it = my_map.begin();
data_type *data_item;
while(it != my_map.end());
you have a bad ";" in the above line.
{
data_item = it->second;
delete data_item->data;
delete data_item;
printf("\nPass" );
it++;
}
my_map.clear();
}

Best

Kai-Uwe Bux
Sep 13 '06 #3
Kai-Uwe Bux wrote:
Su*****@gmail.c om wrote:
>#include <stdio.h>
#include <map>
using namespace std;

typedef struct
{
char *data;
}data_type;
typedef map<int ,data_type* map_type;

int main()
{
map_type my_map;
map_type::iter ator it = my_map.begin();
data_type *data_item;
while(it != my_map.end());

you have a bad ";" in the above line.
>{
data_item = it->second;
delete data_item->data;
delete data_item;
printf("\nPass ");
it++;
}
my_map.clear() ;
}

BTW, don't sweat that error. I did the exact same thing in the first
program I wrote as a professional... . I still tell that story.
Sep 13 '06 #4

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

Similar topics

4
1477
by: Randall Smith | last post by:
In the case I know how may times I want to iterate, one way to do it is like this: for i in range(100000): dothis() I like how clean this syntax is, but this method is very wasteful. The list created by range(100000) consumes a fair amount of memory and it is only used to iterate.
4
3644
by: Julian Yap | last post by:
Hi all, I'm trying to get some ideas on the best way to do this. In this particular coding snippet, I was thinking of creating a dictionary of file objects and file names. These would be optional files that I could open and parse. At the end, I would easily close off the files by iterating through the dictionary. ---< CODE FOLLOWS >--- optionalfiles = {fileAreaCode: "areacode.11", fileBuild: "build.11"}
0
1373
by: Yogi_Bear_79 | last post by:
I have a form with 30 text boxes on it. They are set up in three columns, and 10 rows. Each row equals one record in the array. What I want to do is start by reading the first box txtColumnA_1, if it is not empty proceed into a loop. Inside the loop assign txtColumnA_1 as the first element of the array, txtColumnB_1 as the second element, and txtColumnC_3 as the third. Then loop again and check ColumnA_2, if it is empty stop, else repeat...
2
2183
by: martin | last post by:
hi, I am using vb.net. I have wrote a data access class and one of my methods returns a dataset. I would like to iterate through this, although this is proving problematic for me. Can anybody please point out the addition that I would need to the follwoing code.
2
1955
by: James Doran | last post by:
Hello, I'd like to iterate through each Page of my ASP.NET project from within a Custom web control and access the Page.Controls collection. I've tried using Reflection on the web project assembly, but the controls collection count was zero. While this method gives details of the private controls in each page, I need access to actual instances of the controls to interrogate their individual properties.
7
7421
by: Jacob JKW | last post by:
I need to iterate over combinations of n array elements taken r at a time. Because the value of r may vary quite a bit between program invocations, I'd like to avoid simply hardcoding r loops. I assume the best way to do this would be either using closures or creating some sort of iterator class. Any guidance on how to get started? Thanks,
13
2410
by: stef mientki | last post by:
hello, I generate dynamically a sequence of values, but this "sequence" could also have length 1 or even length 0. So I get some line in the form of: line = '(2,3,4)' line = '' line = '(2)' (in fact these are not constant numbers, but all kind of integer
1
2369
by: prathna | last post by:
Hi .. I have a logic:iterate tag which will display 5 rows each row with a drop downlist and 2 textfields.now by default all the rows will be shown.how do i hide all the rows except the first one.i know how to show/hide a row when its not dynamic.but i dont know how to do it when its logic iterate tag. <logic:iterate indexId="index" id="phoneItem" name="nameForm" property="results"> <% String dropdownType = "results.dropdownType";...
3
6887
Kelicula
by: Kelicula | last post by:
Hi all, I am usually a Perl programmer, I have some background in javascript and am attempting to create a Googleish selector div. Please bear with me, excuse the long introduction... Here's the details: Our site filters content by location. I use zip codes to find radius's. So if a user wants to see content from a location that they don't know the zip code for, I have set up this neat little input field that allows you to type the...
0
8823
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
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
9238
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 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...
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
4593
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
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.