Replace vector of vector with flat memory structure : <p>I have the following type:</p>

<pre><code>std::vector&lt;std::vector&lt;int&gt;&gt; indicies
</code></pre>

<p>where the size of the inner vector is always 2. The problem is, that vectors are non-contiguous in memory. I would like to replace the inner vector with something contiguous so that I can cast the flattened array:</p>

<pre><code>int *array_a = (int *) &amp;(a[0][0])
</code></pre>

<p>It would be nice if the new type has the [] operator, so that I don't have to change the whole code. (I could also implement it myself if necessary). My ideas are either:</p>

<pre><code>std::vector&lt;std::array&lt;int, 2&gt;&gt;
</code></pre>

<p>or </p>

<pre><code>std::vector&lt;std::pair&lt;int, int&gt;&gt;
</code></pre>

<p>How do these look in memory? I wrote a small test:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;array&gt;
#include &lt;vector&gt;
int main(int argc, char *argv[])
{
    using namespace std;

    vector&lt;array&lt;int, 2&gt;&gt; a(100);

    cout &lt;&lt; sizeof(array&lt;int, 2&gt;) &lt;&lt; endl;

    for(auto i = 0; i &lt; 10; i++){
        for(auto j = 0; j &lt; 2; j++){
            cout &lt;&lt; "a[" &lt;&lt; i &lt;&lt; "][" &lt;&lt; j &lt;&lt; "] " 
                &lt;&lt;&amp;(a[i][j]) &lt;&lt; endl;
        }
    }
    return 0;
}
</code></pre>

<p>which results in:</p>

<pre><code>8
a[0][0] 0x1b72c20
a[0][1] 0x1b72c24
a[1][0] 0x1b72c28
a[1][1] 0x1b72c2c
a[2][0] 0x1b72c30
a[2][1] 0x1b72c34
a[3][0] 0x1b72c38
a[3][1] 0x1b72c3c
a[4][0] 0x1b72c40
a[4][1] 0x1b72c44
a[5][0] 0x1b72c48
a[5][1] 0x1b72c4c
a[6][0] 0x1b72c50
a[6][1] 0x1b72c54
a[7][0] 0x1b72c58
a[7][1] 0x1b72c5c
a[8][0] 0x1b72c60
a[8][1] 0x1b72c64
a[9][0] 0x1b72c68
a[9][1] 0x1b72c6c
</code></pre>

<p>It seems to work in this case. Is this behavior in the standard or just a lucky coincidence? Is there a better way to do this?</p>
