Today, I want to talk to you about the header files in C++. When I was writing an algorithm to show the source code, many friends left me messages saying that they were shocked by the contents in my header file. In fact, the reason why my header file is so complicated is entirely because I have been using it since the acm competition in college. For the players in the acm competition, such a header file is actually a pediatrics.
Today, let's take a look at the secrets hidden in acmer header files.
First, let's look at my complete header file code.
# include & ltiostream & gt
# include & ltcstdio & gt
# include & ltcstring & gt
# include & lt string & gt
# include & ltcstdlib & gt
# include & ltcmath & gt
# include & lt queue & gt
# include & ltvector & gt
# include & lt map>
# include & ltset & gt
# include & lt algorithm & gt
# include & lt functionality & gt
# define rep(i, a, b) for (int I = a; I & ltb;; i++)
# define Rep(i, a, b) for (int I = a; Me & gtb;; I-)
#define foreach(e,x)for(_ _ type of(x . begin())e = x . begin(); e! = x . end(); e++)
# define mid ((l+r) >; & gt 1)
# Define Aelsson (k<& lt 1)
# define rson (k <; & lt 1| 1)
# define MEM(a, x) memory set (a, x, sizeof a)
# Define pii pair & ltint, int>
# Define LL long long
usingnamespacestd
constint N = 500005
constlonglong Mod = 99997867
Please click to enter a picture description.
Include parts.
First, let's look at the inclusion part. Let's look at iostream one by one. Needless to say, the C++ standard I/O header file contains the C++ iostream function, which is the classic cin and cout. Speaking of cin and cout, the overhead of cin and cout in C language is much slower than that of scanf and printf, which easily affects the performance of the program. So for acmer, what scanf and printf can do, cin and cout can never do. Of course, scanf and printf are not the fastest, and there are faster getchar and putchar, so some players will read char manually and turn it into int or float function, which is unnecessary in most cases.
The function of Cstdio is the same as iostream, but it is just an input and output function in C language, so I won't say much.
Cstring also belongs to C language, which is a string library in C language and has many string-related functions. Similarly, for performance reasons, if you can use char[] in C language, you don't need to use C++ string. Needless to say, the next string library is the string processing library of C++.
The library function of cstdlib is equivalent to stdlib.h in C language, which encapsulates some commonly used library functions, such as rand, s rand, free, malloc, etc.
Cmath library is equivalent to math.h in C language, and encapsulates some library functions related to mathematical operations, such as pow and sqrt.
The following queues, vectors, maps and collections are all STL libraries, which contain some useful data structures. For example, queue encapsulates queue and dequeue, and priority_queue, that is, queue, dequee and priority queue. Vector, mapping and set are linear table, mapping table and set respectively. Using them skillfully can greatly reduce the complexity of coding.
The algorithm library is translated into the algorithm library, which naturally encapsulates many algorithms. For example, sort sorting, reverse flip, next full permutation under next _ permission, lower_bound, upper_bound functions, etc.
The last function library, which is not used much, encapsulates some operations on function objects as the name suggests. Such as bind, reference_wrapper and so on.
In fact, there is a clever way to include all the header files at the same time:
# include & ltbits/stdc++。 h & gt
However, there is a small problem with this method, which is not supported by all environments, especially official competitions. So generally speaking, everyone will use it in some online competitions. Personally, I am too lazy to distinguish between environments, so I am used to including them one by one.
Please click to enter a picture description.
Define parts
Define is a very powerful function in C++, which can define rules instead of code. Skillful use of define can also greatly simplify coding. However, it should be noted that everything should not go too far. Too many definitions will affect the readability of the program and may also affect other people's coding. Therefore, many large companies prohibit the use of define. Personally, I don't think it's that serious Define can be used, as long as it is used correctly according to the specification.
The first line is these two lines, which are the definitions of the for loop.
# define rep(i, a, b) for (int I = a; I & ltb;; i++)
# define Rep(i, a, b) for (int I = a; Me & gtb;; I-)
#define foreach(e,x)for(_ _ type of(x . begin())e = x . begin(); e! = x . end(); e++)
Rep is short for repeat. Only rep(i, A, B) is needed to replace the long for loop, where I is the loop variable, and A and B are the upper and lower bounds of the loop respectively. Note that the interval is left closed and right opened.
Rep is the same logic, but it's just a reverse cycle.
Foreach uses the new feature of C++ 1 1, which can realize automatic iteration and is very convenient in some scenarios.
# define mid ((l+r) >; & gt 1)
This line is used in method of bisection, the left boundary is L and the right boundary is R, so their midpoint is (l+r)/2, which is expressed by bit operation: (L+R) > >; 1。 Example:
Before # define
while(l+ 1 & lt; r) {
int m = (l + r)>& gt 1;
if(a[m]& lt; = v) {
l = m;
} Otherwise {
r = m;
}
}
After # define
while(l+ 1 & lt; r) {
if(a[mid]& lt; = v) {
l = mid
} Otherwise {
r = mid
}
}
# Define Aelsson (k<& lt 1)
# define rson (k <; & lt 1| 1)
These two lines are mainly used for line segment trees, because C++ often does not use classes to realize line segment trees, but uses arrays to simulate them. In the segment tree, if the id of a node is U, then its left child is 2 x u, and its right child's id is 2 x u+ 1, which is expressed as U by bit operation.
# define MEM(a, x) memory set (a, x, sizeof a)
# Define pii pair & ltint, int>
# Define LL long long
The last three lines are put together, and the first line is the abbreviation of memset, which can be used to initialize the array. Pii is abbreviated as pair.
Regarding the redefinition of types, it is not good to use define here. A better way is to use typedef. For example, the above pair and long long can be written as:
Typedef pair & ltint, int & gtpii
typedeflonglong LL
This is more standardized than the definition, because the definition is a blunt string substitution, and typedef is a type alias, which can be checked by the compiler, so typedef or typedef can be used.
Please click to enter a picture description.
End of movement
In addition to the above-mentioned header files, there are some higher-end uses, such as some template classes, some commonly used algorithms, such as gcd and so on. But personally, I don't think it means much. For the code link of interview and written test, the above header file is enough.
Apple iCloud data center put into operation; Netease announced the result of HR processing of inappropriate comments: expulsion; OceanBase database will be open source | geek headlines
Should programmers retire at 35?
From "Don't be evil" to "The door is over there", the ethical history of Google AI.