By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,141 Members | 1,765 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,141 IT Pros & Developers. It's quick & easy.

width() and setw()

P: n/a
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:al****@connect.to
http://mathforum.org/library/view/10978.html
news://news.gmane.org/gmane.comp.lang.c++.perfometer
=====================================


Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.