---------------
template<typename C, typename Tr = char_traits<C>>
class basic_iostream :
    public basic_istream<C,Tr>, public basic_ostream<C,Tr> {
public:
    using char_type = C;
    using int_type = typename Tr::int_type;
    using pos_type = typename Tr::pos_type;
    using off_type = typename Tr::off_type;
    using traits_type = Tr;

    explicit basic_iostream(basic_streambuf<C,Tr>* sb);
    virtual ~basic_iostream();
protected:
    basic_iostream(const basic_iostream& rhs) = delete;
    basic_iostream(basic_iostream&& rhs);
    basic_iostream& operator=(const basic_iostream& rhs) = delete;
    basic_iostream& operator=(basic_iostream&& rhs);
    void swap(basic_iostream& rhs);
};
---------------
template<typename C, typename Tr=char_traits<C>>
class basic_fstream
: public basic_iostream<C,Tr> {
public:
    using char_type = C;
    using int_type = typename Tr::int_type;
    using pos_type = typename Tr::pos_type;  // dla pozycji w pliku
    using off_type = typename Tr::off_type;  // dla przesuni w pliku
    using traits_type = Tr;
    //...
};
---------------
using ifstream = basic_ifstream<char>;
using wifstream = basic_ifstream<wchar_t>;
using ofstream = basic_ofstream<char>;
using wofstream = basic_ofstream<wchar_t>;
using fstream = basic_fstream<char>;
using wfstream = basic_fstream<wchar_t>;

---------------
ofstream ofs("cel");  // o oznacza output, co implikuje ios::out
if (!ofs)
    error("nie mona otworzy celu do zapisu");
fstream ifs;          // i oznacza input, co implikuje ios::in
ifs.open("rdo",ios_base::in);
if (!ifs)
    error("nie mona otworzy rda do odczytu");
---------------
template<typename C, typename Tr = char_traits<C>, typename A = allocator<C>>
class basic_stringstream
    : public basic_iostream<C,Tr> {
public:
    using char_type = C;
    using int_type = typename Tr::int_type;
    using pos_type = typename Tr::pos_type;  // dla pozycji w strumieniu
    using off_type = typename Tr::off_type;  // dla przesuni w strumieniu
    using traits_type = Tr;
    using allocator_type = A;
    //...

---------------
using istringstream = basic_istringstream<char>;
using wistringstream = basic_istringstream<wchar_t>;
using ostringstream = basic_ostringstream<char>;
using wostringstream = basic_ostringstream<wchar_t>;
using stringstream = basic_stringstream<char>;
using wstringstream = basic_stringstream<wchar_t>;
---------------
void test()
{
    ostringstream oss {"Etykieta: ",ios::ate}; // zapis na kocu
    cout << oss.str() << '\n';                 // zapisuje "Etykieta: "
    oss<<"val";
    cout << oss.str() << '\n';                 // zapisuje "Etykieta: val" ("val" dodane za "Etykieta: ")

    ostringstream oss2 {"Etykieta: "};         // zapis na pocztku
    cout << oss2.str() << '\n';                // zapisuje "Etykieta: "
    oss2<<"val";
    cout << oss2.str() << '\n';                // zapisuje "valkieta: " (val nadpisuje "Etykieta: ")
}
---------------
void test2()
{
    istringstream iss;
    iss.str("Foobar");         // wstawia do iss

    cout << iss << '\n';       // pisze 1
    cout << iss.str() << '\n'; // OK: pisze "Foobar"
}
---------------
if (iss) { // ostatnia operacja iss powioda si; stan iss to good() lub eof()
    //...
}
else {
    // obsuga bdu
}

---------------
for (X x; cin>>x;) { // wczytuje do bufora wejciowego typu X
    //... robi co z x...
}
// tu dochodzimy, jeli operator >> nie moe wczyta kolejnego X z cin
---------------
int i;
if (cin>>i) {
    //... uycie i...
} else if (cin.fail()){ // zapewne bd formatowania
    cin.clear();
    string s;
    if (cin>>s) {       // moe bdzie mona uy acucha w celu odzyskania sprawnoci
        //... uycie s...
    }
}

---------------
cin.exceptions(cin.exceptions()|ios_base::badbit);
---------------
struct Io_guard {  // klasa RAII wyjtkw iostream
    iostream& s;
    auto old_e = s.exceptions();
    Io_guard(iostream& ss, ios_base::iostate e) :s{ss} { s.exceptions(s.exceptions()|e); }
    ~Io_guard() { s.exceptions(old_e); }
};

void use(istream& is)
{
    Io_guard guard(is.ios_base::badbit);
    //... uycie is...
}
catch (ios_base::badbit) {
    //... kopoty!...
}
---------------
template<typename C, typename Tr = char_traits<C>>
class basic_istream : virtual public basic_ios<C,Tr> {
public:
    using char_type = C;
    using int_type = typename Tr::int_type;
    using pos_type = typename Tr::pos_type;
    using off_type = typename Tr::off_type;
    using traits_type = Tr;

    explicit basic_istream(basic_streambuf<C,Tr>*sb);
    virtual ~basic_istream(); // zwalnia wszystkie zasoby

    class sentry;
    //...
protected:
    // przenoszenie, ale nie kopiowanie:
    basic_istream(const basic_istream& rhs) = delete;
    basic_istream(basic_istream&& rhs);
    basic_istream& operator=(const basic_istream& rhs) = delete;
    basic_istream& operator=(basic_istream&& rhs);
    //...
};
---------------
template<typename C, typename Tr = char_traits<C>>
basic_ostream<C,Tr>& basic_ostream<C,Tr>::operator<<(int i)
{
    sentry s {*this};
    if (!s) { // sprawdzenie, czy wszystko jest w porzdku, aby mona byo rozpocz wysyanie na wyjcie
        setstate(failbit);
        return *this;
    }
    //... wysanie na wyjcie wartoci...
    return *this;
}

---------------
template<typename T1, typename T2>
void read_pair(T1& x, T2& y)
{
    char c1, c2, c3;
    cin >> c1 >> x >> c2 >> y >> c3;
    if (c1!='{' || c2!=',' || c3!='}') { // bd formatu, ktrego nie mona naprawi
        cin.setstate(ios_base::badbit);  // ustawia badbit
        throw runtime_error("bd odczytu pary");
    }
}
---------------
for (int i; cin>>i && 0<i;)
    cout << i << '\n';
---------------
void f() // niskopoziomowa starowiecka metoda wczytywania linii tekstu
{
    char word[MAX_WORD][MAX_LINE];  // MAX_WORD tablic, po MAX_LINE znakw kada
    int i = 0;
    while(cin.getline(word[i++],MAX_LINE,'\n') && i<MAX_WORD)
        /* nic nie rb */;
    //...
}
---------------
template<typename C, typename Tr = char_traits<C>>
class basic_ostream : virtual public basic_ios<C,Tr> {
public:
    using char_type = C;
    using int_type = typename Tr::int_type;
    using pos_type = typename Tr::pos_type;
    using off_type = typename Tr::off_type;
    using traits_type = Tr;

    explicit basic_ostream(basic_streambuf<char_type ,Tr>* sb);
    virtual ~basic_ostream(); // zwalnia wszystkie zasoby

    class sentry;  // patrz 38.4.1
    //...
protected:
    // przenoszenie, ale nie kopiowanie:
    basic_ostream(const basic_ostream& rhs) = delete;
    basic_ostream(basic_ostream&& rhs);
    basic_ostream& operator=(const basic_ostream& rhs) = delete;
    basic_ostream& operator=(basic_ostream&& rhs);
    //...
};
---------------
cout << "Warto x to " << x << '\n';
---------------
void print_val(char ch)
{
    cout << "warto '" << ch << "' wynosi " << int{ch} << '\n';
}

void test()
{
    print_val('a');
    print_val('A');
}
---------------
warto 'a' wynosi 97
warto 'A' wynosi 65
---------------
template<typename T>
struct Named_val {
    string name;
    T value;
};

ostream& operator<<(ostream& os, const Named_val& nv)
{
    return os << '{' << nv.name << ':' << nv.value << '}';
}
---------------
class My_base {
public:
    //...
    virtual ostream& put(ostream& s) const = 0; // zapisuje *this do s
};

ostream& operator<<(ostream& s, const My_base& r)
{
    return r.put(s); // uywa odpowiedniej funkcji put()
}
---------------
class Sometype : public My_base {
public:
    //...
    ostream& put(ostream& s) const override;  // prawdziwa funkcja wyjciowa
};

void f(const My_base& r, Sometype& s) // uywa << wywoujcego odpowiedni funkcj put()
{
    cout << r << s;
}
---------------
cout << setprecision(4) << angle;
---------------
struct smanip {
    ios_base& (*f)(ios_base&,int); // funkcja do wywoania
    int i;                         // warto do uycia
    smanip(ios_base&(*ff)(ios_base&,int), int ii) :f{ff}, i{ii} { }
};

template<typename C, typename Tr>
basic_ostream<C,Tr>& operator<<(basic_ostream<C,Tr>& os, const smanip& m)
{
    m.f(os,m.i); // wywouje f obiektu m z zapisan wartoci m
    return os;
}
---------------
inline smanip setprecision(int n)
{
    auto h = [](ios_base& s, int x) -> ios_base& { s.precision(x); return s; };
    return smanip(h,n); // tworzy obiekt funkcyjny
}
---------------
cout << setprecision(4) << angle;
---------------
template<typename C, typename Tr = char_traits<C>>
class basic_ios : public ios_base {
public:
    using char_type = C;
    using int_type = typename Tr::int_type;
    using pos_type = typename Tr::pos_type;
    using off_type = typename Tr::off_type;
    using traits_type = Tr;
    //...
};
---------------
class ios_base {
public:
    using fmtflags = /* typ zdefiniowany w implementacji */;
    using iostate = /* typ zdefiniowany w implementacji */;
    using openmode = /* typ zdefiniowany w implementacji */;
    using seekdir = /* typ zdefiniowany w implementacji */;
    class failure; // klasa wyjtkw
    class Init;    // inicjuje standardowe strumienie wejcia i wyjcia
};

---------------
template<typename T>
char* as_bytes(T& i)
{
    return static_cast<char*>(&i); // traktuje t pami jako bajty
}

void test()
{
    ifstream ifs("source",ios_base::binary);      // binarny tryb strumienia
    ofstream ofs("target",ios_base::binary);      // binarny tryb strumienia

    vector<int> v;

    for (int i; ifs.read(as_bytes(i),sizeof(i));) // wczytuje bajty z pliku binarnego
        v.push_back(i);

    //... jakie dziaania przy uyciu v...

    for (auto i : v)                              // zapisuje bajty w pliku binarnym:
        ofs.write(as_bytes(i),sizeof(i));
}

---------------
for (X x; cin>>x;) {
    //...
}
---------------
for (X x; !(cin>>x).fail();) {
    //...
}
---------------
cout << "Wpisz liczb: ";
int num;
cin >> num;

---------------
enum event {
    erase_event,
    imbue_event,
    copyfmt_event
};

using event_callback = void (*)(event, ios_base&, int index);

---------------
ios.unsetf(ios_base::floatfield); // uywa domylnego formatu zmiennoprzecinkowego
ios.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield); 
(// uywa szesnastkowych liczb zmiennoprzecinkowych

---------------
cout.precision(8);
cout << 1234.56789 << ' ' << 1234.56789 << ' ' << 123456 << '\n';

cout.precision(4);
cout << 1234.56789 << ' ' << 1234.56789 << ' ' << 123456 << '\n';
---------------
1234.5679 1234.5679 123456
1235 1235 123456
---------------
cout.width(4);
cout << 12; // drukuje liczb 12 poprzedzon dwiema spacjami
---------------
cout.width(4);
cout.fill('#');
cout << "ab"; // drukuje ##ab
---------------
cout.width(0); // "tyle znakw, ile potrzeba"
---------------
cout.width(4);
cout << "abcdef"; // drukuje abcdef
---------------
cout.width(4);
cout.fill('#');
cout << 12 << ':' << 13; // drukuje ##12:13
---------------
cout << 1234 << ',' << hex << 1234 << ',' << oct << 1234 << '\n'; // drukuje 1234,4d2,2322
---------------
constexpr double d = 123.456;

cout << d << "; "
    << scientific << d << "; "
    << hexfloat << d << "; "
    << fixed << d << "; "
    << defaultfloat << d << '\n';
---------------
123.456; 1.234560e+002; 0x1.edd2f2p+6; 123.456000; 123.456

---------------
cout << "Witaj, wiecie!\n";
---------------
cout << "Witaj, wiecie!" << endl;

---------------
cout << '(' << setw(4) << setfill('#') << 12 << ") (" << 12 << ")\n"; // drukuje (##12) (12)

---------------
string input {"0 1 2 3 4"};
istringstream iss {input};
string s;
for (char ch; iss>>ch;)
    s+=ch;
cout << s; // drukuje "01234"

istringstream iss2 {input};
iss>>noskipws;
for (char ch; iss2>>ch;)
    s+=ch;
cout << s; // drukuje "0 1 2 3 4"
}
---------------
Form gen4 {4}; // oglny format, precyzja 4

void f(double d)
{
    Form sci8;
    sci8.scientific().precision(8);     // format naukowy, precyzja 8
    cout << d << ' ' << gen4(d) << ' ' << sci8(d) << ' ' << d << '\n';

    Form sci {10,ios_base::scientific}; // format naukowy, precyzja 10
    cout << d << ' ' << gen4(d) << ' ' << sci(d) << ' ' << d << '\n';
}
---------------
1234.57 1235 1.23456789e+003 1234.57
1234.57 1235 1.2345678900e+003 1234.57
---------------
class Form; // nasz typ formatujcy

struct Bound_form { // forma plus warto
    const Form& f;
    double val;
};

class Form {
    friend ostream& operator<<(ostream&, const Bound_form&);

    int prc;  // precyzja
    int wdt;  // szeroko 0 oznacza "taka szeroko, jaka jest potrzebna"
    int fmt;  // general, scientific lub fixed (38.4.5.1)
    //...
public:
    explicit Form(int p =6, ios_base::fmtflags f =0, int w =0) : prc{p}, fmt{f}, wdt{w} {}

    Bound_form Form::operator()(double d) const // tworzy obiekt typu Bound_form dla *this i d
    {
        return Bound_form{*this,d};
    }

    Form& scientific() { fmt = ios_base::scientific; return *this; }
    Form& fixed() { fmt = ios_base::fixed; return *this; }
    Form& general() { fmt = 0; return *this; }

    Form& uppercase();
    Form& lowercase();
    Form& precision(int p) { prc = p; return *this; }

    Form& width(int w) { wdt = w; return *this; } // dotyczy wszystkich typw
    Form& fill(char);

    Form& plus(bool b = true);                    // jawny plus
    Form& trailing_zeros(bool b = true);          // drukuje zera na kocu
    //...
};
---------------
ostream& operator<<(ostream& os, const Bound_form& bf)
{
    ostringstream s; // 38.2.2
    s.precision(bf.f.prc);
    s.setf(bf.f.fmt,ios_base::floatfield);
    s << bf.val;          // skada acuch w s
    return os << s.str(); // wysya s do os
}
---------------
template<typename T,
         typename C = char,
         typename Tr = char_traits<C>,
         typename Distance = ptrdiff_t>
class istream_iterator
    :public iterator<input_iterator_tag, T, Distance , const T*, const T&> {
    using char_type = C;
    using traits_type = Tr;
    using istream_type = basic_istream<C,Tr>;
    //...
};
template<typename T, typename C = char, typename Tr = char_traits<C>>
class ostream_iterator: public iterator<output_iterator_tag, void, void, void, void> {
using char_type = C;
using traits_type = Tr;
using ostream_type = basic_ostream<C,Tr>;
//...
};
---------------
copy(istream_iterator<double>{cin}, istream_iterator<double ,char>{},
ostream_iterator<double>{cout,";\n"});
---------------
1;
2;
3;
---------------
template<typename C, typename Tr = char_traits<C>>
class basic_streambuf {
public:
    using char_type = C;                     // typ znakw
    using int_type = typename Tr::int_type;  // typ cakowitoliczbowy, na ktry
                                             // mona przekonwertowa znak

    using pos_type = typename Tr::pos_type;  // typ pozycji w buforze
    using off_type = typename Tr::off_type;  // typ przesunicia wzgldem pozycji w buforze
    using traits_type = Tr;
    //...
    virtual ~basic_streambuf();
};
---------------
using streambuf = basic_streambuf<char>;
using wstreambuf = basic_streambuf<wchar_t>;

---------------
template<typename C, typename Tr = char_traits<C>>
class basic_ostream : virtual public basic_ios<C,Tr> {
public:
    //...
    explicit basic_ostream(basic_streambuf<C,Tr>* b);

    pos_type tellp();                                    // pobiera biec pozycj
    basic_ostream& seekp(pos_type);                      // ustawia biec pozycj
    basic_ostream& seekp(off_type, ios_base::seekdir);   // ustawia biec pozycj

    basic_ostream& flush();                              // oprnia bufor (do rzeczywistego 
                                                         // miejsca docelowego)
    basic_ostream& operator<<(basic_streambuf<C,Tr> *b); // zapisuje z b
};
---------------
int f(ofstream& fout) // fout odnosi si do jakiego pliku
{
    fout << "0123456789";
    fout.seekp(8);                // o 8 od pocztku
    fout << '#';                  // dodaje '#' i przesuwa pozycj (+1)
    fout.seekp(-4,ios_base::cur); // o 4 wstecz
    fout << '*';                  // dodaje '*' i przesuwa pozycj (+1)
}
---------------
01234*67#9
---------------
template<typename C, typename Tr = char_traits<C>>
class basic_istream : virtual public basic_ios<C,Tr> {
public:
    //...
    explicit basic_istream(basic_streambuf<C,Tr>* b);
    pos_type tellg();                                   // pobiera biec pozycj
    basic_istream& seekg(pos_type);                     // ustawia biec pozycj
    basic_istream& seekg(off_type, ios_base::seekdir);  // ustawia biec pozycj

    basic_istream& putback(C c); // wstawia c z powrotem do bufora
    basic_istream& unget();      // zwraca ostatnio wczytany znak
    int_type peek();             // sprawdza, jaki jest nastpny znak do wczytania

    int sync();                  // czyci bufor (oprnia)

    basic_istream& operator>>(basic_streambuf<C,Tr>* b); // wczytuje do b
    basic_istream& get(basic_streambuf<C,Tr>& b, C t = Tr::newline());
    streamsize readsome(C* p, streamsize n);  // wczytuje najwyej n znakw
};
---------------
template<typename C, typename Tr = char_traits<C>>  // iso.24.6.3
class istreambuf_iterator
    :public iterator<input_iterator_tag, C, typename Tr::off_type ,/* nieokrelone */, C> {
public:
    using char_type = C;
    using traits_type = Tr;
    using int_type = typename Tr::int_type;
    using streambuf_type = basic_streambuf<C,Tr>;
    using istream_type = basic_istream<C,Tr>;
    //...
};
---------------
template<typename C, typename Tr = char_traits<C>>  // iso.24.6.4
class ostreambuf_iterator
    :public iterator<output_iterator_tag, void, void, void, void> {
public:
    using char_type = C;
    using traits_type = Tr;
    using streambuf_type = basic_streambuf<C,Tr>;
    using ostream_type = basic_ostream<C,Tr>;
    //...
};