what really is the difference between using setw function and width(). Are they the same thing? can someone give me an example of each please.
setw vs width
Started by kumamako, Aug 01 2009 11:30 AM
4 replies to this topic
#1
Posted 01 August 2009 - 11:30 AM
|
|
|
#2
Posted 01 August 2009 - 11:48 AM
Uhh... are you referring to C++ character streams? Or is this some kind of GUI library you're talking about? Info man, info!
Anyway, the differences between using setw and width() in the C++ ostreams are marginal, nothing more than syntax as far as I know. The documentation goes so far to say that setw is nothing more than a call to the width() member.
The only real difference is with width() you don't have to include the iomanip lib, whereas if you use setw you must explicitly #include <iomanip>.
- Zeke
Anyway, the differences between using setw and width() in the C++ ostreams are marginal, nothing more than syntax as far as I know. The documentation goes so far to say that setw is nothing more than a call to the width() member.
The only real difference is with width() you don't have to include the iomanip lib, whereas if you use setw you must explicitly #include <iomanip>.
- Zeke
#3
Posted 01 August 2009 - 10:45 PM
And what about between cout.precision() and setprecision()? Is that all the same too?
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
#4
Posted 02 August 2009 - 01:27 AM
I refer to these guys a lot.
The manipulator setprecision behaves as a call to the member function ios_base::precision(). Again, it seems the only real difference is preference and the need to include <iomanip>. However, a small difference I notice is with the "fixed" manipulator:
- Zeke
The manipulator setprecision behaves as a call to the member function ios_base::precision(). Again, it seems the only real difference is preference and the need to include <iomanip>. However, a small difference I notice is with the "fixed" manipulator:
float f = 3.14159; cout << setprecision(9) << f << endl; // prints "3.14159" cout << fixed << setprecision(9) << f << endl; // prints "3.141590000"Whereas with cout.precision() this would require two seperate function calls.
float f = 3.14159; cout.precision(9); cout << f << endl; // prints "3.14159" cout.setf(ios::fixed, ios::floatfield); //when a float is provided, the precision is fixed. cout << f << endl; // prints "3.141590000"I'd use the manipulators from <iomanip> personally since they're a bit more self documenting and easier, at least for me, to read. The differences are marginal though.
- Zeke
#5
Posted 02 August 2009 - 08:56 AM
Thanx m8 :)
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


Sign In
Create Account


Back to top









