++ , , . , , - ++ ++, . - . - - . -, , , , - . ( ) . ++: 10.2 : , , int, char *, complex Employee_record . . 10.3 : , . 10.4 : , , int , , . , , . 10.5 : ++ - (cout), (cin) (cerr). - . . 10.6 - : printf <stdio.h> <iostream.h> ++. , - , , , . , , . -, - . , , ; , ++. <iostream.h> . <stream.h>. , <iostream.h> , <stream.h> , , . , , , . , - , , , , , . 10.2  , , . : put(cerr,"x = "); // cerr - put(cerr,x); put(cerr,'\n'); . , , . << , "" ("put to"), , : cerr << "x = " << x << '\n'; cerr . , int 123, x = 123 . , complex (1,2.4), x = (1,2.4) cerr. x , <<, << . , , , . << ? (. 7.2). , , . , = , cout=a=b cout=(a=b). < >, " " " ", - . << >> . , "" "". , << , : cout << "a*b+c=" << a*b+c << '\n'; , : cout << "a^b|c=" << (a^b|c) << '\n'; , , , : cout << "a<<b=" << (a<<b) << '\n'; 10.2.1  ostream << (): class ostream : public virtual ios { // ... public: ostream& operator<<(const char*); // ostream& operator<<(char); ostream& operator<<(short i) { return *this << int(i); } ostream& operator<<(int); ostream& operator<<(long); ostream& operator<<(double); ostream& operator<<(const void*); // // ... }; , ostream operator<<() . operator<< ostream, , operator<<. , int, cerr << "x = " << x; (cerr.operator<<("x = ")).operator<<(x); , , , : - . ostream::operator<<(int) , ostream::operator<<(char) - . void val(char c) { cout << "int('"<< c <<"') = " << int(c) << '\n'; } main() { val('A'); val('Z'); } int('A') = 65 int('Z') = 90 ASCII, . , char, cout<<'Z' Z, 90. ostream::operator<<(const void*) , . main() { int i = 0; int* p = new int(1); cout << "local " << &i << ", free store " << p << '\n'; } , , local 0x7fffead0, free store 0x500c . ios 10.4.1. 10.2.2  : class complex { double re, im; public: complex(double r = 0, double i = 0) { re=r; im=i; } friend double real(complex& a) { return a.re; } friend double imag(complex& a) { return a.im; } friend complex operator+(complex, complex); friend complex operator-(complex, complex); friend complex operator*(complex, complex); friend complex operator/(complex, complex); //... }; complex << : ostream& operator<<(ostream&s, complex z) { return s << '(' real(z) << ',' << imag(z) << ')'; }; operator<< . , main() { complex x(1,2); cout << "x = " << x << '\n'; } x = (1,2) ostream, , . , ostream , , , . , . ostream, . 10.3  . istream, >> (" " - "input from") . operator>>. 10.3.1  istream : class istream : public virtual ios { //... public: istream& operator>>(char*); // istream& operator>>(char&); // istream& operator>>(short&); istream& operator>>(int&); istream& operator>>(long&); istream& operator>>(float&); istream& operator>>(double&); //... }; operator>> : istream& istream::operator>>(T& tvar) { // // - T `tvar' return *this; } VECTOR , , : int readints(Vector<int>& v) // { for (int i = 0; i<v.size(); i++) { if (cin>>v[i]) continue; return i; } // Vector // } , int, , . , 1 2 3 4 5. 6 7 8. readints() 1 2 3 4 5 , . , , , .. , , , . isspace() <ctype.h>. get(): class istream : public virtual ios { //... istream& get(char& c); // istream& get(char* p, int n, char ='n'); // }; , . istream::get(char&) . : main() { char c; while (cin.get(c)) cout << c; } , >> put(), : main() { char c; while (cin.get(c)) cout.put(c); } istream::get() n , p. get() , ( ), 0, n, n-1 . , . get() , : void f() { char buf[100]; cin >> buf; // cin.get(buf,100,'\n'); // //... } cin>>buf , 99 . , . : void f() { char buf[100]; cin.get(buf,100,'\n'); // char c; if (cin.get(c) && c!='\n') { // , } //... } , get() unsigned char. <ctype.h> , : int isalpha(char) // 'a'..'z' 'A'..'Z' int isupper(char) // 'A'..'Z' int islower(char) // 'a'..'z' int isdigit(char) // '0'..'9' int isxdigit(char) // '0'..'9' 'a'..'f' 'A'..'F' int isspace(char) // ' ' '\t' // int iscntrl(char) // // (ASCII 0..31 127) int ispunct(char) // , int isalnum(char) // isalpha() | isdigit() int isprint(char) // : ascii ' '..'~' int isgraph(char) // isalpha() | isdigit() | ispunct() int isascii(char c) { return 0<=c && c<=127; } , isascii(), , . (('a'<=c && c<='z') || ('A'<=c && c<='Z')) // , ( EBCDIC ), isalpha(), . eatwhite(), : istream& eatwhite(istream& is) { char c; while (is.get(c)) { if (isspace(c)==0) { is.putback(c); break; } } return is; } putback(), , . 10.3.2  (istream ostream) . . ios: class ios { //ios ostream istream //... public: int eof() const; // int fail() const; // int bad() const; // int good() const; // //... }; , good() eof(). good(), , . , good(), . v, v ( , v , istream ostream). , fail() bad() , . fail(), , , ; bad() . , , ios: class ios { //... public: enum io_state { goodbit=0, eofbit=1, filebit=2, badbit=4, }; //... }; , , . : switch (cin.rdstate()) { case ios::goodbit: // cin break; case ios::eofbit: // break; case ios::filebit: // // break; case ios::badbit: // cin break; } . , ios. , : const int _good = ios::goodbit; const int _bad = ios::badbit; const int _file = ios::filebit; const int _eof = ios::eofbit; typedef ios::io_state state_value ; , . , , , ios::goodbit ios::io_state. , << >>, : while (cin>>z) cout << z << '\n'; , , (.. 0) good(). istream, cin>>z. , , . (7.3.2). , z , (.. , ). z complex, , 10.2.2 10.2.3, . : complex z; iocopy(z,cin,cout); // complex double d; iocopy(d,cin,cout); // double char c; iocopy(c,cin,cout); // char - , , . , . - , , -. 10.3.3  , , , , : istream& operator>>(istream& s, complex& a) /* input complex; "f" float: f ( f ) ( f , f ) */ { double re = 0, im = 0; char c = 0; s >> c; if (c == '(') { s >> re >> c; if (c == ',') s >> im >> c; if (c != ')') s.clear(ios::badbit); // } else { s.putback(c); s >> re; } if (s) a = complex(re,im); return s; } , , . , , '(', . , a . , , clear() ( clear - , ), good(); ios::clear() ios::goodbit. 10.4  10.2 , , , . . , , , , . . 10.4.1 ios - ios, ostream istream. istream ostream , -. ios : . , ios , (, ), .., , . class ios { //... public: ostream* tie(ostream* s); // input output ostream* tie(); // "tie" int width(int w); // width int width() const; char fill(char); // char fill() const; // long flags(long f); long flags() const; long setf(long setbits, long field); long setf(long); long unsetf(long); int precision(int); // float int precision() const; int rdstate(); const; // , . $$10.3.2 int eof() const; int fail() const; int bad() const; int good() const; void clear(int i=0); //... }; 10.3.2 , , . 10.4.1.1  tie() ostream istream. : main() { String s; cout << "Password: "; cin >> s; // ... } , Password: , ? cout cin , , Password: , . , cout cin cin.tie(cout). ostream istream, istream. cout << "Password: "; cin >> s; cout << "Password: "; cout.flush(); cin >> s; is.tie(0) is , , . , , tie(s) , .. 0. tie() . 10.4.1.2  width() , . cout.width(4); cout << '(' << 12 << ')'; 12 4 , .. ( 12) fill(), : cout.width(4); cout.fill('#'); cout << '(' << "ab" << ')'; (##ab) , 0, " , ". cout.width(0); // `` , '' width() . , , cout.width(4); cout << '(' << "121212" << ")\n"; (121212) , , , , . , , , . width() , cout.width(4); cout.fill('#'); cout << '(' << 12 << "),(" << '(' <<12 << ")\n"; (##12),(12) (##12),(##12) . , , , : (##12#),(##12# ) , 10.4.2.1, . 10.4.1.3  ios , flags() setf(). , : class ios { public: // : enum { skipws=01, // input // : left=02, // right=04, // internal=010, // // : dec=020, // oct=040, // hex=0100, // showbase=0200, // showpoint=0400, // uppercase=01000, // 'E', 'X' , 'e', 'x' showpos=02000, // '+' // float: scientific=04000, // .dddddd Edd fixed=010000, // dddd.dd // : unitbuf=020000, // stdio=040000 // }; //... }; . , . - , . , , , : const int my_io_options = ios::left|ios::oct|ios::showpoint|ios::fixed; cout.flags(my_io_options); : void your_function(int ios_options); void my_function() { // ... your_function(my_io_options); // ... } flags(), : void your_function(int ios_options) { int old_options = cout.flags(ios_options); // ... cout.flags(old_options); // reset options } flags() . , , . myostream.flags(myostream.flags()|ios::showpos); myostream + , , . , | showpos. setf() , myostream.setf(ios::showpos); . - - - . , , ( 10.4.2.1). , . 10.4.1.4  | flags() setf() , . . , , . , <iostream.h>, setf(), "", . cout.setf(ios::oct,ios::basefield); // cout.setf(ios::dec,ios::basefield); // cout.setf(ios::hex,ios::basefield); // , . , , cout << 1234 << ' '; // cout << 1234 << ' '; cout.setf(ios::oct,ios::basefield); // cout << 1234 << ' '; cout << 1234 << ' '; cout.setf(ios::hex,ios::basefield); // cout << 1234 << ' '; cout << 1234 << ' '; 1234 1234 2322 2322 4d2 4d2 , showbase. , cout.setf(ios::showbase); 1234 1234 02322 02322 0x4d2 0x4d2 , $$10.4.2.1, . 10.4.1.5  setf() : cout.setf(ios::left,ios::adjustfield); // cout.setf(ios::right,ios::adjustfield); // cout.setf(ios::internal,ios::adjustfield); // , ios::width(), . : cout.width(4); cout << '(' << -12 << ")\n"; cout.width(4); cout.setf(ios::left,ios::adjustfield); cout << '(' << -12 << ")\n"; cout.width(4); cout.setf(ios::internal,ios::adjustfield); cout << '(' << -12 << "\n"; ( -12) (-12 ) (- 12) internal (), . , . 10.4.1.6 . , . , : cout.setf(ios::scientific,ios::floatfield); cout.setf(ios::fixed,ios::floatfield); cout.setf(0,ios::floatfield); // . : cout << 1234.56789 << '\n'; cout.setf(ios::scientific,ios::floatfield); cout << 1234.56789 << '\n'; cout.setf(ios::fixed,ios::floatfield); cout << 1234.56789 << '\n'; 1234.57 1.234568e+03 1234.567890 n , cout.precision(n) n 6. precision - precision, cout.precision(8); cout << 1234.56789 << '\n'; cout << 1234.56789 << '\n'; cout.precision(4); cout << 1234.56789 << '\n'; cout << 1234.56789 << '\n'; 1234.5679 1234.5679 1235 1235 , , . , $$10.4.2.1, . 10.4.2  , -. : cout << x; cout.flush(); cout << y; cin.eatwhite(); cin >> x; , , , . flush() eatwhite() -. flush(). operator<<(), flush(): class Flushtype { }; ostream& operator<<(ostream& os, Flushtype) { return flush(os); } Flushtype FLUSH; , FLUSH , : cout << x << FLUSH << y << FLUSH ; . , , . , . : typedef ostream& (*Omanip) (ostream&); ostream& operator<<(ostream& os, Omanip f) { return f(os); } " , ostream& ostream&". , flush() " ostream& ostream&", cout << x << flush << y << flush; flush(). <iostream.h> flush() ostream& flush(ostream&); operator<<, , : class ostream : public virtual ios { // ... public: ostream& operator<<(ostream& ostream& (*)(ostream&)); // ... }; cout : cout << x << flush << y << flush; istream: istream& ws(istream& is ) { return is.eatwhite(); } class istream : public virtual ios { // ... public: istream& operator>>(istream&, istream& (*) (istream&)); // ... }; cin >> ws >> x; x. , >> "" , ws() . . , cout << setprecision(4) << angle; angle . , , . , setprecision(4) , "" operator<<(): class Omanip_int { int i; ostream& (*f) (ostream&,int); public: Omanip_int(ostream& (*ff) (ostream&,int), int ii) : f(ff), i(ii) { } friend ostream& operator<<(ostream& os, Omanip& m) { return m.f(os,m.i); } }; Omanip_int i f, operator<< f() i. -. cout << setprecision(4) << angle , , setprecision(4) Omanip_int, 4 , ostream , : ostream& _set_precision(ostream&,int); Omanip_int setprecision(int i) { return Omanip_int(&_set_precision,i); } , operator<<() precision(i). Omanip_int , : template<class T> class OMANIP { T i; ostream& (*f) (ostream&,T); public: OMANIP(ostream (*ff) (ostream&,T), T ii) : f(ff), i(ii) { } friend ostream& operator<<(ostream& os, OMANIP& m) { return m.f(os,m.i) } }; OMANIP : ostream& precision(ostream& os,int) { os.precision(i); return os; } OMANIP<int> setprecision(int i) { return OMANIP<int>(&precision,i); } <iomanip.h> OMANIP, istream - SMANIP, SMANIP - ioss. , , . , , istream, ostream, OMANIP SMANIP. . . (layout ) - 68. -.