471,831 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,831 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 8371
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Randall Smith | last post: by
2 posts views Thread by martin | last post: by
7 posts views Thread by Jacob JKW | last post: by
13 posts views Thread by stef mientki | last post: by
Kelicula
3 posts views Thread by Kelicula | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by

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.