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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
struct Student
{
// I don't know anything about your StudentName, EmailAddress, and Age objects.
// So I just used standard things here.
std::string id;
std::string first_name;
std::string surname;
std::string email;
int age;
// Here is where we construct a student's information from a string
Student( const std::string& s ) { from_stream( std::istringstream( s ) ); }
Student( const char* s ) { *this = std::string( s ); } // be explict due to a Clang++ bug
// Here is where we assign a student's information from a string
Student& operator = ( const std::string& s ) { from_stream( std::istringstream( s ) ); return *this; }
Student& operator = ( const char* s ) { return *this = std::string( s ); } // be explict due to a Clang++ bug
// Here is where we extract a student's information from a stream
std::istream& from_stream( std::istream& ins )
{
getline( ins, id, ',' );
getline( ins, first_name, ',' );
getline( ins, surname, ',' );
getline( ins, email, ',' );
return ins >> age;
}
// Here is where we insert a student's information into a stream
std::ostream& to_stream( std::ostream& outs ) const
{
return outs
<< id << ","
<< first_name << ","
<< surname << ","
<< email << ","
<< age;
}
// Here are some convenient overloads for rvalue stuff that we use above
std::istream& from_stream( std::istream&& ins ) { return from_stream( std::forward <std::istream&> ( ins ) ); }
std::ostream& to_stream ( std::ostream&& outs ) const { return to_stream ( std::forward <std::ostream&> ( outs ) ); }
};
// Stream extraction operator for Student
std::istream& operator >> ( std::istream& ins, Student& student )
{
return student.from_stream( ins );
}
// Stream insertion operator for Student
std::ostream& operator << ( std::ostream& outs, const Student& student )
{
return student.to_stream( outs );
}
int main()
{
// Here we create an array of Students -- IN SPACE! FROM STRINGS!
Student students[] =
{
"Student1,Adam,Smith,ASmith@gmail.com,20",
"Student2,Erick,Smith,ESmith@gmail.com,19",
};
// Prove it!
for (const auto& student : students)
std::cout
<< student.first_name << " "
<< student.surname << " | "
<< student.email << "\n";
}
|