width() and setw()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Vinokur

    width() and setw()

    Both function width() and manipulator setw() set field width for _next_ output operation.
    width() seems to be less handy because we should split a chain of <<.

    Is there any situation in which width() is more preferable?

    ====== C++ code : File foo.cpp : BEGIN ======

    #include <iostream>
    #include <iomanip>
    using namespace std;

    int main ()
    {
    double f = 1.234;

    cout.setf(ios:: fixed, ios::floatfield );
    cout.precision( 2);

    cout << f << endl
    << setw(7)
    << f << endl
    << f << endl;

    cout << endl;

    cout << f << endl;
    cout.width (7);
    cout << f << endl
    << f << endl;


    return 0;
    }

    ====== C++ code : File foo.cpp : END ========


    ====== Compilation & Run : BEGIN ======

    $ g++ -v
    [---omitted---]
    gcc version 3.3.1 (cygming special)

    $ g++ -W -Wall foo.cpp

    $ a
    1.23
    1.23
    1.23

    1.23
    1.23
    1.23


    ====== Compilation & Run : END ========


    --
    =============== =============== =======
    Alex Vinokur
    mailto:alexvn@c onnect.to

    news://news.gmane.org/gmane.comp.lang.c++.perfometer
    =============== =============== =======




Working...