Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The definition of map is:<br> <b>map&lt;Key, Data, Compare, Alloc&gt;</b></p> <p>Where the last two template parameters default too:<br> Compare:&nbsp;less&lt;Key&gt;<br> Alloc:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;allocator&lt;value_type&gt;<br></p> <p>When inserting new values into a map. The new value (valueToInsert) is compared against the old values in order (<b>N.B.</b> This is not sequential search, the standard guarantees a max insert complexity of O(log(N)) ) until Compare(value,ValueToInsert) returns true. Because you are using <b><i>'const char*'</i></b> as the key. The Compare Object is using <b><i>less&lt;const char*&gt;</i></b> this class just does a &lt; on the two values. So in effect you are comparing the pointer values (not the string) therefore the order is random (as you don't know where the compiler will put strings.</p> <p>There are two possible solutions:</p> <ul> <li>Change the type of the key so that it compares the string values.</li> <li>Define another Compare Type that does what you need.</li> </ul> <p>Personally I (like Chris) would just use a std::string because &lt; operator used on strings returns a comparison based on the string content. But for arguments sake we can just define a Compare type.</p> <pre><code>struct StringLess { bool operator()(const char* const&amp; left,const char* const&amp; right) const { return strcmp(left,right) &lt; 0; } }; /// typedef std::map&lt;const char*, int,StringLess&gt; TMyMap; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload