Iom

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 ========

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.