title: Using vector of user defined class type objects
body: <p>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. </p>
  
  <p>Currently I have to use </p>
  
  <pre><code>vector&lt;grades&gt; students(10000); 
  </code></pre>
  
  <p>where 10000 is some max value of records I have assumed. 
  if some file has more records, it crashes obviously.</p>
  
  <p>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() ?</p>
  
  <p>Code below should explain - </p>
  
  <pre><code>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&lt;grades&gt; 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 &lt;&lt; "Error opening input data file:" &lt;&lt;argv[1] &lt;&lt; "...exiting\n";
              exit(-1);
          }
  
              while(!infile.eof()) 
              {
                   infile &gt;&gt; students[i].mName;//Can i use push_back() here and for reading all below entities, to make vector grow dynamically
                  infile &gt;&gt; students[i].mSurname;
                  infile &gt;&gt; students[i].mAge;
                  infile &gt;&gt; students[i].mLocation;
                  infile &gt;&gt; students[i].mMarks;
                  i++;
                  no_of_records++;
              }
  
              for(i=0;i&lt;no_of_records;i++)
              {
                 //Do some functions on each object students[i] in the vector 
              }
  }
  </code></pre>
  
  <p>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) :</p>
  
  <pre><code>Name1 Surname1 Course1 Marks1
  Name2 Surname2 Course2 Marks2
  Name3 Surname3 Course3 Marks3
  ...
  ...
  </code></pre>
  
  <p><strong>EDIT: code to handle all kinds of spurious spaces, tabs in records entities</strong></p>
  
  <pre><code>while(!infile.eof()) 
      {
          c=infile.get();     
          if(isalnum(c))
          {
              infile.seekg(-1,ios::cur);
          }
  
          if(isalnum(c))
          {
              grades stud_temp;
  
              infile &gt;&gt; stud_temp.mName;
              infile &gt;&gt; stud_temp.mSurname;
              infile &gt;&gt; stud_temp.mAge;
              infile &gt;&gt; stud_temp.mLocation;
              infile &gt;&gt; stud_temp.mMarks;         
              students.push_back(stud_temp);          
          }
      }
  </code></pre>