bogus entries in map (std library)

S

suresh

Hi,

I am getting zero as value for some keys in my map which can never
happen as per my understanding of my code. These are the details:

I have defined an array of vectors, where each vector element is a
map, like this:

vector<map<string,int> entry>* fulldata ;

using new operator, fulldata is memory allocated and each vector's
size is made 10 by calling resize() function.

The maps are always updated using ++fulldata[j][key_string]; This
means that no key_string in the map can have corresponding zero
integer values.

But I get bogus entries in some maps with 0 values corresponding to
some valid key_string.

How can this happen? Interestingly this problem is noticed in the maps
stored in the 2nd position in the vector (for many vectors it is
seen). ie fulldata[2][key_string] is zero for many i values.

I am using debian etch with g++ version 4:4.1.1-15.

Thanks for any suggestions,

suresh
 
V

Victor Bazarov

suresh said:
I am getting zero as value for some keys in my map which can never
happen as per my understanding of my code. These are the details:

I have defined an array of vectors, where each vector element is a
map, like this:

vector<map<string,int> entry>* fulldata ;

using new operator, fulldata is memory allocated and each vector's
size is made 10 by calling resize() function.

The maps are always updated using ++fulldata[j][key_string]; This
means that no key_string in the map can have corresponding zero
integer values.

But I get bogus entries in some maps with 0 values corresponding to
some valid key_string.

How can this happen? Interestingly this problem is noticed in the maps
stored in the 2nd position in the vector (for many vectors it is
seen). ie fulldata[2][key_string] is zero for many i values.


How do you know that there are "bogus entries"? Did you know that
using indexing on a map inserts a default-initialized value into
the map with the key you use do index?

map<int, double> whoa;
if (whoa[42] == 0) {
std::cerr << "BOGUS!!!";
}

Is that what you're doing?

V
 
M

Mark P

suresh said:
Hi,

I am getting zero as value for some keys in my map which can never
happen as per my understanding of my code. These are the details:

I have defined an array of vectors, where each vector element is a
map, like this:

vector<map<string,int> entry>* fulldata ;

Syntactically invalid statement...
using new operator, fulldata is memory allocated and each vector's
size is made 10 by calling resize() function.

The maps are always updated using ++fulldata[j][key_string]; This
means that no key_string in the map can have corresponding zero
integer values.

But I get bogus entries in some maps with 0 values corresponding to
some valid key_string.

How can this happen? Interestingly this problem is noticed in the maps
stored in the 2nd position in the vector (for many vectors it is
seen). ie fulldata[2][key_string] is zero for many i values.

I am using debian etch with g++ version 4:4.1.1-15.

Thanks for any suggestions,

suresh


How can we help you if you don't post any code? (Although Victor's
guess is probably a good one given the uselessly little information
you've provided.)
 
S

suresh

Hi Victor

Thanks for the reply. I read Stroustrup carefully and wrote a small
piece of code to confirm the indexing behaviour. Thanks.

Mark, sorry for the cut paste error in my code. Actually it was
written like this:

typedef map<string,int> entry;
vector<entry>* fulldata ;

Anyway I learned maps better now. Thank you both,

regards
suresh



suresh said:
I am getting zero as value for some keys in my map which can never
happen as per my understanding of my code. These are the details:
I have defined an array of vectors, where each vector element is a
map, like this:
vector<map<string,int> entry>* fulldata ;

Syntactically invalid statement...




using new operator, fulldata is memory allocated and each vector's
size is made 10 by calling resize() function.
The maps are always updated using ++fulldata[j][key_string]; This
means that no key_string in the map can have corresponding zero
integer values.

But I get bogus entries in some maps with 0 values corresponding to
some valid key_string.
How can this happen? Interestingly this problem is noticed in the maps
stored in the 2nd position in the vector (for many vectors it is
seen). ie fulldata[2][key_string] is zero for many i values.

I am using debian etch with g++ version 4:4.1.1-15.
Thanks for any suggestions,

How can we help you if you don't post any code? (Although Victor's
guess is probably a good one given the uselessly little information
you've provided.)
 
R

Richard Herring

In message
Hi,

I am getting zero as value for some keys in my map which can never
happen as per my understanding of my code. These are the details:

I have defined an array of vectors, where each vector element is a
map, like this:

vector<map<string,int> entry>* fulldata ;

using new operator

Why not just use a vector of vectors?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top