[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ Programming ➜ STL ➜ Checking values in std::map

Checking values in std::map

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Samson   USA  (683 posts)  Bio
Date Fri 12 Jan 2007 03:40 AM (UTC)

Amended on Fri 12 Jan 2007 03:41 AM (UTC) by Samson

Message

CMDF( do_maptest )
{
   map<int,int> numbers;
   map<int,int>::iterator inum;

   numbers[0] = 1;
   numbers[1] = 2;
   numbers[2] = 3;
   numbers[3] = 4;
   numbers[4] = 5;

   int count = 0;
   for( inum = numbers.begin(); inum != numbers.end(); ++inum )
   {
      ++count;
      ch->printf( "Number %d:%d\r\n", inum->first, inum->second );
   }
   ch->printf( "Count #1: %d\r\n", count );

   for( int x = 0; x < 900; ++x )
   {
      if( numbers[x] == 3 )
         ch->print( "Number is 3!!\r\n" );
   }

   count = 0;
   for( inum = numbers.begin(); inum != numbers.end(); ++inum )
   {
      ++count;
      ch->printf( "Number %d:%d\r\n", inum->first, inum->second );
   }
   ch->printf( "Count #2: %d\r\n", count );
}


Darien told me that checking a std::map for a certain value, like so: if( numbers[x] == 0 ) would cause the map to throw a default value into the map at key[x]. I didn't believe him because it sounded silly, but the above function when added as a test command to my codebase returned a Count #2 value of 900, instead of the expected 5.

Is this normal expected behavior for a std::map or is this a compiler bug that needs to be reported to GNU?
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Fri 12 Jan 2007 03:53 AM (UTC)
Message
Yes, this is quite normal. The reason is that you need to create something so that you can make creating assignments.

Consider e.g.
for (int i = 0; i < 100; i++) {
  m[i] = true;
}


If it didn't create some value to return a reference for, that code would not work. Of course, this has the unfortunate consequence of the behavior you are observing.

The real way to test for presence is:
if (m.find(123) == m.end()) {
  // not present
}
else {
  // present
}


If you also want the element, you do:

map<int,bool>::iterator it;
it = m.find(123);
if (it != m.end()) {
  cout << it->first << ":" << it->second << endl;
}


(The exact syntax might not be that; it might be it->left and it->right, or something similar. But that's the basic idea.)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,094 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]