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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  STL
. . -> [Subject]  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] Refresh page


Posted by Samson   USA  (683 posts)  [Biography] 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?
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] 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
[Go to top] 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.


12,519 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] 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]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]