一些日常工具集合(C++代码片段)

  ——工欲善其事,必先利其器

  尽管不会松松松,但是至少维持一个比较小的常数还是比较好的

  在此之前依然要保证算法的正确性以及代码的可写性

  本文依然会持久更新,因为一次写不完

Tools1:算法

  这个的重要性就不强调了,轻则多$log$,重则爆$n^2$,更令人窒息者为多项式和非多项式的区别

  设计一个好的算法,首先不要想着如何去用$O(n^2)$碾压$O(n)$,而是先想如何实现$O(n)$才是比较好的

  洛咕日报15期(霸占评论区三天2333),关于基数排序的

Tools2:IO

  IO输出要么没有显著优化,要么直接从TLE优化到AC,在另一篇博客有介绍https://www.cnblogs.com/CreeperLKF/p/8448568.html

  然后下面放上一些我平时用的贴在代码前面且具有不同功能的一些东西:

  1.  短一些,只有一个小的工具包,没有使用要求

 1 #include <cstdio>
 2 #include <cctype>
 3 #include <cstring>
 4
 5 //User's Lib
 6
 7 using namespace std;
 8
 9 char buf[11111111], *pc = buf;
10
11 struct Main_Guard{
12     Main_Guard(){
13         fread(buf, 1, 11111111, stdin);
14     }
15     Main_Guard(const char *ins){
16         freopen(ins, "r", stdin);
17         fread(buf, 1, 11111111, stdin);
18     }
19     Main_Guard(const char *ins, const char *ous){
20         freopen(ins, "r", stdin);
21         freopen(ous, "w", stdout);
22         fread(buf, 1, 11111111, stdin);
23     }
24     ~ Main_Guard(){
25         fclose(stdin), fclose(stdout);
26     }
27 };
28
29 static inline int read(){
30     int num = 0;
31     char c, sf = 1;
32     while(isspace(c = *pc++));
33     if(c == 45) sf = -1, c = *pc ++;
34     while(num = num * 10 + c - 48, isdigit(c = *pc++));
35     return num * sf;
36 }
37
38 namespace LKF{
39     template <typename T>
40     extern inline T abs(T tar){
41         return tar < 0 ? -tar : tar;
42     }
43     template <typename T>
44     extern inline void swap(T &a, T &b){
45         T t = a;
46         a = b;
47         b = t;
48     }
49     template <typename T>
50     extern inline void upmax(T &x, const T &y){
51         if(x < y) x = y;
52     }
53     template <typename T>
54     extern inline void upmin(T &x, const T &y){
55         if(x > y) x = y;
56     }
57     template <typename T>
58     extern inline T max(T a, T b){
59         return a > b ? a : b;
60     }
61     template <typename T>
62     extern inline T min(T a, T b){
63         return a < b ? a : b;
64     }
65 }
66
67 //Source Code

  2.  长一些,分类讨论一些开关,然后不是所有东西保证效率,功能多

  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
 27 using namespace std;
 28
 29 //Debug Port
 30
 31 // #define DEBUG_PORT
 32 #define DEBUG
 33
 34 #ifdef ONLINE_JUDGE
 35 #undef DEBUG_PORT
 36 #undef DEBUG
 37 #endif
 38
 39 #ifdef DEBUG_PORT
 40 #if __cplusplus >= 201103L
 41 #ifdef DEBUG
 42 template<typename T>
 43 extern inline void Debug(T tar){
 44     cerr << tar << endl;
 45 }
 46 template<typename Head, typename T, typename... Tail>
 47 extern inline void Debug(Head head, T mid, Tail... tail){
 48     cerr << head << ' ';
 49     Debug(mid, tail...);
 50 }
 51 #else
 52 template<typename Head, typename T, typename... Tail>
 53 extern inline void Debug(Head, T, Tail...){
 54     return ;
 55 }
 56 #endif
 57 #else
 58 # pragma message "Warning : C++11 Not Use"
 59 #ifdef DEBUG
 60 template <typename T>
 61 extern inline void Debug(T tar){
 62     cerr << tar << endl;
 63 }
 64 #else
 65 template <typename T>
 66 extern inline void Debug(T){
 67     return ;
 68 }
 69 #endif
 70 #endif
 71 #else
 72 template<typename Head, typename T, typename... Tail>
 73 extern inline void Debug(Head, T, Tail...){
 74     return ;
 75 }
 76 template <typename T>
 77 extern inline void Debug(T){
 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     struct Main_Guard{
130         Main_Guard(){
131             #ifndef ONLINE_JUDGE
132             FILE_OPT();
133             pc = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0);
134             #endif
135         }
136         ~ Main_Guard(){
137             #ifdef FAST_WRITE
138             fwrite(outp, 1, op - outp, stdout);
139             #endif
140             fclose(stdin), fclose(stdout);
141         }
142     };
143
144     #else
145
146     char buf[MAX_BUF_SIZE], *pc = buf;
147
148     struct Main_Guard{
149         Main_Guard(){
150             FILE_OPT();
151             fread(buf, 1, MAX_BUF_SIZE, stdin);
152         }
153         ~ Main_Guard(){
154             #ifdef FAST_WRITE
155             fwrite(outp, 1, op - outp, stdout);
156             #endif
157             fclose(stdin), fclose(stdout);
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     template<typename T>
348     extern inline void write(char, T tar){
349         Call_Write(tar);
350         #ifdef WRITE_ENDL
351         write();
352         #endif
353     }
354
355     template<typename Head, typename T, typename... Tail>
356     extern inline void write(char Split, Head head, T mid, Tail... tail){
357         Call_Write(Split, head);
358         write(Split, mid, tail...);
359     }
360
361     #else
362
363     template <typename T>
364     extern inline void write(char Split, T tar){
365         Call_Write(tar);
366         #ifdef WRITE_ENDL
367         write();
368         #endif
369     }
370
371     #endif
372
373     #else
374
375     extern inline void write(){
376         cout << endl;
377     }
378
379     template<typename T>
380     extern inline void write(T tar){
381         cout << tar;
382         #ifdef WRITE_ENDL
383         write();
384         #endif
385     }
386
387     #if __cplusplus >= 201103L
388
389     template<typename T>
390     extern inline void write(char Split, T tar){
391         cout << tar << Split;
392         #ifdef WRITE_ENDL
393         write();
394         #endif
395     }
396
397     template<typename Head, typename T, typename... Tail>
398     extern inline void write(char Split, Head head, T mid, Tail... tail){
399         #ifdef SPLIT_WRITE
400         cout << head << Split;
401         #else
402         cout << head;
403         #endif
404         write(Split, mid, tail...);
405     }
406
407     #else
408
409     template <typename T>
410     extern inline void write(char Split, T tar){
411         cout << tar << Split;
412         #ifdef WRITE_ENDL
413         write();
414         #endif
415     }
416
417     #endif
418
419     #endif
420
421     template <typename T>
422     extern inline void upmax(T &x, const T &y){
423         if(x < y) x = y;
424     }
425     template <typename T>
426     extern inline void upmin(T &x, const T &y){
427         if(x > y) x = y;
428     }
429
430     #if __cplusplus >= 201103L
431
432     template<typename T>
433     extern inline T max(T tar){
434         return tar;
435     }
436
437     template<typename T>
438     extern inline T min(T tar){
439         return tar;
440     }
441
442     template <typename Head, typename T, typename... Tail>
443     extern inline Head max(Head head, T mid, Tail... tail){
444         Head tmp = max(mid, tail...);
445         return head > tmp ? head : tmp;
446     }
447     template <typename Head, typename T, typename... Tail>
448     extern inline Head min(Head head, T mid, Tail... tail){
449         Head tmp = min(mid, tail...);
450         return head < tmp ? head : tmp;
451     }
452
453     #else
454
455     template <typename T>
456     extern inline T max(T a, T b){
457         return a > b ? a : b;
458     }
459     template <typename T>
460     extern inline T min(T a, T b){
461         return a < b ? a : b;
462     }
463
464     #endif
465
466     template <typename T>
467     extern inline T abs(T tar){
468         return tar < 0 ? -tar : tar;
469     }
470     template <typename T>
471     extern inline void swap(T &a, T &b){
472         T t = a;
473         a = b;
474         b = t;
475     }
476 #ifdef NAME_SPACE
477 }
478 #endif
479
480 //Algorithm
481
482 #ifdef NAME_SPACE
483 namespace LKF{
484 #endif
485
486     template <class Tn, size_t ArraySize>
487     struct Queue{
488         size_t s, t;
489         Tn q[ArraySize];
490         Queue(){
491             s = 1, t = 0;
492         }
493         inline void clear(){
494             s = 1, t = 0;
495         }
496         inline bool empty(){
497             return s > t;
498         }
499         inline size_t size(){
500             return t - s + 1;
501         }
502         inline void push(Tn tar){
503             q[++ t] = tar;
504         }
505         inline void pop_front(){
506             s ++;
507         }
508         inline void pop_back(){
509             t --;
510         }
511         inline Tn front(){
512             return q[s];
513         }
514         inline Tn back(){
515             return q[t];
516         }
517     };
518
519     template <class Tn, size_t ArraySize>
520     struct Stack{
521         size_t t;
522         Tn s[ArraySize];
523         Stack(){
524             t = 0;
525         }
526         inline void clear(){
527             t = 0;
528         }
529         inline bool empty(){
530             return t == 0;
531         }
532         inline size_t size(){
533             return t;
534         }
535         inline void push(Tn tar){
536             s[++ t] = tar;
537         }
538         inline Tn top(){
539             return s[t];
540         }
541         inline void pop(){
542             t --;
543         }
544     };
545
546 #ifdef NAME_SPACE
547 }
548 #endif
549
550 #ifdef USING
551
552 #ifdef NAME_SPACE
553 using LKF::pc;
554 using LKF::read_ch;
555 using LKF::read_dec;
556 using LKF::read;
557 using LKF::write;
558 using LKF::upmax;
559 using LKF::upmin;
560 using LKF::max;
561 using LKF::min;
562 using LKF::abs;
563 // using LKF::swap;
564 #else
565 using ::pc;
566 using ::read_ch;
567 using ::read_dec;
568 using ::read;
569 using ::write;
570 using ::upmax;
571 using ::upmin;
572 using ::max;
573 using ::min;
574 using ::abs;
575 // using ::swap;
576 #endif
577
578 #endif
579
580 //Source Code

  3.  C++11下可以使用调试多参数调试 Debug(arg1, arg2...) ,建议搭配dot可以直接图论题中绘图,可以不删除调试代码交到Luogu上

  1 #include <cstdio>
  2 #include <cctype>
  3 #include <cstring>
  4 #include <iostream>
  5
  6 //User's Lib
  7
  8 using namespace std;
  9
 10 // #define DEBUG_PORT
 11 #define DEBUG
 12
 13 #ifdef ONLINE_JUDGE
 14 #undef DEBUG_PORT
 15 #undef DEBUG
 16 #endif
 17
 18 #ifdef DEBUG_PORT
 19 #if __cplusplus >= 201103L
 20 #ifdef DEBUG
 21 template<typename T>
 22 extern inline void Debug(T tar){
 23     cerr << tar << endl;
 24 }
 25 template<typename Head, typename T, typename... Tail>
 26 extern inline void Debug(Head head, T mid, Tail... tail){
 27     cerr << head << ' ';
 28     Debug(mid, tail...);
 29 }
 30 #else
 31 template<typename Head, typename T, typename... Tail>
 32 extern inline void Debug(Head, T, Tail...){
 33     return ;
 34 }
 35 #endif
 36 #else
 37 # pragma message "Warning : C++11 Not Use"
 38 #ifdef DEBUG
 39 template <typename T>
 40 extern inline void Debug(T tar){
 41     cerr << tar << endl;
 42 }
 43 #else
 44 template <typename T>
 45 extern inline void Debug(T){
 46     return ;
 47 }
 48 #endif
 49 #endif
 50 #else
 51 template<typename Head, typename T, typename... Tail>
 52 extern inline void Debug(Head, T, Tail...){
 53     return ;
 54 }
 55 template <typename T>
 56 extern inline void Debug(T){
 57     return ;
 58 }
 59 #endif
 60
 61 char buf[11111111], *pc = buf;
 62
 63 struct Main_Guard{
 64     Main_Guard(){
 65         fread(buf, 1, 11111111, stdin);
 66     }
 67     Main_Guard(const char *ins){
 68         freopen(ins, "r", stdin);
 69         fread(buf, 1, 11111111, stdin);
 70     }
 71     Main_Guard(const char *ins, const char *ous){
 72         freopen(ins, "r", stdin);
 73         freopen(ous, "w", stdout);
 74         fread(buf, 1, 11111111, stdin);
 75     }
 76     ~ Main_Guard(){
 77         fclose(stdin), fclose(stdout);
 78     }
 79 };
 80
 81 static inline int read(){
 82     int num = 0;
 83     char c, sf = 1;
 84     while(isspace(c = *pc++));
 85     if(c == 45) sf = -1, c = *pc ++;
 86     while(num = num * 10 + c - 48, isdigit(c = *pc++));
 87     return num * sf;
 88 }
 89
 90 namespace LKF{
 91     template <typename T>
 92     extern inline T abs(T tar){
 93         return tar < 0 ? -tar : tar;
 94     }
 95     template <typename T>
 96     extern inline void swap(T &a, T &b){
 97         T t = a;
 98         a = b;
 99         b = t;
100     }
101     template <typename T>
102     extern inline void upmax(T &x, const T &y){
103         if(x < y) x = y;
104     }
105     template <typename T>
106     extern inline void upmin(T &x, const T &y){
107         if(x > y) x = y;
108     }
109     template <typename T>
110     extern inline T max(T a, T b){
111         return a > b ? a : b;
112     }
113     template <typename T>
114     extern inline T min(T a, T b){
115         return a < b ? a : b;
116     }
117 }
118
119 //Source Code

简单

  4.  只能读非负整数,然后用的是更快的读入,但是在本地都开了文件输入输出

 1 #include <cstdio>
 2
 3 #include <fcntl.h>
 4 #include <unistd.h>
 5 #include <sys/mman.h>
 6
 7 //User's Lib
 8
 9 using namespace std;
10
11 char *pc;
12
13 char outp[1111111], *op = outp;
14
15 struct Main_Guard{
16     Main_Guard(){
17         pc = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0);
18     }
19     Main_Guard(const char *ins){
20         freopen(ins, "r", stdin);
21         pc = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0);
22     }
23     Main_Guard(const char *ins, const char *ous){
24         freopen(ins, "r", stdin);
25         freopen(ous, "w", stdout);
26         pc = (char *) mmap(NULL, lseek(0, 0, SEEK_END), PROT_READ, MAP_PRIVATE, 0, 0);
27     }
28     ~ Main_Guard(){
29         fwrite(outp, 1, op - outp, stdout);
30         fclose(stdin), fclose(stdout);
31     }
32 };
33
34 inline int read(){
35     int num = 0;
36     char c;
37     while((c = *pc ++) < 48);
38     while(num = num * 10 + c - 48, (c = *pc ++) >= 48);
39     return num;
40 }
41
42 inline void Write(const char *tar){
43     for(register int i = 0, lim = strlen(tar); i < lim; i++)
44         *op ++ = tar[i];
45 }
46
47 inline void Write(const int &tar){//You Need To Write '-' and '\n' By Hand
48     if(!tar) return ;
49     Write(tar / 10);
50     *op ++ = (tar % 10) ^ 48;
51 }
52
53 namespace LKF{
54     template <typename T>
55     extern inline T abs(T tar){
56         return tar < 0 ? -tar : tar;
57     }
58     template <typename T>
59     extern inline void swap(T &a, T &b){
60         T t = a;
61         a = b;
62         b = t;
63     }
64     template <typename T>
65     extern inline void upmax(T &x, const T &y){
66         if(x < y) x = y;
67     }
68     template <typename T>
69     extern inline void upmin(T &x, const T &y){
70         if(x > y) x = y;
71     }
72     template <typename T>
73     extern inline T max(T a, T b){
74         return a > b ? a : b;
75     }
76     template <typename T>
77     extern inline T min(T a, T b){
78         return a < b ? a : b;
79     }
80 }
81
82 //Source Code

快速

  5.  C++11特性用,删掉了分类讨论

  1 #include <cstdio>
  2 #include <cctype>
  3 #include <cstring>
  4 #include <iostream>
  5
  6 //User's Lib
  7
  8 using namespace std;
  9
 10 #define DEBUG
 11
 12 #ifdef ONLINE_JUDGE
 13 #undef DEBUG
 14 #endif
 15
 16 #ifdef DEBUG
 17     template<typename T>
 18     extern inline void Debug(T tar){
 19         cerr << tar << endl;
 20     }
 21     template<typename Head, typename T, typename... Tail>
 22     extern inline void Debug(Head head, T mid, Tail... tail){
 23         cerr << head << ' ';
 24         Debug(mid, tail...);
 25     }
 26 #else
 27     template<typename Head, typename T, typename... Tail>
 28     extern inline void Debug(Head, T, Tail...){
 29         return ;
 30     }
 31 #endif
 32
 33 char buf[11111111], *pc = buf;
 34
 35 struct Main_Guard{
 36     Main_Guard(){
 37         fread(buf, 1, 11111111, stdin);
 38     }
 39     Main_Guard(const char *ins){
 40         freopen(ins, "r", stdin);
 41         fread(buf, 1, 11111111, stdin);
 42     }
 43     Main_Guard(const char *ins, const char *ous){
 44         freopen(ins, "r", stdin);
 45         freopen(ous, "w", stdout);
 46         fread(buf, 1, 11111111, stdin);
 47     }
 48     ~ Main_Guard(){
 49         fclose(stdin), fclose(stdout);
 50     }
 51 };
 52
 53 static inline int read(){
 54     int num = 0;
 55     char c, sf = 1;
 56     while(isspace(c = *pc++));
 57     if(c == 45) sf = -1, c = *pc ++;
 58     while(num = num * 10 + c - 48, isdigit(c = *pc++));
 59     return num * sf;
 60 }
 61
 62 namespace LKF{
 63     template <typename T>
 64     extern inline void upmax(T &x, const T &y){
 65         if(x < y) x = y;
 66     }
 67     template <typename T>
 68     extern inline void upmin(T &x, const T &y){
 69         if(x > y) x = y;
 70     }
 71
 72     template<typename T>
 73     extern inline T max(T tar){
 74         return tar;
 75     }
 76
 77     template<typename T>
 78     extern inline T min(T tar){
 79         return tar;
 80     }
 81
 82     template <typename Head, typename T, typename... Tail>
 83     extern inline Head max(Head head, T mid, Tail... tail){
 84         Head tmp = max(mid, tail...);
 85         return head > tmp ? head : tmp;
 86     }
 87     template <typename Head, typename T, typename... Tail>
 88     extern inline Head min(Head head, T mid, Tail... tail){
 89         Head tmp = min(mid, tail...);
 90         return head < tmp ? head : tmp;
 91     }
 92
 93     template <typename T>
 94     extern inline T abs(T tar){
 95         return tar < 0 ? -tar : tar;
 96     }
 97     template <typename T>
 98     extern inline void swap(T &a, T &b){
 99         T t = a;
100         a = b;
101         b = t;
102     }
103 }
104
105 //Source Code

C++ 11

  6.  最简单的快读

 1 #include <cstdio>
 2
 3 using namespace std;
 4
 5 char buf[11111111], *pc = buf;
 6
 7 struct Main_Guard{
 8     Main_Guard(){
 9         fread(buf, 1, 11111111, stdin);
10     }
11     Main_Guard(const char *ins){
12         freopen(ins, "r", stdin);
13         fread(buf, 1, 11111111, stdin);
14     }
15     Main_Guard(const char *ins, const char *ous){
16         freopen(ins, "r", stdin);
17         freopen(ous, "w", stdout);
18         fread(buf, 1, 11111111, stdin);
19     }
20     ~ Main_Guard(){
21         fclose(stdin), fclose(stdout);
22     }
23 };
24
25 inline int read(){
26     int num = 0;
27     char c;
28     while((c = *pc ++) < 48);
29     while(num = num * 10 + c - 48, (c = *pc ++) >= 48);
30     return num;
31 }
32
33 //Source Code

快读

由于最近改了一下,所以可能还会有一点Bug

Main_Guard调用方法就是在main函数开头写 Main_Guard main_guard; 如果需要读入的话就在后面带括号和文件名,输出的话就带两个字符串

这样的好处就是一定会调用析构函数

Tools3:__builtin_

  一个非常妙而且实用的工具,有些C++库函数会调用到,不过自己去学会调用肯定会比较妙,整个Builtin家族非常大(看文档https://gcc.gnu.org/onlinedocs/gcc/,具体可以在https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#Other-Builtins查找),这里只介绍一些常用的。下面的函数的参数以及返回值对照了gcc文档。

  二进制相关

  (注意下面函数定义在unsigned int(或int)上,如果需要使用unsigned long long(或long long)版则可以在函数名后面加上ll,例如__builtin_ffsll接受long long的参数)(还有一个__builtin_clrsb看不懂什么玩意)。

  1.    int __builtin_ffs (int x) :查询x二进制最后一个1是第几位
  2.    int __builtin_clz (unsigned int x) :查询x有多少个前导0,If x is 0, the result is undefined.
  3.    int __builtin_ctz (unsigned int x) :查询x有多少个后缀0,If x is 0, the result is undefined.
  4.    int __builtin_popcount (unsigned int x) :查询x有多少个1,在洛谷日爆未来的某期内我还了解到了还可以加#pragma GCC target ("popcnt")直接把这玩意转化成指令加速,优化效果高达70%(网上某些题的实验)
  5.    int __builtin_parity (unsigned int x) :查询x的二进制奇偶校验,也就是x中1的个数模2
  6.    uint16_t __builtin_bswap16 (uint16_t x) :按照字节翻转二进制位,例如0xaabb翻转成为0xbbaa,然后16可替换为32,64,表示位数

  CPU分支预测优化

   long __builtin_expect (long exp, long c)

    函数的作用就是引导CPU在分支预测的时候选择某个if分支执行,以前靠这个东西直接把一道题卡到了0ms,单点甩Rank2接近8ms,可见有些情况下效果还是不错的

    食用方法:exp那里填写你希望预测的一个表达式,建议不要写的太复杂了,c填写你预测表达式大概率会是什么值,然后直接用在if内即可,例如 if(__builtin_expect(a === b, 0))

    效果:如果写的比较好的话有一些大优化,否则会导致CPU频繁罚时

    例子:例如你知道某个数据结构它的体型庞大而且你有特意地防止node指针指向NULL,但是如果你不放心的话那么可以加一个这个。例如表达式$i%100000000==1$一般会比较难成立,可以优化,而$i%10==1$就不需要这样优化了

  提前写入缓存

   void __builtin_prefetch (const void *addr, ...)

    就是提前把数据取到缓存

    gcc的参数表最后面还有两个可选参数,rw和locality,表示使用这个数据是读或者写(1是写0是读),以及这个数据的时间局部性。

    读或者写就不说了

    时间局部性表示这个数据在被访问之后在一定时间内还会不会再次访问(而不是距离第一次访问还有多久),决定了这个数据在缓存中的“寿命”。0表示没有时间局部性,也就是很长时间内不再访问,而3表示高时间局部性,表示访问了时候很快又会访问。这个参数一定是个编译时候的常量,默认为3.

Tools4:细节

  首先register和inline标记应该都会吧。register表示建议编译器将变量放在编译器中,C++11及以上为了防止滥用渐渐开始忽略这个东西,在C++17时你会被提醒这个东西已经不能用了。inline在我讲快读那篇文章的末尾有https://www.cnblogs.com/CreeperLKF/p/8448568.html

  例如下面Luogu上两份A+B Problem(找LZL123测试的),代码相差无几,但是时间差了584ms,空间差了63971kb,差距竟然只有1000的数据范围

  见提交记录https://www.luogu.org/record/show?rid=6205849和https://www.luogu.org/recordnew/show/5650350

  代码区别:

  584ms:

 1 #include <cstdio>
 2 int x[1<<24];
 3 int main()
 4 {
 5     int a,b,ans=0;
 6     for(int i=1;i<=1<<24;++i)
 7         x[i]++,ans+=x[i];
 8     scanf("%d%d",&a,&b);
 9     printf("%d",a+b);
10     return 0;
11 }

  0ms:

 1 #include <cstdio>
 2 int x[1<<24+1000];
 3 int main()
 4 {
 5     int a,b,ans=0;
 6     for(int i=1;i<=1<<24+1000;++i)
 7         x[i]++,ans+=x[i];
 8     scanf("%d%d",&a,&b);
 9     printf("%d",a+b);
10     return 0;
11 }

关于register:这种东西比较奇怪,现代CPU确实先进了不少,但是某些OJ上是有效的,例如提交记录

没有优化

register优化

O2优化

O2+register

可能存在干扰因素,有兴趣的可以自己去各大OJ调查一下(例如上次我在HDU加了优化之后还变慢了)

Tools5:强力Random工具

这个Random工具就是用mt19937(C++ Reference Wiki)加上Linux下的/dev/random和/dev/urandom以及某种非常高精度的时钟实现的,速度可观,可以在Windows下运行

然后测试效果海星,其中的宏也已经定义好,开袋即食,您的造数据好帮手

注意如果要大量生成数据的话不要使用/dev/random,否则会比较卡(如果必须的话你可以手动调节一下中间的Init_Rand_Buf)

注意某些函数可能会被成功inline,调试的时候如果发现这个函数“没有调用”不要慌

持续更新QWQ

  1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~Random_Part~~~~~~~~~~~~~~~~~~~~~~~~
  2
  3 //食用说明:在一开始需要调用Rand_Initialize
  4 //如果需求量和速度要求不高的话可以打开DO_NOT_USE_URANDOM
  5 //如果在Linux的话可以打开RAND_BUF
  6 //这样可以让随机位取地比较好
  7 //为什么要每8位取1次?因为发现如果8位取8个的话好像随机性不优?
  8
  9 #include <chrono>
 10 #include <random>
 11 #include <algorithm>
 12
 13 #ifdef __gnu_linux__
 14 #define RAND_BUF
 15 // #define DO_NOT_USE_URANDOM
 16 #endif
 17
 18 #ifdef RAND_BUF
 19 #include <fcntl.h>
 20 #include <unistd.h>
 21 #include <sys/types.h>
 22 #include <sys/stat.h>
 23 #endif
 24
 25 using namespace std;
 26
 27 mt19937 Generator;
 28
 29 #define BUF_SIZE 8193
 30
 31 #ifdef RAND_BUF
 32 char rand_buf[BUF_SIZE], *p1 = rand_buf, *p2 = rand_buf;
 33 int buf_times, rand_fd;
 34 inline void Init_Rand_Buf(){
 35     buf_times ++;
 36     if(buf_times < 127) p2 = (p1 = rand_buf) + read(rand_fd, rand_buf, sizeof(rand_buf));
 37     if(buf_times == 127 || p1 + BUF_SIZE != p2){
 38         if(buf_times == 127) buf_times = 0;
 39         for(int i = 0; i < BUF_SIZE; i++) rand_buf[i] = Generator();
 40         p2 = (p1 = rand_buf) + BUF_SIZE;
 41     }
 42 }
 43 inline int Rand_Bit(){
 44     if(p1 == p2) Init_Rand_Buf();
 45     return (*p1 ++ & 1);
 46 }
 47 #endif
 48
 49 inline void Rand_Initialize(){
 50     unsigned seed1 = chrono::system_clock::now().time_since_epoch().count();
 51
 52     #ifdef RAND_BUF
 53     #ifdef DO_NOT_USE_URANDOM
 54     rand_fd = open("/dev/random", 0);
 55     #else
 56     rand_fd = open("/dev/urandom", 0);
 57     #endif
 58     Init_Rand_Buf();
 59     *(-- p2) = seed1 & 0xff;
 60     *(-- p2) = (seed1 >> 8) & 0xff;
 61     *(-- p2) = (seed1 >> 16) & 0xff;
 62     *(-- p2) = (seed1 >> 24) & 0xff;
 63     p2 ++, p2 ++, p2 ++, p2 ++;
 64     seed_seq seed2(p1, p2);
 65     Generator = mt19937(seed2);
 66     #else
 67     Generator mt19937(seed1);
 68     #endif
 69 }
 70
 71 inline unsigned Rand(){
 72     return Generator();
 73 }
 74
 75 inline unsigned Rand(unsigned up){
 76     return Rand() % up + 1;
 77 }
 78
 79 inline unsigned Rand(unsigned down, unsigned up){
 80     return Rand() % (up - down + 1) + down;
 81 }
 82
 83 inline double Rand_Float(){
 84     return (double)Rand() / (unsigned)0xffffffff;
 85 }
 86
 87 inline unsigned long long Rand_ull(){
 88     return (unsigned long long)Rand() * Rand();
 89 }
 90
 91 #ifndef RAND_BUF
 92 inline int Rand_Bit(){
 93     return Rand() & 1;
 94 }
 95 #endif
 96
 97 inline unsigned Rand_Number(unsigned up){
 98     return Rand() % up;
 99 }
100
101 template <typename Iterator_Type>
102 inline void Random_Shuffle(Iterator_Type __first, Iterator_Type __last){
103     random_shuffle(__first, __last, Rand_Number);
104 }
105
106 //~~~~~~~~~~~~~~~~~~~~~~~~~~~Random_Part~~~~~~~~~~~~~~~~~~~~~~~~

Tools6:其他的东西

  1 //~~~~~~~~~~~~~~~~~~~~~~~Graph_Code~~~~~~~~~~~~~~~~~~~~
  2
  3 template <size_t VS, size_t ES>
  4 struct __Graph_Base{
  5     int tot;
  6     int beginx[VS], endx[ES], nxt[ES];
  7     __Graph_Base() : tot(1){
  8         memset(beginx, 0, sizeof(beginx));
  9     }
 10     inline void clear(){
 11         tot = 1;
 12         memset(beginx, 0, sizeof(beginx));
 13     }
 14 };
 15
 16 template <size_t VS, size_t ES>
 17 struct Graph : __Graph_Base<VS, ES>{
 18
 19     using __Graph_Base<VS, ES>::tot;
 20     using __Graph_Base<VS, ES>::nxt;
 21     using __Graph_Base<VS, ES>::beginx;
 22     using __Graph_Base<VS, ES>::endx;
 23
 24     inline void clear(){
 25         __Graph_Base<VS, ES>::clear();
 26     }
 27     inline void add_edge(const int &u, const int &v){
 28         nxt[++ tot] = beginx[u], beginx[u] = tot, endx[tot] = v;
 29         nxt[++ tot] = beginx[v], beginx[v] = tot, endx[tot] = u;
 30     }
 31 };
 32
 33 //~~~~~~~~~~~~~~~~~~~~~~~Graph_Code~~~~~~~~~~~~~~~~~~~~
 34
 35 //~~~~~~~~~~~~~~~~~~~~~~~Matrix_Code~~~~~~~~~~~~~~~~~~~
 36
 37 template <size_t Matrix_Size>
 38 struct Matrix{
 39     int a[Matrix_Size][Matrix_Size];
 40
 41     Matrix(){
 42         memset(a, 0, sizeof(a));
 43     }
 44
 45     Matrix(const int &tar){
 46         memset(a, 0, sizeof(a));
 47         for(register int i = 1; i <= MAX; i++)
 48             a[i][i] = tar;
 49     }
 50
 51     inline int * operator [] (const int &tar){
 52         return a[tar];
 53     }
 54
 55     inline const int * operator [] (const int &tar) const {
 56         return a[tar];
 57     }
 58
 59     inline Matrix operator * (const Matrix &tar) const {
 60         Matrix ret;
 61         for(register int i = 1; i <= MAX; i++)
 62             for(register int k = 1; k <= MAX; k++)
 63                 for(register int j = 1; j <= MAX; j++)
 64                     ret[i][j] += a[i][k] * tar.a[k][j];
 65         return ret;
 66     }
 67
 68     inline Matrix & operator *= (const Matrix &tar){
 69         return *this = (*this) * tar;
 70     }
 71
 72     template <typename T>
 73     inline Matrix operator ^ (register T tar) const {
 74         Matrix ret(1), tmp = *this;
 75         while(tar){
 76             if(tar & 1) ret *= tmp;
 77             tmp *= tmp;
 78             tar >>= 1;
 79         }
 80         return ret;
 81     }
 82
 83     template <typename T>
 84     inline Matrix & operator ^= (const T &tar){
 85         return *this = (*this) ^ tar;
 86     }
 87
 88     template <typename T>
 89     inline Matrix pow(register T tar, Matrix Init_Matrix) const {
 90         Matrix ret(1);
 91         while(tar){
 92             if(tar & 1) ret *= Init_Matrix;
 93             Init_Matrix *= Init_Matrix;
 94             tar >>= 1;
 95         }
 96         return ret;
 97     }
 98
 99 };
100
101 //~~~~~~~~~~~~~~~~~~~~~~~Matrix_Code~~~~~~~~~~~~~~~~~~~

关于Graph:就是有时候同一道题可能会有很多张图,然后你要调试的话就可以把这个东西写开,然后为每一种图加一个Debug,或者是有时候需要同时维护有向图和无向图

关于Matrix:就不解释了QWQ

转载于:https://www.cnblogs.com/CreeperLKF/p/9314916.html

一些日常工具集合(C++代码片段)相关推荐

  1. 程序员折腾之路---日常工具集合整理之软件篇

    时间:2021年3月25日,觉得是时候该写点什么了."时间像一头野驴呀,跑起来就不停" --------吴站长(>_>) 不知不觉工作快一年了,身为程序员日常折腾不少, ...

  2. 工具集合-脚本合并(Groovy)

    说明:只针对于groovy脚本,只能将两个脚本合并起来,不会进行去重,内容不会重复,然后也可以进行执行,验证脚本语法也没问题.2个以上也可以的,只是用了最笨的方法实现了需求.别去看怎么写的了,能套死你 ...

  3. iview代码片段 去除水平menu底部横线

    此处记录日常写的小代码片段 直接使用iview的Menu在底部会出现一条border线,而我的使用场景,不需要border线.所以我就覆盖一下它原来的样式,就可以了. 代码: .ivu-menu-li ...

  4. VS code代码片段

    代码片段的作用及生成方式 在我们使用vue的过程中,有些代码片段是经常需要些的,我们就可以在VS code中生成一个代码片段,方便我们快速生成 VS code的代码片段有固定的格式,所以我们一般会借助 ...

  5. sublime 3 新建代码片段

    sublime 3 新建代码片段 新建代码片段的目的就是快速实现自定义的代码,从而提高开发效率. 1. 点击 sublime 菜单栏中的工具 –> 插件开发 –> 新建代码片段 选项 2. ...

  6. 第一百一十八期:运行 JavaScript 代码片段的 20 种工具

    运行 JavaScript 代码片段的 20 种工具 前端日常开发中,我们使用喜爱的 IDE 调试 JavaScript 代码,比如我喜欢的代码编辑器有两个,Sublime Text 3 和 VS C ...

  7. 【编程技巧】Code snippets(代码片段)在Visual Studio中的使用(附简单的Snippet管理工具)

    引言 Code snippet (代码片段)在VS中指的是基于IDE支持的利用快捷方式快速输入一小段,或者称之为一整块代码的功能,在日常编程,特别是在工作中写内容相似的业务代码时,利用Snippet功 ...

  8. 代码片段管理工具_VS代码片段:提高编码效率的最强大工具

    代码片段管理工具 by Sam Williams 通过山姆·威廉姆斯 VS代码片段:提高编码效率的最强大工具 (VS Code snippets: the most powerful tool to ...

  9. 推荐7款非常棒的将代码片段转换成图片的工具

    本文首发于[码友网]–<我来推荐,你随意挑!推荐7款非常棒的将代码片段转换成图片的工具> 概述 大家好,我是码友网的维护者–Rector.很高兴今天又为大家分享有趣并有用的开发者工具啦! ...

最新文章

  1. 浅析 Linux 初始化系统(系统服务管理和控制程序/init system) -- UpStart
  2. 十字路口红绿灯plc程序_实例讲解红绿灯PLC程序设计方法
  3. php记录登录时间,php记录 用户当前页面停留时间
  4. 蓝桥杯练习题:保留100位小数的黄金分割数
  5. SQL数据库语言基础之SqlServer系统函数、聚合集合函数【大总结】
  6. python的turtle模块(海龟画图)
  7. EntityFramework6 用 PostgreSQL
  8. unity之EasyAR使用
  9. 计算机触摸板设置方法,笔记本电脑触摸板设置
  10. html富强民主鼠标特效,给网站添加鼠标点击特效富强、民主、和谐 js效果 - 寒星皓月...
  11. Python:实现reverse letters反向字母算法(附完整源码)
  12. Altium Designer 19 安装方法
  13. S5PV210 iNAND/SD卡
  14. 小福利,用selenium模块爬取qq音乐歌单
  15. 神经网络基本原理简明教程-0-基本函数导数公式
  16. 黑客安全专家郭盛华:逃避僵尸网络恶意软件攻击的13种方法
  17. 安卓电子书格式_如何将电子书导入kindle App?
  18. C# 数组内元素合并转换成以指定字符连接的字符串
  19. 对冲基金表现大盘点(一):DE Shaw
  20. Catch That Cow

热门文章

  1. 为什么接口中要允许添加默认方法?
  2. 39.什么是操作系统(os)
  3. 疫情期间在公共场所要全程佩戴口罩,不要抱有侥幸心理
  4. 我国人工智能专利申请量去年超3万件,广东领先
  5. Arxiv最新论文,深度学习热点论文,Arxiv每日更新
  6. android 代码设置alignleft,如何动态的设置Relative Layout中按钮的layout_align_parent_right属性...
  7. 天涯社区服务器位置,天涯到底怎么了,哪份帖子都打不开,是服务器的问题吗...
  8. eclipse没有日志_IPFS技术最新进展:抵抗eclipse攻击的能力
  9. Oracle 约束 Constraint
  10. synchronize与lock的区别