模板
putInt()
inline void putInt(register int src, const char suffix) { static char tmp[13]; register unsigned int div; char* st = tmp + 11; *st = suffix, * (st + 1) = '\0'; if (src < 0) { src = ~src + 1; *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; while (src = div) * (--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; *(--st) = '-'; } else { do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); } fputs(st, stdout); }
|
putUInt()
inline void putUInt(register unsigned int src, const char suffix) { static char tmp[12]; register unsigned int div; char* st = tmp + 10; *st = suffix, * (st + 1) = '\0'; do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); fputs(st, stdout); }
|
putLongLong()
inline void putLongLong(register long long src, const char suffix) { static char tmp[22]; register unsigned long long div; char* st = tmp + 20; *st = suffix, * (st + 1) = '\0'; if (src < 0) { src = ~src + 1; *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; while (src = div) * (--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; *(--st) = '-'; } else { do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); } fputs(st, stdout); }
|
putULongLong()
inline void putULongLong(register unsigned long long src, const char suffix) { static char tmp[22]; register unsigned long long div; char* st = tmp + 20; *st = suffix, * (st + 1) = '\0'; do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); fputs(st, stdout); }
|
概述
本組模板為輸出優化模板,使用此模板取代 printf() 可以大幅印出資料的時間。
參數
src
本參數為要輸出的整數
suffix
本參數為串接在整數後面的一個後墜字元,若無後墜請輸入 0 (或 '\0')
返回值
本函式無返回值
實例
#include <stdio.h>
inline void putInt(register int src, const char suffix) { static char tmp[13]; register unsigned int div; char* st = tmp + 11; *st = suffix, * (st + 1) = '\0'; if (src < 0) { src = ~src + 1; *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; while (src = div) * (--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; *(--st) = '-'; } else { do { *(--st) = (src - ((div = src / 10) << 3) - (div << 1)) | '0'; } while (src = div); } fputs(st, stdout); }
int main() { int min = 1 << 31; putInt(30, ' '); putInt(2147483647, '\n'); putInt(min, '\0'); return 0; }
|
編譯運行結果
30 2147483647
-2147483648
注意事項
更新紀錄
2019-9-6
- 將轉正移至單獨一行避免 ZJ 編譯器出錯
- 整數轉字元用 '|' 取代 '+' 提升效率
- buffer 改成靜態減少宣告時間