Accessing elements of a vector of ints by raw pointers
<p>I wonder if the code below is legal.</p>

<p>Basically I have a <code>std::vector&lt;int&gt;</code> and I have a legacy function that processes an array of <code>int</code>s. As the elements of a <code>std::vector</code> are always contiguous the code should always work (it actually works on my implementation), but it still seems bit of a hack to me.</p>

<pre><code>#include &lt;vector&gt;
#include &lt;iostream&gt;

void LecagyFunction(int *data, int length)
{
  for (int i = 0; i &lt; length; i++)
    std::cout &lt;&lt; data[i] &lt;&lt; std::endl;
}

int main()
{
  std::vector&lt;int&gt; vector;
  vector.push_back(5);
  vector.push_back(4);
  vector.push_back(3);
  LecagyFunction(&amp;vector[0], vector.size());
}
</code></pre>

<p>The output as expected is:</p>

<pre><code>5
4
3
</code></pre>