473,385 Members | 1,337 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,385 software developers and data experts.

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::iterator 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 8470
On 13 Sep 2006 01:20:38 -0700, Su*****@gmail.com 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::iterator 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.com 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::iterator 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.com 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::iterator 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
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...
4
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...
0
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,...
2
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...
2
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...
7
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...
13
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)'...
1
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...
3
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.