C++/Console/cout fill
Содержание
cout fill(): Specify the fill character
#include <iostream>
using namespace std;
int main()
{
cout.precision(4) ;
cout.width(10);
cout << 10.12345 << "\n";
cout.fill("*");
cout.width(10);
cout << 10.12345 << "\n";
cout.width(10);
cout << "Hi!" << "\n";
cout.width(10);
cout.setf(ios::left);
cout << 10.12345;
return 0;
}
Set fill() for string output
#include <iostream>
using namespace std;
int main()
{
cout << "Hello" << endl;
// Now set the width and the fill character.
cout.width(10);
cout.fill("*");
cout << "Hello" << endl;
return 0;
}
Uses ios member functions: setf, ios::hex, ios::basefield
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout.setf(ios::hex, ios::basefield);
cout << 12300 << "\n";
cout.fill("?");
cout.width(10);
cout << 232343.0;
return 0;
}
Using fill() and width for string output
#include <iostream>
using namespace std;
int main()
{
cout << "Start >";
cout.width(25);
cout << 123 << "< End\n";
cout << "Start >";
cout.width(25);
cout.fill("*");
cout << 123 << "< End\n";
return 0;
}
Using fill() and width setting for string output
#include <iostream>
using namespace std;
int main()
{
cout << "Start >";
cout.width(25);
cout << 123 << "< End\n";
cout << "Start >";
cout.width(25);
cout.fill("*");
cout << 123 << "< End\n";
cout << "Start >";
cout.width(25);
cout << 456 << "< End\n";
return 0;
}
Using fill() to set filler for extra space
#include <iostream>
using namespace std;
int main()
{
cout << "Start >";
cout.width(25);
cout << 123 << "< End\n";
cout << "Start >";
cout.width(25);
cout.fill("*");
cout << 123 << "< End\n";
return 0;
}