Here is a working example with the data format you gave:
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Student
{
public:
Student() : _Name("") {}
~Student() {}
void setName(const string& name) { _Name = name; }
string getName() const { return _Name; }
void setGrades(const vector& grades) { _Grades.assign(grades.begin(), grades.end()); }
vector getGrades() const { return _Grades; }
private:
string _Name;
vector _Grades;
};
std::ostream& operator<<(std::ostream& os, const Student& s)
{
os << s.getName() << " "; // NOTE: older versions of STL will require a character buffer instead
const vector grades = s.getGrades();
copy(grades.begin(), grades.end(), ostream_iterator(os, " "));
os << std::endl;
return os;
}
std::istream& operator>>(std::istream& is, Student& s)
{
string name = ""; // NOTE: older versions of STL will require a character buffer instead
is >> name;
string sGrades = "";
getline(is, sGrades);
vector grades;
stringstream ss(sGrades);
copy(istream_iterator(ss), istream_iterator(), back_inserter(grades));
s.setName(name);
s.setGrades(grades);
return is;
}
int main()
{
ifstream fin;
vector students;
fin.open("data.txt");
copy(istream_iterator(fin), istream_iterator(), back_inserter(students));
fin.close();
// do whatever you want with students vector
std::copy(students.begin(), students.end(), ostream_iterator(cout, "\n\n"));
}
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac