repeat就是将某個字元重複若幹次生成一個字元串。這在需要多個前導空格、文本行分隔線的時候非常有用。
- string repeat(string s, int count) {
- string r;
- for (int i = 0; i < count; i++) {
- r += s;
- }
- return r;
- }
- string repeat(char ch, int count) {
- ostringstream s;
- s << setw(count) << setfill(ch) << "";
- return s.str();
- }