goldenmean
goldenmean

Reputation: 19036

Using vector of user defined class type objects

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 allocated while instantiating that vector of objects. i.e.

Currently I have to use

vector<grades> students(10000); 

where 10000 is some max value of records I have assumed. if some file has more records, it crashes obviously.

So in case of code below, how to grow the vector of my class objects dynamically and read the records in my class variables. I cannot use push_back(), as explained in code below. Or how can i use push_back() ?

Code below should explain -

class grades
{
public:
    string mName;
    string mSurname;
    int mAge;
    string mLocation;
    int mMarks;
    char mGrade;

    //void readdata();
    void calcgrade();
    void showgrade(string name_surname);

};

    using namespace std;
    int main(int argc,char *argv[])
    {
        **vector<grades> students(10000);// I do not want to do this but make it dynamic**
        ifstream infile;
        char c;
        int i=0;
        int no_of_records=0;

        infile.open(argv[1],ios::in);
        if(infile.is_open() != true)
        {
            cerr << "Error opening input data file:" <<argv[1] << "...exiting\n";
            exit(-1);
        }

            while(!infile.eof()) 
            {
                 infile >> students[i].mName;//Can i use push_back() here and for reading all below entities, to make vector grow dynamically
                infile >> students[i].mSurname;
                infile >> students[i].mAge;
                infile >> students[i].mLocation;
                infile >> students[i].mMarks;
                i++;
                no_of_records++;
            }

            for(i=0;i<no_of_records;i++)
            {
               //Do some functions on each object students[i] in the vector 
            }
}

FYI:- I read a text file in C++ which has entries as below(The order of entities and number of different types of entities in one row is fixed which I know but the number of rows can vary based on different input file to be read) :

Name1 Surname1 Course1 Marks1
Name2 Surname2 Course2 Marks2
Name3 Surname3 Course3 Marks3
...
...

EDIT: code to handle all kinds of spurious spaces, tabs in records entities

while(!infile.eof()) 
    {
        c=infile.get();     
        if(isalnum(c))
        {
            infile.seekg(-1,ios::cur);
        }

        if(isalnum(c))
        {
            grades stud_temp;

            infile >> stud_temp.mName;
            infile >> stud_temp.mSurname;
            infile >> stud_temp.mAge;
            infile >> stud_temp.mLocation;
            infile >> stud_temp.mMarks;         
            students.push_back(stud_temp);          
        }
    }

Upvotes: 3

Views: 19610

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258678

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().

Upvotes: 8

Related Questions