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

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  STL
. . -> [Subject]  Checking values in std::map
Home  |  Users  |  Search  |  FAQ
Register forum user name
Forgotten password?
(New message)
Subject: Checking values in std::map
Your forum user name.
Register forum user name
Your forum password.
Forgotten password?
Message to be posted (in English, please)
Maximum of 6000 characters. Text only please, no HTML.
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...

Subject review (reverse sequence)

Posted by David Haley   USA  (3,881 posts)  [Biography] bio   Moderator
Date Fri 12 Jan 2007 03:53 AM (UTC)  quote  ]
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

[Go to top] top

Posted by Samson   USA  (683 posts)  [Biography] bio
Date Fri 12 Jan 2007 03:40 AM (UTC)  quote  ]

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


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 )
      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 )
      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?

SmaugMuds.org: http://www.smaugmuds.org - The Smaug MUDs Community Center

"The past was erased, the erasure was forgotten, the lie became truth." -- George Orwell, 1984
[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.


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


Written by Nick Gammon - 5K

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

[Best viewed with any browser - 2K]    [Web site powered by FutureQuest.Net]