Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've already gotten a reasonably direct answer to your question. Based on what you seem to be trying to accomplish, however, it seems to me that a less direct answer may really be a better one.</p> <p>At least as I read your description, you have a number of unique students, and a number of courses for each. When a student has completed a course, you want to look for the student. If they're not in the collection, add them. Then add the data for the course they completed.</p> <p>That being the case, a vector strikes me as a less than ideal solution. You could implement code a couple of different ways, but I'd probably do it like this:</p> <pre><code>struct course { std::string Quarter_, Course_, Credits_, Grade_; using std::string; course(string const &amp;q, string const &amp;c, string const &amp;cr, string const &amp;g) : Quarter_(q), Course_(c), Credits_(cr), Grade_(g) {} }; std::map&lt;std::string, std::vector&lt;course&gt; &gt; m_students; </code></pre> <p>Using this, your entire sequence to look up a student, insert a new student if there isn't one by that name, then adding the course work to the (new or existing) student's record would work out as:</p> <pre><code>m_students[strName].push_back(course(strQuarter, strCourse, strCredits, strGrade)); </code></pre> <p>Getting back to your original question, the standard containers are intended to work with <em>values</em>. You pass a value to them, and they store a copy of that value. One consequence of that is that anything like <code>push_back(new XXX)</code> is essentially <em>always</em> a mistake (pretty much a guaranteed memory leak). If you have an object, just pass it. If you don't, just create a temporary and pass that. In Java (for one example) seeing <code>new XXX</code> all over the place is routine and nearly unavoidable. While you <em>can</em> write C++ that way as well, it's not something you should expect as a rule.</p>
    singulars
    1. This table or related slice is empty.
    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