title: How to use a 2d vector of pointers
body: <p>What is the correct way to implement an efficient 2d vector? I need to store a set of Item objects in a 2d collection, that is fast to iterate (most important) and also fast to find elements.</p>
  
  <p>I have a 2d vector of pointers declared as follows:</p>
  
  <pre><code>std::vector&lt;std::vector&lt;Item*&gt;&gt; * items;
  </code></pre>
  
  <p>In the constructor, I instantiate it as follows:</p>
  
  <pre><code>items = new std::vector&lt;std::vector&lt;Item*&gt;&gt;();
  items-&gt;resize(10, std::vector&lt;Item*&gt;(10, new Item()));
  </code></pre>
  
  <p>I how do I (correctly) implement methods for accessing items? Eg:</p>
  
  <pre><code>items[3][4] = new Item();
  
  AddItem(Item *&amp; item, int x, int y)
  {
       items[x][y] = item;
  }
  </code></pre>
  
  <p>My reasoning for using pointers is for better performance, so that I can pass things around by reference. </p>
  
  <p>If there is a better way to go about this, please explain, however I would still be interested in how to correctly use the vector.</p>
  
  <p>Edit: For clarification, this is part of a class that is for inventory management in a simple game. The set 10x10 vector represents the inventory grid which is a set size. The Item class contains the item type, a pointer to an image in the resource manager, stack size etc. </p>
  
  <p>My pointer usage was in an attempt to improve performance, since this class is iterated and used to render the whole inventory every frame, using the image pointer.</p>