I/O Manipulation Header
2025年3月21日...小于 1 分钟
I/O Manipulation Header
0x00 IOManip Usage
#include <iomanip.h>
0x00 Methods
Set Width
setw(n)
Case Study
Output 10 chars, filling in blank ones with spaces.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
string n;
cin >> n;
cout << left << setw(10) << n << endl;
return 0;
}
Case Study
Pretty-printing datetime:
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main() {
int y, m, d;
cin << y << m << d;
cout << y << '-' << setw(2) << m << '-' << setw(2) << d;
}
Set Fill
setfill(n);
Case Study
Output 10 chars, filling in blank ones with dashes.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
string n;
cin >> n;
cout << setfill('-') << setw(10)<< n << endl;
return 0;
}
你认为这篇文章怎么样?
- 0
- 0
- 0
- 0
- 0
- 0
Powered by Waline v3.5.6