Question : Understanding the push_back method of putting information into vectors...

Let's say I want to test scores with a student.  I created a struct like so:

struct StudentInfo
{
      CString csName;
      int nTestScore;
}

vector <StudentInfo> vClassScores;

Now,  let's say I wish to enter in data for a particular student.  He has 3 test scores I'd like to add to a vector vClassScores.  George scored a 95, 91, and 86.

in the main:

StudentInfo siWinterExams;

siWinterExams.csName = "George";
siWinterExams.nTestScore = 95;
vClassInfo.push_back(siWinterExams);

siWinterExams.nTestScore = 91;
vClassInfo.push_back(siWinterExams);

siWinterExams.nTestScore = 86;
vClassInfo.push_back(siWinterExams);

My question is...do I have to repeat "siWinterExams.csName = "George"" for ALL 3 entries if I want the name George to be associated with his 3 scores?  Or will it "remember" that I haven't changed the name yet....

Answer : Understanding the push_back method of putting information into vectors...

>> do I have to repeat "siWinterExams.csName = "George"" for ALL 3 entries if I want the name George to be associated with his 3 scores?

No. What's stored in the vector are three separate copies of the siWinterExams struct, each taken at a different instant. All three will have the same csName, but a different nTestScore.
Random Solutions  
 
programming4us programming4us