using namespace std;
typedef vector<vector<int>> graph;
typedef pair<int, int> pii;
According to https://www.geeksforgeeks.org/c-data-types/
char
1 byte (8 bits)
short int
2 bytes (16 bits)
int
4 bytes (32 bits)
unsigned int
4 bytes (32 bits)
long long int
8 bytes (64 bits)
float
4 bytes (32 bits)
double
8 bytes (64 bits)
INT_MIN
, INT_MAX
: #include <climits>
INT_MIN
-2147483648 = - (2^31)
INT_MAX
2147483647 = (2^31) − 1; 2.15 * (10^9); 二十一亿四千七百四十八万三千六百四十七
65535 = (2^16) - 1; 六万五千五百三十五
1 |
|
FLT_MIN
等 属于<cfloat>
,不属于<climits>
。
numeric_limits<int>::min()
, numeric_limits<int>::max()
: #include <limits>
numeric_limits<int>::min()
同 INT_MIN
numeric_limits<int>::max()
同 INT_MAX
numeric_limits<float>::min()
同 FLT_MIN
numeric_limits<double>::min()
同 DBL_MIN
INT_MIN
等 属于<climits>
,不属于<limits>
。
numeric_limits<int>::min()
等 属于<limits>
,不属于<climits>
。
memset
: #include <cstring>
1 |
|
According to https://www.geeksforgeeks.org/memset-in-cpp/
We can use memset()
to set all values as 0
or -1
for integral data types also. It will not work if we
use it to set as other values. The reason is simple,
memset works byte by byte.
fill_n
: #include <algorithm>
1 |
|
cout
: #include <iostream>
cin
: #include<iostream>
scanf
: #include<iostream>
printf
: #include<iostream>
priority_queue
: #include <queue>
empty(), size(), top(), push(), pop()
1 |
|
In C++, the default STL (Standard Template Library) priority queue is Max priority queue (returns the largest element). Using greater as comparison function to get Min priority queue, like:
priority_queue<int, vector<int>, greater<int> >pq;
To use lambda expressions:
1 |
|
queue
: #include <queue>
empty(), size(), front(), back(), push(), pop()
stack
: #include <stack>
empty(), size(), top(), push(), pop()
vector
: #include <vector>
size(), empty(), front(), back(), push_back(), pop_back()
1 |
|
Doubly-linked lists
list
: #include <list>
1 |
|
Singly-linked lists
forward_list
: #include <forward_list>
1 |
|
pair
: #include <utility> // std::pair, std::make_pair
1 |
|
Sets are typically implemented as binary search trees.
set
: #include <set>
begin(), end(), empty(), size(), max_size(), clear()
1 |
|
unordered_set
: #include <unordered_set>
begin(), end(), find(), count(), insert(), erase(), clear()
Maps are typically implemented as binary search trees.
map
: #include <map>
empty(), size(), max_size(), [], at(), insert(), erase(), clear(), find(), count()
[]
: If k matches the key of an element in the container, the function returns a reference to its mapped value.
If k does not match the key of any element in the container, the function inserts a new element with that
key and returns a reference to its mapped value. Notice that this always increases the container size by one,
even if no mapped value is assigned to the element (the element is constructed using its default constructor).
unordered_map
: #include <unordered_map>
begin(), end(), [], at(), find(), count(), insert(), erase(), clear()
library
1 |
|
less<T>()
: #include <functional>
1 |
|
greater<T>()
: #include <functional>
1 |
|
string
: #include <string>
1 |
|
Lambda function in C++11
1 |
|
Macro definitions: #define
#define TABLE_SIZE 100
#define int32 uint32_t
const int MAXN = 21;
bool adj[MAXN][MAXN];
bool visited[51];
C++ coding competition style IO:
1 |
|
1 |
|
1 |
|
1 |
|
rand()
Returns a pseudo-random integral number in the range between 0
and RAND_MAX
.
RAND_MAX
is a constant defined in <cstdlib>
. Test with http://cpp.sh/ shows that:
cout << RAND_MAX << endl; // 2147483647
1 |
|
According to https://stackoverflow.com/questions/35910043/why-does-rand-compile-without-including-cstdlib-or-using-namespace-std :
rand()
is defined in <cstdlib>
. However, standard library header files may
include other standard library header files. So iostream
may include cstdlib
directly or indirectly.
In practice, it suffices to use #include<iostream>
.
Header files with C-standard library equivalents (e.g. cstdlib
) are allowed to bring C standard library
names into the global namespace, that is, outside of the std
namespace (e.g. rand
) This is formally allowed
since C++11, and was largely tolerated before.
abs()
abs()
is defined in <cmath>
. However, in practice, it suffices to use #include<iostream>
.
Library bitset
1 |
|
Modulo 10^9+7 (1000000007):
https://www.geeksforgeeks.org/modulo-1097-1000000007/
- prevent integer overflows
- In some of the problems, to compute the result modulo inverse is needed and this number helps a lot because it is prime.
A few distributive properties of modulo are as follows:
( a + b) % c = ( ( a % c ) + ( b % c ) ) % c
( a * b) % c = ( ( a % c ) * ( b % c ) ) % c
( a – b) % c = ( ( a % c ) – ( b % c ) ) % c
( a / b ) % c = ( ( a % c ) / ( b % c ) ) % c
is not true!!!!
struct
in C++
1 |
|
Array in C++ (raw array, as opposed to STL std::array
in <array>
)
1 |
|
Compiler uses pointer arithmetic to access array element, arr[i]
is treated as *(arr + i)
by the compiler. However, array and pointer are two different things in general.
1 |
|
hash
: #include <functional>
1 |
|
To create an unordered_map
of user-defined class in C++:
- implement
const
methodoperator==
in the user-defined class - create a
struct
/class
that containsconst
methodoperator()
1 |
|
1 |
|
numeric_limits<double>::epsilon()
:
Machine epsilon (the difference between 1 and the least value greater than 1 that is representable). See also FLT_EPSILON
, DBL_EPSILON
.
1 |
|