Материал из C\C++ эксперт
Default right justification
#include <iostream>
using namespace std;
int main()
{
// Use default width.
cout << "Default format.\n";
cout << "|";
cout << 123.45 << "|" << "\n\n";
// Use default right justification
cout << "Right-justify in a field width of 12.\n";
cout << "|";
cout.width(12);
cout << 123.45 << "|" << "\n\n";
return 0;
}
Switch to left justification
#include <iostream>
using namespace std;
int main()
{
// Use default width.
cout << "Default format.\n";
cout << "|";
cout << 123.45 << "|" << "\n\n";
// Switch to left justification.
cout << "Left-justify in a field width of 12.\n";
cout.setf(ios::left, ios::adjustfield);
cout << "|";
cout.width(12);
cout << 123.45 << "|" << "\n\n";
return 0;
}
Turning on internal justification
#include <iostream>
using namespace std;
int main()
{
// Use default width.
cout << "Default format.\n";
cout << "|";
cout << 123.45 << "|" << "\n\n";
// Now, use internal.
cout << "Turning on internal justification.\n";
cout.setf(ios::internal, ios::adjustfield);
cout << "Internal justification in a field width of 12.\n";
cout << "|";
cout.width(12);
cout << 123.45 << "|" << endl;
return 0;
}
Turn on showpos, use left-justification
#include <iostream>
using namespace std;
int main()
{
// Use default width.
cout << "Default format.\n";
cout << "|";
cout << 123.45 << "|" << "\n\n";
// Turn on showpos, use left-justification.
cout << "Turning on showpos flag.\n";
cout.setf(ios::showpos);
cout << "Left-justify set in a field width of 12 again.\n";
cout << "|";
cout.width(12);
cout << 123.45 << "|" << "\n\n";
return 0;
}