# C++ programming and STL facts

1. 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 } }; `

2. 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'};```
3. 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 `

/div>
4. 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 ` `using` `namespace` `std; ` `int` `main() ` `{ ` `    ``int` `r = __gcd(10, 15); ` `    ``cout << r; ` `} `

Output:

```5
```

Note that this works only in GCC.

5. Convert integer to string directly in c++ using direct command
to_string();

 `#include ` `using` `namespace` `std; ` `int` `main() ` `{ ` `    ``int` `a = 2665; ` `    ``string t = to_string(a); ` `} `

Output:

6. Convert from string to integer
stoi(t);

 `#include ` `using` `namespace` `std; ` `int` `main() ` `{ ` `    ``string a = ``"2665"``; ` `    ``int` `t = stoi(a); ` `} `

Output:

7. A set always keeps elements in ascending order:

 `set s; ` ` `  `// For descending order ` `set <``int``, greater> s; `

8. Every variable declared outside of functions are static and have the default value of 0

 `#include ` `using` `namespace` `std; ` ` `  `int` `a; ` `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; ` `    ``for` `(``int` `i = 0; i < 5; i++) ` `        ``cout << b[i] << ``" "``; ` `} `

Output:

```0 0 0 0 0
4196880 0 4196368 0 -731594144
```
9. 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 ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``// Values in a[] are 0 ` `    ``int` `a = {}; ` `    ``for` `(``int` `i = 0; i < 5; i++) ` `        ``cout << a[i] << ``" "``; ` ` `  `    ``// Values in b[] are garbage ` `    ``cout << endl; ` `    ``int` `b; ` `    ``for` `(``int` `i = 0; i < 5; i++) ` `        ``cout << b[i] << ``" "``; ` `} `

Output:

```0 0 0 0 0
4196896 0 4196368 0 354948080
```
10. Number of set bits in binary representation of a number x is
__builtin_popcountll(x)

 `#include ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `x = 5; ` `    ``cout << __builtin_popcountll(x); ` `} `

Output:

```2
```

Note : This works only in GCC.

11. 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 ` `using` `namespace` `std; ` ` `  `int` `main() ` `{ ` `    ``int` `a; ` ` `  `    ``// 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
```

## tags:

C++ cpp-pair cpp-set cpp-string STL STL CPP