Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A <code>vector&lt;vector&lt;int&gt; &gt;</code> (note the space in the <code>&gt; &gt;</code>) can work well, but it's not necessarily the most efficient way to do things. Another that can work quite nicely is a wrapper around a single vector, that keeps track of the "shape" of the matrix being represented, and provides a function or overloaded operator to access the data:</p> <pre><code>template &lt;class T&gt; class matrix { int columns_; std::vector&lt;T&gt; data; public: matrix(int columns, int rows) : columns_(columns), data(columns*rows) {} T &amp;operator()(int column, int row) { return data[row*columns_+column]; } }; </code></pre> <p>Note that the C++ standard only allows <code>operator[]</code> to take a single operand, so you can't use it for this job, at least directly. In the example above, I've (obviously enough) used <code>operator()</code> instead, so subscripts look more like Fortran or BASIC than you're accustomed to in C++. If you're really set on using <code>[]</code> notation, you can do it anyway, though it's mildly tricky (you overload it in the matrix class to return a proxy, then have the proxy class also overload <code>operator[]</code> to return (a reference to) the correct element -- it's mildly ugly internally, but works perfectly well anyway).</p> <p>Edit: Since I have it lying around, here's an example of how to implement the latter. I wrote this (quite a while) before most compilers included <code>std::vector</code>, so it statically allocates an array instead of using a vector. It's also for the 3D case (so there are two levels of proxies involved), but with a bit of luck, the basic idea comes through anyway:</p> <pre><code>template&lt;class T, int size&gt; class matrix3 { T data[size][size][size]; friend class proxy; friend class proxy2; class proxy { matrix3 &amp;m_; int index1_, index2_; public: proxy(matrix3 &amp;m, int i1, int i2) : m_(m), index1_(i1), index2_(i2) {} T &amp;operator[](int index3) { return m_.data[index1_][index2_][index3]; } }; class proxy2 { matrix3 &amp;m_; int index_; public: proxy2(matrix3 &amp;m, int d) : m_(m), index_(d) { } proxy operator[](int index2) { return proxy(m_, index_, index2); } }; public: proxy2 operator[](int index) { return proxy2(*this, index); } }; </code></pre> <p>Using this, you can address the array with the normal C++ syntax, such as:</p> <pre><code>matrix3&lt;double, size&gt; m; for (int x=0; x&lt;size; x++) for (int y = 0; y&lt;size; y++) for (int z = 0; z&lt;size; z++) m[x][y][z] = x*100 + y * 10 + z; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload