Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here, added some comments and stuff. Not sure if this is what you were looking for, but here it is.</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; // string would be welcome here! struct _student // the typedef thing is not necessary in C++ { std::string name; // i find this "using namespace ..." thing a bad habit, it can make code harder to read int age; std::vector&lt;std::string&gt; subject; }; int _tmain(int argc, _TCHAR* argv[]) { std::vector&lt;student&gt; x; student y; size_t size; // calling vector.size() every iterations is a bad idea, performance-wise size_t size_subj; // same y.name = "John"; y.age = 9; y.subject.push_back("biology"); y.subject.push_back("math"); y.subject.push_back("art"); x.push_back(y); y.name = "Bon"; y.age = 12; y.subject.clear(); // clear subjects of the other student y.subject.push_back("history"); y.subject.push_back("physics"); x.push_back(y); std::cout &lt;&lt; "my vector contains:"; for (int i = 0, size = x.size(); i &lt; size; ++i) { size_subj = x[i].subject.size(); // I prefer using operator[] when I'm sure nothing can go wrong std::cout &lt;&lt; "Student # " &lt;&lt; i + 1 &lt;&lt;endl; std::cout &lt;&lt; "\tname: " &lt;&lt; x[i].name &lt;&lt;endl; std::cout &lt;&lt; "\tage: " &lt;&lt; x[i].age &lt;&lt;endl; std::cout &lt;&lt; "\tSubjects: "; for (int j = 0; j &lt; size_subj; ++j) std::cout &lt;&lt; x[i].subject[j]; std::cout &lt;&lt; endl; } return 0; } </code></pre> <p>Finally, using a std::vector&lt; std::string* > or std::vector&lt; std::string&amp; > could be a better idea performance-wise, depending on what you are planning to do with it later.</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