Using vector of user defined class type objects

后端 未结 1 1425
名媛妹妹
名媛妹妹 2021-01-15 16:18

Working on solving a simple problem in C++, want to understand how I could use vector of objects of user defined class type without fixed(worst case) number of elements allo

相关标签:
1条回答
  • You can just declare your vector first:

    vector<grades> students;
    

    And then read the values, while pushing elements in the vector:

    while(!infile.eof()) 
    {
        grades student;
        infile >> student.mName;
        infile >> student.mSurname;
        infile >> student.mAge;
        infile >> student.mLocation;
        infile >> student.mMarks;
        students.push_back(student);
    }
    

    You don't need no_of_records anymore, since the number of records will be students.size().

    0 讨论(0)
提交回复
热议问题