The following program demonstrates the behavior of three manipulators: setw, left, and right. The program surounds each output line with '|' characters to demark the field width and padding to better demonstrates the affect of the left and right manipulators.
Program | Output |
---|---|
#include <iostream> #include <iomanip> using namespace std; int main() { cout << "String" << endl; cout << "|" << "Hello world" << "|" << endl; cout << "|" << setw(20) << "Hello world" << "|" << endl; cout << "|" << left << setw(20) << "Hello world" << "|" << endl; cout << "|" << right << setw(20) << "Hello world" << "|" << endl; cout << endl; cout << "Number" << endl; cout << "|" << 123.45 << "|" << endl; cout << "|" << setw(20) << 123.45 << "|" << endl; cout << "|" << left << setw(20) << 123.45 << "|" << endl; cout << "|" << right << setw(20) << 123.45 << "|" << endl; return 0; } |
String |Hello world| | Hello world| |Hello world | | Hello world| Number |123.45| | 123.45| |123.45 | | 123.45| |