-
Assign value by a pair of {} to a container
// Lots of programmers write code
// like this one:
pair<
int
,
int
> p = make_pair(3, 4);
// while you can just do this :
pair<
int
,
int
> p = { 3, 4 };
// even a more complex pair
pair<
int
, <
char
,
int
> > p = { 3, {
'a'
, 10 } };
-
As we are familiar with the pair there is also one thing known as tuples.
// A tuple can hold elements of different types tuple t = {3, 4, 5, 'a'};
-
Get rid of those includes!
// Simply use below, we do not need to
// include any other library such as
// iostream, vector, string, cmath and so on.
#include <bits/stdc++.h>
- Hidden function (not really hidden but not used often)
__gcd(value1, value2)
You don’t need to code Euclidean Algorithm for a gcd function, from now on we can use. This function returns gcd of two numbers.
e.g. __gcd(18, 45) = 9.
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
int
r = __gcd(10, 15);
cout << r;
}
Output:5
Note that this works only in GCC.
- Convert integer to string directly in c++ using direct command
to_string();#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
int
a = 2665;
string t = to_string(a);
}
Output:- Convert from string to integer
stoi(t);#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
string a =
"2665"
;
int
t = stoi(a);
}
Output:- A set always keeps elements in ascending order:
set s;
// For descending order
set <
int
, greater> s;
- Every variable declared outside of functions are static and have the default value of 0
#include <bits/stdc++.h>
using
namespace
std;
int
a[5];
int
main()
{
// Values in a[] are 0
for
(
int
i = 0; i < 5; i++)
cout << a[i] <<
" "
;
// Values in b[] are garbage
cout << endl;
int
b[5];
for
(
int
i = 0; i < 5; i++)
cout << b[i] <<
" "
;
}
Output:0 0 0 0 0 4196880 0 4196368 0 -731594144
- If you declare an array inside a function the value of its elements are garbage, but if you write TYPE a[n] = { }; all elements will be zero by default.
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
// Values in a[] are 0
int
a[5] = {};
for
(
int
i = 0; i < 5; i++)
cout << a[i] <<
" "
;
// Values in b[] are garbage
cout << endl;
int
b[5];
for
(
int
i = 0; i < 5; i++)
cout << b[i] <<
" "
;
}
Output:0 0 0 0 0 4196896 0 4196368 0 354948080
- Number of set bits in binary representation of a number x is
__builtin_popcountll(x)#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
int
x = 5;
cout << __builtin_popcountll(x);
}
Output:2
Note : This works only in GCC.
- memset sets the byte values (or unsigned char) of a block of memory. We can use memset to initialize all elements of an integer array to 0 or -1 but not the other values
#include <bits/stdc++.h>
using
namespace
std;
int
main()
{
int
a[5];
// all elements of A are zero
memset
(a, 0,
sizeof
(a));
for
(
int
i = 0; i < 5; i++)
cout << a[i] <<
" "
;
cout << endl;
// all elements of A are -1
memset
(a, -1,
sizeof
(a));
for
(
int
i = 0; i < 5; i++)
cout << a[i] <<
" "
;
cout << endl;
// Would not work
memset
(a, 5,
sizeof
(a));
// WRONG
for
(
int
i = 0; i < 5; i++)
cout << a[i] <<
" "
;
}
Output:0 0 0 0 0 -1 -1 -1 -1 -1 84215045 84215045 84215045 84215045 84215045
This article is attributed to GeeksforGeeks.org
0 0You Might Also Like
Subscribe to Our Newsletter
- Hidden function (not really hidden but not used often)
leave a comment
0 Comments