 Open in App
Not now

# C++ Programming and STL Facts

• Difficulty Level : Easy

C++ is widely used for competitive programming. It is preferred because of its reliability, efficient execution, short snippets, etc. It has become adaptive by most coders as it also provides the benefits of Standard Template Library(STL)

C++ STL is the backbone of programming. The inbuilt functions reduce a code snippet to even a single line sometimes. So, the following are some interesting facts about Standard Template Library(STL) in C++:

1) Values can be assigned by a pair of {} to a container.

## CPP

 `// Various ways to assign value by a pair`   `// Method 1` `pair<``int``, ``int``> p = make_pair(3, 4);`   `// Method 2` `pair<``int``, ``int``> p = { 3, 4 };`   `// Method 3` `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) We can avoid writing all the header files such as iostream, vector, string, math, and so on. Including just one header file will do the work!

```#include<bits/stdc++.h>
using namespace std;```

4) You don’t need to code Euclidean Algorithm for a GCD function, instead, we can use __gcd(value1, value2). This function returns the Greatest common divisor of two numbers.

`Example: __gcd(18, 45) = 9`

## CPP

 `// CPP Program to demonstrate the GCD function`   `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `r = __gcd(10, 15);` `    ``cout << r;`   `    ``return` `0;` `}`

Output

`5`

Note: This works only in GCC.

5) In C++, you can directly convert integer to string using the ‘to_string();‘ command.

## CPP

 `// CPP Program to demonstrate the functionality of` `// to_string() function`   `#include ` `using` `namespace` `std;`   `// Driver Code` `int` `main()` `{` `    ``int` `a = 97;` `    ``string t = to_string(a);` `    ``cout << t;` `}`

Output

`97`

6) In C++, you can directly convert string to an integer using the ‘stoi();‘ command.

## CPP

 `// CPP Program to demonstrate the functionality of` `// stoi() function`   `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``string a = ``"2665"``;` `    ``int` `t = stoi(a);` `    ``cout<

Output

`2665`

7) A set stores the elements in ascending order by default.

`set<datatype> setname;`

Note: set<datatype, greater<datatype>> setname; is used for storing values in a set in descending order.

8) Every variable declared outside of functions is static and has the default value of 0.

## CPP

 `// CPP Program to demonstrate variables 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] << ``" "``;`   `    ``return` `0;` `}`

Output

```0 0 0 0 0
4196880 0 4196368 0 846571392 ```

9) If you declare an array inside a function, the value of its elements are garbage, but to set the value of all elements to zero, we can use,

` TYPE a[n] = { };`

## CPP

 `// CPP Program to set the value of all elements to zero of` `// an array in a function` `#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] << ``" "``;`   `    ``return` `0;` `}`

Output

```0 0 0 0 0
4196896 0 4196368 0 -345132736 ```

10) Number of set bits in the binary representation of a number x can be found by __builtin_popcountll(x). Basically, this function counts the number of one’s(set bits) in an integer.

## CPP

 `// CPP Program to demonstrate the functionality of` `// __builtin_popcountll(x)`   `#include ` `using` `namespace` `std;`   `int` `main()` `{` `    ``int` `x = 5; ``// 0101` `    ``cout << __builtin_popcountll(x);`   `    ``return` `0;` `}`

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.

Initializing all element of an array other than 0 or -1, will not set all the element as desired, because memset sets the bytes values, For example, Calling memset(arr, 1, 4) to set all elements  of an array int arr. it become 0x01 0x01 0x01 0x01. (i.e.16,843,009  depending on your CPU architecture ) But your expected value is 0x00 0x00 0x00 0x01.

## CPP

 `// CPP Program to demonstrate that memset initialises all` `// elements of an integer array to 0 or -1`   `#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;`   `    ``// will not work` `    ``memset``(a, 5, ``sizeof``(a));` `    ``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 ```