Open In App

C++ Programming and STL Facts

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




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


 

 

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!

 

The header file is <bits/stdc++.h>.

 

#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




<div id="highlighter_781438" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">// CPP Program to demonstrate the GCD function</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"><code class="preprocessor">#include <bits/stdc++.h></code></div><div class="line number4 index3 alt1"><code class="keyword bold">using</code> <code class="keyword bold">namespace</code> <code class="plain">std;</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="color1 bold">int</code> <code class="plain">main()</code></div><div class="line number7 index6 alt2"><code class="plain">{</code></div><div class="line number8 index7 alt1"><code class="undefined spaces">    </code><code class="color1 bold">int</code> <code class="plain">r = __gcd(10, 15);</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">    </code><code class="plain">cout << r;</code></div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"><code class="undefined spaces">    </code><code class="keyword bold">return</code> <code class="plain">0;</code></div><div class="line number12 index11 alt1"><code class="plain">}</code></div></div></td></tr></tbody></table></div>


 
 

Output

5

Note: This works only in GCC.

 

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

 

CPP




<div id="highlighter_955360" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">// CPP Program to demonstrate the functionality of</code></div><div class="line number2 index1 alt1"><code class="comments">// to_string() function</code></div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="preprocessor">#include <bits/stdc++.h></code></div><div class="line number5 index4 alt2"><code class="keyword bold">using</code> <code class="keyword bold">namespace</code> <code class="plain">std;</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="comments">// Driver Code</code></div><div class="line number8 index7 alt1"><code class="color1 bold">int</code> <code class="plain">main()</code></div><div class="line number9 index8 alt2"><code class="plain">{</code></div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="color1 bold">int</code> <code class="plain">a = 97;</code></div><div class="line number11 index10 alt2"><code class="undefined spaces">    </code><code class="plain">string t = to_string(a);</code></div><div class="line number12 index11 alt1"><code class="undefined spaces">    </code><code class="plain">cout << t;</code></div><div class="line number13 index12 alt2"><code class="plain">}</code></div></div></td></tr></tbody></table></div>


 
 

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 <bits/stdc++.h>
using namespace std;
 
int main()
{
    string a = "2665";
    int t = stoi(a);
    cout<<t;
}


 
 

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 set the value of all elements to zero of
// an array in a function
#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] << " ";
 
    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 <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] << " ";
 
    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 that memset initialises all
// elements of an integer array to 0 or -1
 
#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;
 
    // will not work
    memset(a, 5, sizeof(a));
    for (int i = 0; i < 5; i++)
        cout << a[i] << " ";
}


 
 

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[4]. 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





 
 

Output

0 0 0 0 0 
-1 -1 -1 -1 -1 
84215045 84215045 84215045 84215045 84215045 

 

 



Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads