title: Weird iterator behaviour + segfault with unordered_set
body: <p>I have a class that has an <code>unordered_set&lt;int&gt;</code> member as follows:</p>
  
  <p>I have the following class definition followed by its regular &amp; copy constructors, as well as some other function that modifies the set (deleted irrelevant code segments since the class is very long):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;unordered_set&gt;
  #include &lt;random&gt;
  
  class HexBoard {
    public:
      HexBoard(int n);
      HexBoard(const HexBoard &amp;obj);
      std::unordered_set&lt;int&gt; emptyPositions();
    private:
      std::unordered_set&lt;int&gt; empty_positions;
  };
  
  HexBoard::HexBoard(int n) {
      for (int i = 0; i &lt; n * n; i++) {
          empty_positions.insert(i);
      }
  }
  
  HexBoard::HexBoard(const HexBoard &amp;obj) : empty_positions(obj.empty_positions) {};
  
  void HexBoard::placeStone(int i) {
      checkBounds(i); // raises an error if i &gt;= n 
      empty_positions.erase(i);
  }
  
  std::unordered_set&lt;int&gt; HexBoard::emptyPositions() {
      return empty_positions;
  }
  </code></pre>
  
  <p>I have a different class that contains an instance of this HexBoard. It has a function that will copy that board into a different variable using the copy constructor:</p>
  
  <pre><code>class Game {
    public:
      Game(HexBoard::HexBoard *board) : board(board) {};
    private:
      HexBoard *board;
      void monteCarlo(int position);
  };
  
  void Game::monteCarlo(int position) {
  
      HexBoard *another_board = new HexBoard(*board);
  
      int count = 0;
      while (count &lt; 5) {
          count++;
  
          std::uniform_int_distribution&lt;unsigned&gt; dis(
              0, another_board-&gt;emptyPositions().size() - 1
          );
  
          std::cout &lt;&lt; "Empty positons:\n";
          for (const auto&amp; pos : another_board-&gt;emptyPositions()) {
              std::cout &lt;&lt; pos &lt;&lt; " ";
          }
          std::cout &lt;&lt; "\n";
  
          int n = dis(gen);
          std::cout &lt;&lt; "Picked random n: " &lt;&lt; n &lt;&lt; "\n";
  
          auto it = another_board-&gt;emptyPositions().begin();
          std::cout &lt;&lt; "it begin: " &lt;&lt; *it &lt;&lt; "\n";
          std::advance(it, n);
          std::cout &lt;&lt; "it advance: " &lt;&lt; *it &lt;&lt; "\n";
          int absolute_position = *it;
          std::cout &lt;&lt; "picked " &lt;&lt; absolute_position &lt;&lt; "\n";
      }
  }
  </code></pre>
  
  <p>In the <code>monteCarlo</code> function, let's say the contents of <code>emptyPositions</code> set were initially <code>8, 7, 6, 5, 4, 3, 2, 1</code>, the stdout output of this function is usually:</p>
  
  <pre><code>Empty positons:
  8 7 6 5 4 3 2 1
  Picked random n: 4
  it begin: 2
  Segmentation fault: 11
  </code></pre>
  
  <p>Why does this segfault? I understand that there is some iterator subtly with regards to the <code>empty_positions.erase(i);</code> line, but even when I comment this out, I get the same behaviour.</p>
  
  <p>I've also added this right after the <code>Picked random n</code> stdout and this segfaults as well (output below it):</p>
  
  <pre><code>std::cout &lt;&lt; "set buckets contain:\n";
  for ( unsigned i = 0; i &lt; ai_board-&gt;emptyPositions().bucket_count(); ++i) {
      std::cout &lt;&lt; "bucket #" &lt;&lt; i &lt;&lt; " contains:";
      for ( auto j = ai_board-&gt;emptyPositions().begin(i);
            j != ai_board-&gt;emptyPositions().end(i); ++j)
          std::cout &lt;&lt; " " &lt;&lt; *j;
      std::cout &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>set buckets contain:
  Segmentation fault: 11
  </code></pre>
  
  <p>The segfault happens at <code>std::advance(it, n);</code> and at this last manual iteration.</p>
  
  <p>I'd appreciate any help.</p>
  
  <p>Thanks</p>