forked from sarthak-2019/CP-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC++_Tricks for CP.cpp
More file actions
136 lines (94 loc) · 2.43 KB
/
C++_Tricks for CP.cpp
File metadata and controls
136 lines (94 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Tricks for Competitive Programming
// To Check if the number is odd or even using bitwise
if (num & 1)
cout << "ODD";
else
cout << "EVEN";
// End
// Fast Multiplication or Division by 2
n = n << 1; // Multiply n with 2
n = n >> 1; // Divide n by 2
// End
// Swapping Two Numbers using Xor
a ^= b;
b ^= a;
a ^= b;
// End
// Don't use strlen to traverse the string instead use
for(int i=0;s[i];i++)
// End
// Calculate Number of digits using log instead of using loop
int digits=log10(number)+1;
// End
// To check if a number is a power of Two
bool check(int N)
{
return N && !(N&(N-1));
}
// End
// Built in algorithms for checking if a a array has positive or negative numbers
// Checking if no element is negative
none_of(arr, arr+size, [](int x){ return x<0; })?
cout << "No negative elements" :
cout << "There are negative elements";
// Checking if any element is negative
any_of(arr, arr+size, [](int x){ return x<0; })?
cout << "There exists a negative element" :
cout << "All are positive elements";
// Checking if all elements are positive
all_of(arr, arr+size, [](int x) { return x>0; })?
cout << "All are positive elements" :
cout << "All are not positive elements";
// Note: Here size=the size of the declared Array arr.
// End
// Use Auto keyword instead of lengthy for loop in all Data Structures such as listed
// Power Of Auto
int A[n];
for(auto i:A)
{
cout<<i<<" ";
}
vector<int>V;
for(auto i:V)
{
cout<<i<<" ";
}
set<int>st;
for(auto i:st)
{
cout<<i<<" ";
}
map<int,int>mp;
for(auto i:mp)
{
cout<<i.first<<" "<<i.second<<endl;
}
// Note: You can also change value of data structures using for auto loop
// For That You need to put a ampersand when declaring a auto keyword like this-
for(auto &i:name_of_the_data_structure)
{}
// Note: Always make a habit to declare auto keyword in for loop with an ampersand
// End
// Get Rid of all the includes by using only one include
#include <bits/stdc++.h>
// End
// I see a lot of programmer code like this
pair<int, int> p;
vector<int> v;
// ...
p = make_pair(3, 4);
v.push_back(4); v.push_back(5);
//while you can just do that
pair<int, int> p;
vector<int> v;
// ...
p = {3, 4};
v = {4, 5};
//Use emplace_back instead of push_back
// Most of Programmer code-
vector<pair<int,int>>vec;
vec.push_back(make_pair(2,3));
//while you can just do that
vector<pair<int,int>>vec;
vec.emplace_back(2,3);
// End