title: Line 933: Char 34: runtime error: reference binding to null pointer of type 'struct value_type' (stl_vector.h) - leet code spiral
body: <p>I tried to solve Leetcode Problem 54 - spiral 
  and got stuck in empty vector input. </p>
  
  <p>the question is about spiral list. input is 2d vector and output should be vector list which written by spiral direction. </p>
  
  <pre><code>Input:
  [
    [1, 2, 3, 4],
    [5, 6, 7, 8],
    [9,10,11,12]
  ]
  Output: [1,2,3,4,8,12,11,10,9,5,6,7]
  </code></pre>
  
  <p>problem is when input is empty list. </p>
  
  <pre><code>Input: [] 
  </code></pre>
  
  <p>it produces runtime error. </p>
  
  <p>another testcases passed except empty input like <b>[] </b>.</p>
  
  <p>It seems no runtime error during testing in my mac OSX terminal, 
  but Leetcode says </p>
  
  <p><b>
      'Line 933: Char 34: runtime error: reference binding to null pointer of type 'struct value_type' (stl_vector.h)
      '
  </b></p>
  
  <p>Here is link 
  <b><a href="https://leetcode.com/problems/spiral-matrix/" rel="nofollow noreferrer">https://leetcode.com/problems/spiral-matrix/</a></b> </p>
  
  <p>Also I attach codes below ... </p>
  
  <pre><code>class Solution {
  public:
  
  vector&lt;int&gt; answer;
  int left = 0, right = 0; 
  vector&lt;int&gt; spiralOrder(vector&lt;vector&lt;int&gt;&gt;&amp; matrix) {
      if(matrix[0].size()&lt;1) return {};
      vector&lt;vector&lt;int&gt;&gt; flag(matrix.size(),vector&lt;int&gt;(matrix[0].size(),0));
          while(1){
             flag[left][right] =1;
             answer.push_back(matrix[left][right]);
  
             if(right+1&lt;matrix[0].size() &amp;&amp; flag[left][right+1] == 0){
                 ++right;
                 continue;
             }
             else if(left+1&lt;matrix.size() &amp;&amp; flag[left+1][right] == 0 ){
                 ++left;
                 continue; 
             }
             else if(right-1&gt;=0 &amp;&amp; flag[left][right-1]==0){
                 --right;
                 continue;
             }
             else if(left-1&gt;=0  &amp;&amp; flag[left-1][right]==0){
                 --left;
                 continue;
             }
             else break;
          }
      return answer; 
  }
  };
  </code></pre>