1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
struct point { point( int x = 0, int y = 0 ) : x(x), y(y) {} int x ; int y ; };
std::ostream& operator<< ( std::ostream& stm, point pt )
{ return stm << '(' << pt.x << ',' << pt.y << ')' ; }
std::istream& operator>> ( std::istream& stm, point& pt )
{
int x, y ;
if( stm.ignore( 1000, '(' ) && // extract and discard characters till a '(' has been extracted and discarded
stm >> x && // read an integer into x
stm.ignore( 1000, ',' ) && // extract and discard characters till a ',' has been extracted and discarded
stm >> y && // read an integer into y
stm.ignore( 1000, ')' ) && // extract and discard characters till a ')' has been extracted and discarded
!stm.eof() // eof was not reached before the ')' was extracted
)
{
pt = { x, y } ; // success, update pt with x, y
}
else { pt = {} ; stm.clear(stm.failbit) ; } // input failed: clear pt, and put stream into a failed state
return stm ;
}
|