title: Using std::map should be deterministic or not?
body: <p>I'm facing a strange behaviour using Intel C++ compiler 2019 update 5. When I fill a <code>std::map</code> it seems to lead to a non deterministic (?) result. The <em>stl</em> is from VS2019 16.1.6 in which ICC is embedded. I am on Windows 10.0.17134.286.</p>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;map&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  
  std::map&lt;int, int&gt; AddToMapWithDependencyBetweenElementsInLoop(const std::vector&lt;int&gt;&amp; values)
  {
      std::map&lt;int, int&gt;  myMap;
      for (int i = 0; i &lt; values.size(); i+=3)
      {
          myMap.insert(std::make_pair(values[i], myMap.size()));
          myMap.insert(std::make_pair(values[i + 1], myMap.size()));
          myMap.insert(std::make_pair(values[i + 2], myMap.size()));
      }
      return myMap;
  }
  
  std::map&lt;int, int&gt; AddToMapOnePerLoop(const std::vector&lt;int&gt;&amp; values)
  {
      std::map&lt;int, int&gt;  myMap;
      for (int i = 0; i &lt; values.size(); ++i)
      {
          myMap.insert(std::make_pair(values[i], 0));
      }
      return myMap;
  }
  
  int main()
  {
      std::vector&lt;int&gt; values{ 6, 7,  15, 5,  4,  12, 13, 16, 11, 10, 9,  14, 0,  1,  2,  3,  8,  17 };
  
      {
          auto myMap = AddToMapWithDependencyBetweenElementsInLoop(values);
          for (const auto&amp; keyValuePair : myMap)
          {
              std::cout &lt;&lt; keyValuePair.first &lt;&lt; ", ";
          }
          std::cout &lt;&lt; std::endl;
      }
  
      {
          auto myMap = AddToMapOnePerLoop(values);
          for (const auto&amp; keyValuePair : myMap)
          {
              std::cout &lt;&lt; keyValuePair.first &lt;&lt; ", ";
          }
          std::cout &lt;&lt; std::endl;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>I simply wanted to perform a test so I call directly <em>icl</em> from the command line:</p>
  
  <pre><code>$ icl /nologo mycode.cpp
  $ mycode.exe
  0, 1, 2, 3, 4, 5, 6, 7, 11, 12, 13, 14, 15, 16, 17,
  0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17
  </code></pre>
  
  <p>Curious. I expected to have 18 entries and I got 15 and 14 (depending on the insertion method, see the code).</p>
  
  <pre><code>$ icl /nologo /EHsc mycode.cpp
  $ mycode.exe
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17,
  0, 1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17
  </code></pre>
  
  <p>Still curious, now I got 17 and 14 entries rather than 18 and 18!</p>
  
  <pre><code>$ icl /nologo /Od mycode.cpp
  $ mycode.exe
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
  </code></pre>
  
  <p>Now, with no optimization, I got 18/18, as expected.</p>
  
  <p>My question is two-fold: 1) is it normal to get such results and 2) if it's not (what I suspect) what did I do wrong? I tought a simple call to the compiler would call the <code>std::map::insert()</code> function correctly?</p>
  
  <p>Does the problem lies in the <code>for(){}</code>???</p>
  
  <p>Thanks for helping me understanding this problem and finding a solution!</p>