Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can make a 2D vector. Reference: <a href="http://en.cppreference.com/w/cpp/container/vector" rel="nofollow"><code>std::vector</code></a>:</p> <pre><code>std::vector&lt;std::vector&lt;MyObject&gt;&gt; objects; </code></pre> <p>But before using this object you should have made this object.</p> <pre><code>// you know the size of your 2D : MaxRow, MaxCol std::vector&lt;std::vector&lt;MyObject&gt; &gt; objects(MaxRow, std::vector&lt;MyObject&gt;(MaxCol, MyObject()); </code></pre> <p>Or you can </p> <pre><code>std::vector&lt;std::vector&lt;MyObject&gt; &gt; objects; for(size_t r_i=0; r_i&lt;MaxRow; ++r_i) { // create and push a row in the 2D matrix. objects.push_back(std::vector&lt;MyObject&gt;()); for(size_t c_i=0; c_i&lt;MaxCol; ++c_i) { // push a col in the new created row. objects[r_i].push_back(MyObject()); } } </code></pre> <p>Now you want to access your matrix.</p> <pre><code>size_t row, col; // now you know your row and col : input or from anywhere </code></pre> <p>Lets say that you have a function <code>callObject</code> in <code>MyObject</code> class.</p> <pre><code>// you can do this. objects[row][col].callObject(); </code></pre> <hr> <p>But if you want to store sparse matrices, then you can do</p> <pre><code>std::vector&lt;std::vector&lt;MyObject*&gt;objects(MaxRow, std::vector&lt;MyObject&gt;(MaxCol, nullptr); objects[2][3] = new MyObject(/*your arguments*/); objects[2][3].callObject(); objects[0][0].callObject(); // Boom !!! Null pointer here. </code></pre> <p>With Pointers use, <a href="http://en.cppreference.com/w/cpp/memory/unique_ptr" rel="nofollow">std::unique_ptr</a> or <a href="http://en.cppreference.com/w/cpp/memory/shared_ptr" rel="nofollow">std::shared_ptr</a>.</p> <hr> <p>If you want a Vector2D class you can use it wrap around this vector of vectors.</p> <pre><code> typedef MyObject T; class Vector2D { std::vector&lt;std::vector&lt;T&gt; &gt; objects; public: // initialize the objects with MaxRow rows each with MaxCol cols. Vector2D(size_t MaxRow, size_t MaxCol); // remember that there should already be somebody at this row or col // or you have to create all the rows before this row and all the cols before // this col in this row. void addItem(size_t row, size_t col, const MyObject &amp; obj); }; </code></pre> <p>I used <code>typedef int T</code> because it would be easier for changing to templates.</p> <hr> <p>As i said before, it depends a lot on what you want and your data is. if its kind of sparse, i would have used vector of vector of pointers.</p> <pre><code> typedef MyObject T; class Vector2D { std::vector&lt;std::vector&lt;std::unique_ptr&lt;T&gt; &gt; &gt; objects; public: // initialize the Nullptr with MaxRow rows each with MaxCol cols. Vector2D(size_t MaxRow, size_t MaxCol); // now you can just add the item, as pointer is already there. void addItem(size_t row, size_t col, MyObject * obj); }; </code></pre> <p>Using unique_ptr as it takes care of deletion and other referencing things.</p> <hr> <p>You could have "got a lot of other things" by inheriting <code>std::vector&lt; std::vector&lt;T&gt; &gt;</code> in Vector2D, but composition is better than inheritance in most cases.</p>
 

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