码迷,mamicode.com
首页 > 其他好文 > 详细

一个比较妙的头文件工具

时间:2018-05-19 13:12:25      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:bsp   hide   freopen   适配   发布   文件输入输出   修复   换行   main   

  1. 具体的使用方法一般是自己配置,所有开关都在namespace 的开头。
  2. 然后在Linux下的mmap要求使用文件输入(提交前请关闭文件开关)。
  3. 如果使用了algorithm库请在max、min前加上::(如::max(a,b))。
  4. 如果有变量名冲突需要请取消namespace的注释,并且更改using部分。
  5. 这里的write、max、min在std>=c++11支持任意参数(N>=1)的运行,write默认使用cout,Split为分隔符。
  6. 如果有需要请自行调节,可以加上__inline__ __attribute__ ((always_inline))和extern优化。
  7. 注意尽量不要更改template部分,已经设计好了。
  8. 关于输出为什么需要用cout原因有4:可以自己适配类型,可以自己定义流输出,输出long long和某些类型速度比scanf快,快写的某些类型(例如double)不方便实现。
  9. 如有Bug请在评论区反馈,感谢您的反馈,作者也会不断更新。
  10. 讲解一下用法
    1. read、write快读快写,其中快写要求第一个参数为分隔字符
    2. max、min等工具(请自行决定打不打开namespace),upmax为更新最大值,upmin同理
    3. Queue和Stack(用法举例:Queue<int> mession(MAXN)(Queue<类型> 名字(大小)))(内存分配可能会有不少Bug)
  11. 讲解一下几个开关
    1. DEBUG_PORT:是否编译Debug代码
    2. DEBUG:是否使用Debug
    3. ONLINE_JUDGE:建议用在using namespace std后面,是否为在线测评,该开关改变DEBUG_PORT、NEED_FILE开关
    4. SF_READ:是否读负数(效率有影响)
    5. EOF_READ:是否判断EOF(读到EOF继续读不会RE)
    6. WRITE_ENDL:是否在write后输出换行
    7. FAST_WRITE:是否使用快写(目前支持所有整数)
    8. WRITE_SPLIT:是否把write输入的第一个参数作为后面参数两两间的分割符
    9. NEED_FILE:是否使用文件输入输出
    10. __gnu_linux:系统定义的,检查是否为linux系统
  12. 注意linux下如果使用mmap那么必须使用文件输入输出

版本线:发布-->修复一些Windows下的错误-->修复template,增强template-->(预计)增多快读支持类型-->增强快读,longlong等支持(多方法调用)-->增强文件操作,using::swap似乎有Bug,增加write输出方式-->实用改进-->修复一些Bug-->支持快速手写队列、栈-->增加小数读入支持-->修复Bug(然而在Luogu的测评环境下一直检测不到DONLINE_JUDGE也是一个比较严重的问题......)-->修复不少Bug,增加Debug工具-->修复ONLINE_JUDGE检测

由于直接是从Luogu博客那蒯来的,所以可能格式会有问题

技术分享图片
  1 //Created By Creeper_LKF
  2 //Caution::We used "pragma" in the code
  3 #include <cstdio>
  4 #include <cctype>
  5 #include <cassert>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <iostream>
  9 
 10 #ifdef __gnu_linux__
 11 
 12 #include <fcntl.h>
 13 #include <unistd.h>
 14 #include <sys/mman.h>
 15 
 16 #endif
 17 
 18 #if __cplusplus < 201103L
 19 
 20 #include <stdarg.h>
 21 
 22 #endif
 23 
 24 //Algorithm Heads
 25 
 26 #include <cmath>
 27 #include <queue>
 28 #include <utility>
 29 #include <algorithm>
 30 
 31 
 32 using namespace std;
 33 
 34 //Debug Port
 35 
 36 #define DEBUG_PORT
 37 #define DEBUG
 38 
 39 #ifdef ONLINE_JUDGE
 40 #undef DEBUG_PORT
 41 #endif
 42 
 43 #ifdef DEBUG_PORT
 44 #if __cplusplus < 201103L
 45 # pragma message "Warning : C++11 Not Use"
 46 #ifdef DEBUG
 47 template<typename T>
 48 extern inline void Debug(T tar){
 49     cerr << tar << endl;
 50 }
 51 template<typename Head, typename T, typename... Tail>
 52 extern inline void Debug(Head head, T mid, Tail... tail){
 53     cerr << head <<  ;
 54     Debug(mid, tail...);
 55 }
 56 #else
 57 template<typename Head, typename T, typename... Tail>
 58 extern inline void Debug(Head head, T mid, Tail... tail){
 59     return ;
 60 }
 61 #endif
 62 #else
 63 #ifdef DEBUG
 64 template <typename T>
 65 extern inline void Debug(T tar){
 66     cerr << tar << endl;
 67 }
 68 #else
 69 template <typename T>
 70 extern inline void Debug(T tar){
 71     return ;
 72 }
 73 #endif
 74 #endif
 75 #else
 76 template <typename T>
 77 extern inline void Debug(T tar){
 78     return ;
 79 }
 80 #endif
 81 
 82 const char file_name[] = "b";
 83 
 84 #define NAME_SPACE
 85 #define USING
 86 
 87 #ifdef NAME_SPACE
 88 namespace LKF{
 89 #endif
 90     #define SF_READ
 91     #define EOF_READ
 92     // #define ONLINE_JUDGE
 93     #define WRITE_ENDL
 94     // #define FAST_WRITE
 95     #define SPLIT_WRITE
 96     const size_t MAX_BUF_SIZE = 50000000;
 97     
 98     #define NEED_FILE
 99 
100     #ifdef FAST_WRITE
101     char outp[MAX_BUF_SIZE], *op = outp;
102     #endif
103 
104     #ifdef ONLINE_JUDGE
105     #undef NEED_FILE
106     #endif    
107 
108     #ifdef FAST_WRITE
109     #ifndef WRITE_ENDL
110     #define WRITE_ENDL
111     #endif
112     #endif
113 
114     extern inline void FILE_OPT(){
115         #ifdef NEED_FILE
116         #define FILE_NAME file_name
117         char IN_FILE[sizeof(FILE_NAME) + 5], OUT_FILE[sizeof(FILE_NAME) + 5];
118         strcpy(IN_FILE, FILE_NAME), strcpy(OUT_FILE, FILE_NAME);
119         strcat(IN_FILE, ".in"), strcat(OUT_FILE, ".out");
120         freopen(IN_FILE, "r", stdin);
121         freopen(OUT_FILE, "w", stdout);  
122         #endif      
123     }
124 
125     #ifdef __gnu_linux__
126 
127     char *pc;
128 
129     extern inline void Main_Init(){
130         static bool INITED = false;
131         if(INITED){
132             #ifdef FAST_WRITE
133             fwrite(outp, 1, op - outp - 1, stdout);
134             #endif
135             fclose(stdin), fclose(stdout);
136         } else {
137             FILE_OPT();
138             pc = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0);
139             INITED = true;
140         }
141     }
142 
143     #else
144 
145     char buf[MAX_BUF_SIZE], *pc = buf;
146 
147     extern inline void Main_Init(){
148         static bool INITED = false;
149         if(INITED){
150             #ifdef FAST_WRITE
151             fwrite(outp, 1, op - outp - 1, stdout);
152             #endif
153             fclose(stdin), fclose(stdout);
154         } else {
155             FILE_OPT();
156             fread(buf, 1, MAX_BUF_SIZE, stdin); 
157             INITED = true;           
158         }
159     }
160 
161     #endif
162 
163     inline char read_ch(){
164         char c;
165         while(isspace(c = *pc ++));
166         return c;
167     }
168 
169     #ifdef EOF_READ
170 
171     #ifdef SF_READ
172 
173     template<typename T>
174     static inline void read(T &num){
175         num = 0;
176         char c, sf = 1;
177         while(isspace(c = *pc++));
178         if(c == 45) sf = -1, c = *pc ++;
179         while(num = num * 10 + c - 48, isdigit(c = *pc++));
180         num *= sf;
181     }
182 
183     static inline int read(){
184         int num = 0;
185         char c, sf = 1;
186         while(isspace(c = *pc++));
187         if(c == 45) sf = -1, c = *pc ++;
188         while(num = num * 10 + c - 48, isdigit(c = *pc++));
189         return num * sf;
190     }
191 
192     static inline double read_dec(){
193         double num = 0, decs = 1;
194         char c, sf = 1;
195         while(isspace(c = *pc ++));
196         if(c == -) sf = -1, c = *pc ++;
197         while(num = num * 10 + c - 48, isdigit(c = *pc ++));
198         if(c != .) return num * sf;
199         c = *pc ++;
200         while(num += (decs *= 0.1) * (c - 48), isdigit(c = *pc ++));
201         return num * sf;
202     }
203 
204     #else
205 
206     template<typename T>
207     static inline T read(T &num){
208         num = 0;
209         char c;
210         while (isspace(c = *pc++));
211         while (num = num * 10 + c - 48, isdigit(c = *pc++));
212         return num;
213     }
214 
215     static inline int read(){
216         int num = 0;
217         char c;
218         while (isspace(c = *pc++));
219         while (num = num * 10 + c - 48, isdigit(c = *pc++));
220         return num;
221     }
222 
223     static inline double read_dec(){
224         double num = 0, decs = 1;
225         char c;
226         while(isspace(c = *pc ++));
227         while(num = num * 10 + c - 48, isdigit(c = *pc ++));
228         if(c != .) return num;
229         c = *pc ++;
230         while(num += (c - 48) * (decs *= 0.1), isdigit(c = *pc ++));
231         return num;
232     }
233 
234     #endif
235 
236     #else
237 
238     #ifdef SF_READ
239 
240     template<typename T>
241     static inline void read(T &num){
242         num = 0;
243         char c, sf = 1;
244         while((c = *pc++) < 45);
245         if(c == 45) sf = -1, c = *pc ++;
246         while(num = num * 10 + c - 48, (c = *pc++) >= 48);
247         num *= sf;
248     }
249 
250     static inline int read(){
251         int num = 0;
252         char c, sf = 1;
253         while((c = *pc++) < 45);
254         if(c == 45) sf = -1, c = *pc ++;
255         while(num = num * 10 + c - 48, (c = *pc++) >= 48);
256         return num * sf;
257     }
258 
259     static inline double read_dec(){
260         double num = 0, decs = 1;
261         char c, sf = 1;
262         while(isspace(c = *pc ++));
263         if(c == -) sf = -1, c = *pc ++;
264         while(num = num * 10 + c - 48, isdigit(c = *pc ++));
265         if(c != .) return num * sf;
266         c = *pc ++;
267         while(num += (decs *= 0.1) * (c - 48), isdigit(c = *pc ++));
268         return num * sf;
269     }
270 
271     #else
272 
273     template<typename T>
274     static inline T read(T &num){
275         num = 0;
276         char c;
277         while ((c = *pc++) < 48);
278         while (num = num * 10 + c - 48, (c = *pc++) >= 48);
279         return num;
280     }
281 
282     static inline int read(){
283         int num = 0;
284         char c;
285         while ((c = *pc++) < 48);
286         while (num = num * 10 + c - 48, (c = *pc++) >= 48);
287         return num;
288     }
289 
290     static inline double read_dec(){
291         double num = 0, decs = 1;
292         char c;
293         while(isspace(c = *pc ++));
294         while(num = num * 10 + c - 48, isdigit(c = *pc ++));
295         if(c != .) return num;
296         c = *pc ++;
297         while(num += (c - 48) * (decs *= 0.1), isdigit(c = *pc ++));
298         return num;
299     }
300 
301     #endif
302 
303     #endif
304 
305     #ifdef FAST_WRITE
306     template <typename T>
307     inline void Call_Write(char Split, T tar){
308         char buf[20];
309         int top = 0;
310         if(tar == 0) *op ++ = 48;
311         else {
312             if(tar < 0) *op ++ = -, tar = -tar;
313             while(tar) buf[++top] = tar % 10, tar /= 10;
314             while(top) *op ++ = buf[top --] ^ 48;
315         }
316         *op ++ = Split;
317     }
318     template <typename T>
319     inline void Call_Write(T tar){
320         char buf[20];
321         int top = 0;
322         if(tar == 0) *op ++ = 48;
323         else {
324             if(tar < 0) *op ++ = -, tar = -tar;
325             while(tar) buf[++top] = tar % 10, tar /= 10;
326             while(top) *op ++ = buf[top --] ^ 48;
327         }
328     }
329     #endif
330 
331     #ifdef FAST_WRITE
332 
333     extern inline void write(){
334         *op ++ = \n;
335     }
336 
337     template<typename T>
338     extern inline void write(T tar){
339         Call_Write(tar);
340         #ifdef WRITE_ENDL
341         write();
342         #endif
343     }
344 
345     #if __cplusplus >= 201103L
346 
347     # pragma GCC diagnostic push
348     # pragma GCC diagnostic ignored "-Wunused-parameter"
349 
350     template<typename T>
351     extern inline void write(char Split, T tar){
352         Call_Write(tar);
353         #ifdef WRITE_ENDL
354         write();
355         #endif
356     }
357 
358     # pragma GCC diagnostic pop
359     # pragma message "Warning : pragma used"
360 
361     template<typename Head, typename T, typename... Tail>
362     extern inline void write(char Split, Head head, T mid, Tail... tail){
363         Call_Write(Split, head);
364         write(Split, mid, tail...);
365     }
366 
367     #else
368 
369     template <typename T>
370     extern inline void write(char Split, T tar){
371         Call_Write(tar);
372         #ifdef WRITE_ENDL
373         write();
374         #endif
375     }
376 
377     #endif
378 
379     #else
380 
381     extern inline void write(){
382         cout << endl;
383     }
384 
385     template<typename T>
386     extern inline void write(T tar){
387         cout << tar;
388         #ifdef WRITE_ENDL
389         write();
390         #endif
391     }
392 
393     #if __cplusplus >= 201103L
394 
395     template<typename T>
396     extern inline void write(char Split, T tar){
397         cout << tar << Split;
398         #ifdef WRITE_ENDL
399         write();
400         #endif
401     }
402 
403     template<typename Head, typename T, typename... Tail>
404     extern inline void write(char Split, Head head, T mid, Tail... tail){
405         #ifdef SPLIT_WRITE
406         cout << head << Split;
407         #else
408         cout << head;
409         #endif
410         write(Split, mid, tail...);
411     }
412 
413     #else
414 
415     template <typename T>
416     extern inline void write(char Split, T tar){
417         cout << tar << Split;
418         #ifdef WRITE_ENDL
419         write();
420         #endif
421     }
422 
423     #endif
424 
425     #endif
426 
427     template <typename T>
428     extern inline void upmax(T &x, const T &y){
429         if(x < y) x = y;
430     }
431     template <typename T>
432     extern inline void upmin(T &x, const T &y){
433         if(x > y) x = y;
434     }
435 
436     #if __cplusplus >= 201103L
437 
438     template<typename T>
439     extern inline T max(T tar){
440         return tar;
441     }
442 
443     template<typename T>
444     extern inline T min(T tar){
445         return tar;
446     }
447 
448     template <typename Head, typename T, typename... Tail>
449     extern inline Head max(Head head, T mid, Tail... tail){
450         Head tmp = max(mid, tail...);
451         return head > tmp ? head : tmp;
452     }
453     template <typename Head, typename T, typename... Tail>
454     extern inline Head min(Head head, T mid, Tail... tail){
455         Head tmp = min(mid, tail...);
456         return head < tmp ? head : tmp;
457     }
458 
459     #else
460 
461     template <typename T>
462     extern inline T max(T a, T b){
463         return a > b ? a : b;
464     }
465     template <typename T>
466     extern inline T min(T a, T b){
467         return a < b ? a : b;
468     }    
469 
470     #endif
471 
472     template <typename T>
473     extern inline T abs(T tar){
474         return tar < 0 ? -tar : tar;
475     }
476     template <typename T>
477     extern inline void swap(T &a, T &b){
478         T t = a;
479         a = b;
480         b = t;
481     }
482 #ifdef NAME_SPACE
483 }
484 #endif
485 
486 //Algorithm
487 
488 #ifdef NAME_SPACE
489 namespace LKF{
490 #endif
491 
492     template <typename T>
493     struct Queue{
494         size_t s, t;
495         T *q;
496         Queue(){
497             s = 1, t = 0;
498             q = NULL;
499         }
500         Queue(size_t siz){
501             s = 1, t = 0;
502             q = (T*)malloc(sizeof(T) * siz);
503             assert(q != NULL);
504         }
505         inline void Re_Init(size_t siz){
506             q = (T*)realloc(q, sizeof(T) * siz);
507             assert(q != NULL);
508         }
509         ~Queue(){
510             delete[] q;
511         }
512         inline void clear(){
513             s = 1, t = 0;
514         }
515         inline bool empty(){
516             return s > t;
517         }
518         inline size_t size(){
519             return t - s + 1;
520         }
521         inline void push(T tar){
522             q[++ t] = tar;
523         }
524         inline void pop_front(){
525             s ++;
526         }
527         inline void pop_back(){
528             t --;
529         }
530         inline T front(){
531             return q[s];
532         }
533         inline T back(){
534             return q[t];
535         }
536     };
537 
538     template <typename T>
539     struct Stack{
540         size_t t;
541         T *s;
542         Stack(){
543             t = 0;
544             s = NULL;
545         }
546         Stack(size_t siz){
547             t = 0;
548             s = (T*)malloc(sizeof(T) * siz);
549             assert(s != NULL);
550         }
551         inline void Re_Init(size_t siz){
552             s = (T*)realloc(s, sizeof(T) * siz);
553             assert(s != NULL);
554         }
555         ~Stack(){
556             delete[] s;
557         }
558         inline void clear(){
559             t = 0;
560         }
561         inline bool empty(){
562             return t == 0;
563         }
564         inline size_t size(){
565             return t;
566         }
567         inline void push(T tar){
568             s[++ t] = tar;
569         }
570         inline T top(){
571             return s[t];
572         }
573         inline void pop(){
574             t --;
575         }
576     };
577 
578 #ifdef NAME_SPACE
579 }
580 #endif
581 
582 #ifdef USING
583 
584 #ifdef NAME_SPACE
585 using LKF::pc;
586 using LKF::read_ch;
587 using LKF::read_dec;
588 using LKF::read;
589 using LKF::Main_Init;
590 using LKF::write;
591 using LKF::upmax;
592 using LKF::upmin;
593 using LKF::max;
594 using LKF::min;
595 using LKF::abs;
596 // using LKF::swap;
597 #else
598 using ::pc;
599 using ::read_ch;
600 using ::read_dec;
601 using ::read;
602 using ::Main_Init;
603 using ::write;
604 using ::upmax;
605 using ::upmin;
606 using ::max;
607 using ::min;
608 using ::abs;
609 // using ::swap;
610 #endif
611 
612 #endif
613 
614 //Source Code
Source Code

 

一个比较妙的头文件工具

标签:bsp   hide   freopen   适配   发布   文件输入输出   修复   换行   main   

原文地址:https://www.cnblogs.com/CreeperLKF/p/9059873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!