1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
//=====================================
struct Student
{
string id;
string name;
vector<int> scores;
};
istream &operator >> ( istream &strm, Student &student )
{
string line;
getline( strm, line );
stringstream ss( line );
getline( ss >> ws, student.id, ',' );
getline( ss >> ws, student.name, ',' );
student.scores.clear();
int mark;
char comma;
ss >> mark; student.scores.push_back( mark );
while ( ss >> comma >> mark ) student.scores.push_back( mark );
return strm;
}
ostream &operator << ( ostream &strm, const Student &student )
{
strm << "id: " << student.id << " name: " << student.name << " scores: ";
for ( int mark : student.scores ) strm << mark << " ";
return strm;
}
//=====================================
int main()
{
vector<Student> students;
// fstream in( "data.txt" );
stringstream in( "3450, Guido VanRossum, 10, 15, 14, 18, 20 \n"
"6120, Yukihiro Matsumoto, 10, 9, 13, 12, 14 \n"
"9230, James Gosling, 10, 16, 13, 12, 10 \n" );
Student stud;
while ( in >> stud ) students.push_back( stud );
cout << "The following students were read:\n";
for ( Student s : students ) cout << s << '\n';
}
| |