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
|
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
struct director
{
director( std::string name = "", int age = 0, std::string country = "" )
: name(name), age(age), country(country) {}
std::string name ;
int age = 0 ;
std::string country ;
};
std::istream& operator>> ( std::istream& stm, director& dir )
{
std::string name ;
int age ;
std::string country ;
// read name, read age, throw away the new line, read country
if( std::getline( stm, name ) && stm >> age && stm.ignore(1000,'\n') && std::getline(stm,country) )
dir = { name, age, country } ;
else dir = {} ;
return stm ;
}
std::ostream& operator<< ( std::ostream& stm, const director& dir )
{ return stm << "name: " << dir.name << "\nage: " << dir.age << "\ncountry: " << dir.country ; }
int main()
{
std::istringstream file(
"John\n"
"45\n"
"England\n"
"Mark\n"
"53\n"
"Belgium\n"
);
std::vector<director> directors ;
director d ;
while( file >> d ) directors.push_back(d) ;
for( const director& d : directors ) std::cout << d << "\n--------\n" ;
}
|