title: push_back a struct into a vector in a struct
body: <p>I have the following structs:</p>
  
  <pre><code>enum messageType {New = 0, Old = 1, No_Message = 2};
  
  typedef struct {
      enum messageType type;
      unsigned int senderID;
      char message[100];
  } StoredMessageData;
  
  struct StoredMessage {
      unsigned int recipientID;
      vector&lt;StoredMessageData&gt; messages;
  
      StoredMessage(const unsigned int&amp; intRecipient = 0, const vector&lt;StoredMessageData&gt;&amp; data = vector&lt;StoredMessageData&gt;())
      : recipientID(intRecipient), messages(data)
      {
          messages.reserve(10);
      }
  
      bool operator&lt;(const StoredMessage&amp; compareTo) const
      {
          return recipientID &lt; compareTo.recipientID;
      }
  
      bool operator==(const StoredMessage&amp; compareTo) const
      {
          return recipientID == compareTo.recipientID;
      }
  };
  </code></pre>
  
  <p>In my main method, at some point I receive a message and want to store it like so:</p>
  
  <pre><code>if(msgs.find(rcvdRecipientID) == msgs.end())
  {
      StoredMessage storedMsg;
      storedMsg.recipientID = rcvdRecipientID;
      msgs.insert(storedMsg);
  }
  
  set&lt;StoredMessage&gt;::iterator it = msgs.find(rcvdRecipientID);
  StoredMessage storedMsg = *it;
  
  StoredMessageData data;
  data.type = New;
  data.senderID = rcvdSenderID;
  strcpy(data.message, rcvdMessage);
  
  storedMsg.messages.push_back(data);
  </code></pre>
  
  <p>If, after push_back(), I call storedMsg.messages.size(), I am given a value of 1. This makes sense to me.</p>
  
  <p>However, later, I wish to know how many messages I stored, so, this code:</p>
  
  <pre><code>StoredMessage storedMsg;
  if(msgs.find(rcvdSenderID) != msgs.end())
  {
      storedMsg = *(msgs.find(rcvdSenderID));
      cout &lt;&lt; "Number of stored messages to send: " &lt;&lt; int(storedMsg.messages.size()) &lt;&lt; endl &lt;&lt; endl;
      ...
  </code></pre>
  
  <p>Here, storedMsg.messages.size() returns 0 even when the same ID as before is used... I am confused as to why this is happening and suspect it has to do with the vectors being copied as their size varies, but I'm not sure. I am not a C++ expert, so please be gentle.
  Thank you!</p>