C++/Console/cout fill
Версия от 17:21, 25 мая 2010; (обсуждение)
Содержание
cout fill(): Specify the fill character
<source lang="cpp">
- 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;
}
</source>
Set fill() for string output
<source lang="cpp">
- 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;
}
</source>
Uses ios member functions: setf, ios::hex, ios::basefield
<source lang="cpp">
- 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;
}
</source>
Using fill() and width for string output
<source lang="cpp">
- 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;
}
</source>
Using fill() and width setting for string output
<source lang="cpp">
- 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;
}
</source>
Using fill() to set filler for extra space
<source lang="cpp">
- 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;
}
</source>