Help...

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

I hate to post a snippet, but as usual things work fine when they're
on their own...

map<string,uint> mPInfo;
vector<string> vLayovers; // populated elsewhere
const char* array[elem_count]; // populated elsewhere

for( unsigned int i=0; i < elem_count; ++i ) {
printf( "### Adding %s to map", array );
mPInfo[string(array)]=i;
}
for( vector<const string>::iterator i=vLayovers.begin();
i != vLayovers.end(); ++i ) {
printf( "*** %s %s", i->c_str(), mPInfo.count(*i)?"found":"not found" );
}

Why the heck would I get output like

*** ATL found
### Adding SAN to map
*** ATL found
### Adding SAN to map
*** ATL found

? The map has one item in it, and it is not the item I'm searching
for. Why the heck would it find something that isn't there? Please,
please tell me that I'm doing something stupid here...
 
C

Christopher Benson-Manica

Christopher Benson-Manica said:
for( unsigned int i=0; i < elem_count; ++i ) {
printf( "### Adding %s to map", array );
mPInfo[string(array)]=i;
}
for( vector<const string>::iterator i=vLayovers.begin();
i != vLayovers.end(); ++i ) {

if( mPInfo.empty() ) {
printf( "This is ridiculous!!\n" );
}
printf( "*** %s %s", i->c_str(), mPInfo.count(*i)?"found":"not found" );
}

And of course, the "This is ridiculous!!" line gets printed right
before mPInfo.count(*i) still manages to find something in an EMPTY
MAP. *sigh*
 
C

Christopher Benson-Manica

Christopher Benson-Manica said:
for( vector<const string>::iterator i=vLayovers.begin();
i != vLayovers.end(); ++i ) {
printf( "*** %s %s", i->c_str(), mPInfo.count(*i)?"found":"not found" );
}
Why the heck would I get output like

Well, it turns out that I was trying to print out the value at the
element in the map with

printf( "%u\n", mPInfo[*i] );

and I suppose that created the element for me... I'm sorry to have
posted my earlier drivel, and would now just like to know why maps
beahve like this.
 
R

Rob Williscroft

Christopher Benson-Manica wrote in in
comp.lang.c++:
Christopher Benson-Manica said:
for( unsigned int i=0; i < elem_count; ++i ) {
printf( "### Adding %s to map", array );
mPInfo[string(array)]=i;
}
for( vector<const string>::iterator i=vLayovers.begin();
i != vLayovers.end(); ++i ) {

if( mPInfo.empty() ) {
printf( "This is ridiculous!!\n" );
}
printf( "*** %s %s", i->c_str(), mPInfo.count(*i)?"found":"not
found" );
}

And of course, the "This is ridiculous!!" line gets printed right
before mPInfo.count(*i) still manages to find something in an EMPTY
MAP. *sigh*


Most probably you've corrupted memory at some earlier point in
your programme and only in the snippet you've posted is the
problem becoming manafest.

Though it is possible your std::map implementation is broken:

What does mPInfo.begin() == mPInfo.end() return ?

What does mPInfo.size() return ?

If either value is inconsistant with mPInfo.empty() then it
becomes more likely that std::map is at fault. It doesn't
rule out memory coruption though :(.

As always try to put your code thorough a different compiler
or where possible STL implementation (http://www.stlport.org).

Rob.
 
J

John Harrison

Christopher Benson-Manica said:
Christopher Benson-Manica said:
for( vector<const string>::iterator i=vLayovers.begin();
i != vLayovers.end(); ++i ) {
printf( "*** %s %s", i->c_str(), mPInfo.count(*i)?"found":"not found" );
}
Why the heck would I get output like

Well, it turns out that I was trying to print out the value at the
element in the map with

printf( "%u\n", mPInfo[*i] );

and I suppose that created the element for me... I'm sorry to have
posted my earlier drivel, and would now just like to know why maps
beahve like this.

operator[] on a map returns a reference to a map element value so that it
can be used on the lhs of assignment. IOW if the element does not exist then
it must create one in order to return that reference. For the same reason
you cannot call operator[] on a const map.

operator[] is really just a convenience function, use insert if you want to
avoid this behaviour.

john
 
J

John Harrison

operator[] is really just a convenience function, use insert if you want to
avoid this behaviour.

I meant use find, find will find a key without adding anything if it doesn't
exist.

john
 

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
474,169
Messages
2,570,918
Members
47,458
Latest member
Chris#

Latest Threads

Top