Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Using a char pointer as a key like you are above is almost certainly not what you want to do.</strong></p> <p>STL containers deal with stored values, in the case of <code>std::unordered_map&lt;char *, unsigned int, ...&gt;</code>, you are dealing with pointers to c strings, which may not even be around on subsequent insertion/removal checks.</p> <p>Note that your <code>my_unordered_map</code> is a global variable but you are trying to insert local char arrays a, b, and c. What do you expect your comparison function <code>my_equal_to()</code> to <code>strcmp()</code> when the inserted c strings fall out of scope? (You suddenly have keys pointing to random garbage that can be compared to newly inserted future values.)</p> <p>It is important that STL map keys be copyable values that cannot have their meanings changed by external program behavior. <strong>You should almost certainly use <code>std::string</code> or similar for your key values, even if their construction seems wasteful to you at first glance.</strong></p> <p>The following will work exactly as you intend things to work above, and is vastly safer:</p> <pre><code>#include &lt;unordered_map&gt; #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; // STL containers use copy semantics, so don't use pointers for keys!! typedef unordered_map&lt;std::string, unsigned int&gt; my_unordered_map; my_unordered_map location_map; int main() { char a[10] = "ab"; location_map.insert(my_unordered_map::value_type(a, 10)); char b[10] = "abc"; location_map.insert(my_unordered_map::value_type(b, 20)); char c[10] = "abc"; location_map.insert(my_unordered_map::value_type(c, 20)); cout &lt;&lt; "map size: " &lt;&lt; location_map.size() &lt;&lt; endl; my_unordered_map::iterator it; if ((it = location_map.find("abc")) != location_map.end()) { cout &lt;&lt; "found \"" &lt;&lt; it-&gt;first &lt;&lt; "\": " &lt;&lt; it-&gt;second &lt;&lt; endl; } return 0; } </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