tie(w,x,y,z) = make_tuple(10,20,30,40)
:- w=10,x=20,y=30,z=40unordered_set
,unordered_map
are faster thanset
andmaps
.set
uses self balancing binery search trees therefore O(nlogn).unordered_set
uses hashing therefore O(1) same for themap
andunordered_map
.auto
keyword can be used for declaring variables, it automatically set the type of that variable.lower_bound
: find >= elem ,upper_bound
: find > element- when we want to find element equal to we use lower_bound else upper_bound
is_sorted
: Returnstrue
if the container is sorted elsefalse
.- In Global we can declare array of
bool
upto10^8
orint
array upto10^7
. - In Local we can declare array of
bool
upto10^7
orint
array upto10^6
.
a>>1
== divide a by 2.a<<1
== multiply a with 2.- if
(a&1)==0
== number is even. - if
(a&1)==1
== number is odd, make sure you use the brackets. - swap numbers ==
a=a^b;b=a^b;a=a^b;
- We should use
emplace_back
to pushpair
invector
insteed ofpb(mp())
orpb({})
.
__builtin_popcount(n)
== returns no. of set bits in n.- find i'th bit -->
(n & (1<<i))==0
then ith bit is 0 else 1. - set i'th bit -->
( n | (1<<i))
then ith bit set to 1. - clear i'th bit -->
(n & ~(1<<i))
then ith bit set to 0 - mask =
&(1<<i)
for find. - mask =
|(1<<i)
for set-1. - mask =
& ~(1<<i)
for set-0. (n&(n-1))
used in many ways :- It reduces last set bit to 1.
ex.1101->1100->1000->0000
then we can count no of set bits by count. - we can find if a number is power of 2 or not -->if
(n&(n-1))==0
then it is power of two else not.
- It reduces last set bit to 1.
temp.find(B) != string::npos
, find B is in string or not.s.insert(pos, string)
, insert string at pos position.
v.erase(iterator)
to delete elem while traversing in vector. Remember to do it--.auto it = lower_bound(v.begin(), v.end(),elem);
int index = it - v.begin();
ORindex = it - v;
auto it = s.lower_bound(elem);
int elem = *it;
s.count()
returns 1 if element is present in set else 0.- NOTE: for vector and other containers which contains duplicate elements count returns count of the number.
mp.count()
returns 1 if element is present in map else 0.- NOTE: for vector and other containers which contains duplicate elements it returns count of the number.
-
We can use dec,hex,oct to print numbers in different bases.
// bitset<bitcnt>(num) cout << bitset<12>(n) << "\n"; // Binary cout << oct << n << "\n"; // Octal cout << hex << n << "\n"; // Hexadecimal cout << dec << n << "\n"; // Decimal-normal
-
std::minmax_element
== returnspair{minelement, maxelement}
vector<int> x = {1, 2, 3, 4}; auto it = minmax_element(all(x)); cout << *it.first << ' ' << *it.second << endl;
output: 1 4
-
is_palindrome
function usingstd::equal
.bool is_palindrome(string s) { return equal(all(s) - s.size() / 2, s.rbegin()); }
©️ prepared by chetan6780