Note that there are some explanatory texts on larger screens.

plurals
  1. POPrint struct element of vector
    primarykey
    data
    text
    <p>I am learning about vector. I try to implement a code that print the struct element of a vector as displayed below. Many resources in the internet only teach me a simple vector. I get stcuk in expression when to print it. However, any suggestion for improving the quality and elegance of the code is open, although the change is fundamental (in struct or looping).</p> <p>Thank you very much.</p> <pre><code>#include "stdafx.h" #include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; typedef struct _student { string name; int age; vector &lt;string&gt; subject; }student; int _tmain(int argc, _TCHAR* argv[]) { vector &lt;student&gt; x; //assmue at this point we do not know the number of students student y; //and I want to insert new information 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); //get new information again //and I want to insert new information y.name ="Bon"; y.age =12; y.subject.push_back("history"); y.subject.push_back("physics"); x.push_back(y); // then I want display all data cout &lt;&lt; "myvector contains:"; for (int i=0; i&lt;x.size(); i++) { cout &lt;&lt; "Student # " &lt;&lt; i+1 &lt;&lt;endl; cout &lt;&lt; " name : " &lt;&lt; x.at(i).name &lt;&lt;endl; //Reference in the internet only display a simple vector -- cout &lt;&lt; " age : " &lt;&lt; x.at(i).age &lt;&lt;endl; //I get stuck to express this and next part cout &lt;&lt;" Subject : "; for (int j =0; j &lt; x.at(i).subject.size(); j++) { cout &lt;&lt; x.at(i).subject.at(j); } cout &lt;&lt; endl; cin.get(); return 0; } </code></pre>
    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.
 

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