width() and setw()

A

Alex Vinokur

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:[email protected]
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top