title: Standard Practice for Creating a "Vector of References" Using Only the Standard Libraries
body: <p>I would like to create an object, put the object into a <code>vector</code>, and still be able to modify the same object by accessing only the <code>vector</code>. However, I understand that when an object is <code>push_back()</code> to a <code>vector</code>, the object is actually copied into the <code>vector</code>. As a result, accessing the object in the <code>vector</code> will merely access a similar, but different object.</p>
  
  <p>I have a beginner's knowledge in C, so I know that I can create a pointer to the object, and make a vector of pointers. e.g. <code>vector&lt;Object *&gt;</code>. However, it seems as if <a href="https://softwareengineering.stackexchange.com/a/163403">pointers</a> are <a href="https://softwareengineering.stackexchange.com/a/163279">discouraged</a> in C++, and <a href="https://stackoverflow.com/a/7058373/4833127">references are preferred</a>. Yet, <a href="https://stackoverflow.com/questions/922360/why-cant-i-make-a-vector-of-references">I cannot make a vector of references</a>.</p>
  
  <p>I wish to use only the standard libraries, so <code>boost</code> is off limits to me. </p>
  
  <p>I heard of smart pointers. However, it appears as if there are multiple types of smart pointers. Would it not be overkill for this purpose? If smart pointers are indeed the answer, then how do I determine which one to use?</p>
  
  <p>So my question is: <strong>What is the standard practice for creating a vector of references/pointers to objects?</strong></p>
  
  <p>In other words, would like the below (pseudo-)code to work.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  class Object 
  {
  public:
      int field;
  };
  
  vector&lt;Object&gt; addToVector(Object &amp;o)
  {
      vector&lt;Object&gt; v;
      v.push_back(o);
      v[0].field = 3; // I want this to set o.field to 3.
  
      return v;
  }
  
  int main()
  {
      Object one;
      one.field = 1;
      cout &lt;&lt; one.field &lt;&lt; endl; // 1 as expected
  
      Object &amp;refone = one;
      refone.field = 2;
      cout &lt;&lt; one.field &lt;&lt; endl; // 2 as expected
  
      vector&lt;Object&gt; v = addToVector(one);
  
      cout &lt;&lt; v[0].field &lt;&lt; endl; // 3 as expected
  
      cout &lt;&lt; one.field &lt;&lt; endl; // I want to get 3 here as well, as opposed to 2.
  
  
      return 0;
  }
  </code></pre>