title: Flatten n-dimensional vector
body: <p>I worked out an algorithm that can be used recursively, which works in a non-recursive manner, however, I cannot test it in recursive form because I cannot create a n-1 dimensional variable from the template.<br/>For help I write down the variable names and its meaning:</p>
  
  <ul>
  <li><code>v</code>: the n-dimensional vector</li>
  <li><code>dims</code>: n long vector with every dimension (e.g: if it was an array looking like <code>int x[3][6][4]</code>, then <code>dims</code> looks like: {3, 6, 4})</li>
  <li><code>p</code>: product of the dimensions, used to get the size of the flat vector (e.g: 3*6*4)</li>
  <li><code>ret</code>: the returned flat vector</li>
  <li><code>sub_dims</code>: same as <code>dims</code> execpt without the first dimension (e.g: {6, 4})</li>
  <li><code>sub_p</code>: same as <code>p</code> except without the first factor (e.g: 6*4)</li>
  <li><code>sub_ret</code>: the returned flat vector for the n-1 dimensional vector</li>
  </ul>
  
  <p>the code:</p>
  
  <pre><code>template &lt;typename T&gt;
  vector&lt;int&gt; dim_flat(vector&lt;T&gt; v, vector&lt;int&gt; dims)
  {
      // creating variables and vectors
      int n = dims.size();
      int p = dims[0];
      for (int i = 1; i &lt; n; i++)
      {
          p *= dims[i];
      }
      int sub_p = p / dims[0];
      vector&lt;int&gt; sub_dims;
      sub_dims.assign(dims.begin() + 1, dims.end());
      vector&lt;int&gt; ret(p);
  
      // algorithm
      if (n &gt; 1)
      {
          for (int i = 0; i &lt; dims[0]; i++)
          {
              vector&lt;int&gt; sub_ret = dim_flat(v[i], sub_dims);
  
              for (int j = 0; j &lt; sub_p; j++)
              {
                  ret[i * sub_p + j] = sub_ret[j];
              }
          }
      }
      else
      {
          // case for the input is 1D 
          // not yet written
      }
  
      return ret;
  }
  </code></pre>
  
  <p>With this code the project build, however if I call it in main:</p>
  
  <pre><code>vector&lt;int&gt; ret = dim_flat(v, dims);
  </code></pre>
  
  <p>where <code>v</code> is e.g. an 4D vector and <code>dims</code> is a vector containing {3, 3, 3, 3}, then I get the following when trying to build:</p>
  
  <pre><code>error C2784: 'std::vector&lt;_Ty&gt; dim_flat(std::vector&lt;T&gt;,std::vector&lt;_Ty&gt;)' : could not deduce template argument for 'std::vector&lt;T&gt;' from 'int'
  </code></pre>
  
  <p>for the line</p>
  
  <pre><code>vector&lt;int&gt; sub_ret = dim_flat(v[i], sub_dims);
  </code></pre>
  
  <p>I kinda (but not really) understand the meaning of this error, so I expected the same to happen, which it did for this:</p>
  
  <pre><code>T x = v[i];
  vector&lt;int&gt; sub_ret = dim_flat(x, sub_dims);
  </code></pre>
  
  <p>This is the part where I don't really understand the error anymore, because I thought that with the argument <code>vector&lt;T&gt; v</code> I specified that the input e.g. 4D vector will be understood as a vector of <code>T</code> where <code>T</code> is a 3D vector that can be also indexed since its a <code>vector&lt;T&gt;</code>. So following this logic I thought that if I give the recursion the first <code>T</code> being a 3D vector, then a step deeper this 3D vector will now be understood as a vector of <code>T'</code> where <code>T'</code> is a 2D vector, and so on.</p>
  
  <p>Clearly either my logic is flawed, or I used the wrong method (, or both), so the question is: How can I solve / fix this?</p>
  
  <p>EDIT:<br/>Credit to Max66 for the solution.<br/>
  The code probably could be more optimized, but at least now it works.<br/>
  Code:</p>
  
  <pre><code>//// for integer only
  // case input is 0-dimensional (simply a variable)
  template &lt;typename T&gt;
  vector&lt;int&gt; dim_flat (const T &amp;v, const vector&lt;int&gt; &amp;dims)
  {
      return vector&lt;int&gt;(1, v);
  }
  
  // case input is n-dimensional
  template &lt;typename T&gt;
  vector&lt;int&gt; dim_flat(const vector&lt;T&gt; &amp;v, const vector&lt;int&gt; &amp;dims)
  {
      // creating variables and vectors
      int n = dims.size();
      int p = dims[0];
      for (int i = 1; i &lt; n; i++)
      {
          p *= dims[i];
      }
      int sub_p = p / dims[0];
      vector&lt;int&gt; sub_dims;
      sub_dims.assign(dims.begin() + 1, dims.end());
      vector&lt;int&gt; ret(p);
  
      // algorithm
      if (n &gt; 1) // case n-dimensional
      {
          for (int i = 0; i &lt; dims[0]; i++)
          {
              vector&lt;int&gt; sub_ret = dim_flat(v[i], sub_dims);
              for (int j = 0; j &lt; sub_p; j++)
              {
                  ret[i * sub_p + j] = sub_ret[j];
              }
          }
      }
      else // case 1-dimensional
      {
          for (int i = 0; i &lt; p; i++)
          {
              vector&lt;int&gt; sub_ret = dim_flat(v[i], sub_dims);
              ret[i] = sub_ret[0];
          }
      }
      return ret;
  }
  </code></pre>