C++ Programming and STL Facts
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
#include <bits/stdc++.h>
using namespace std;
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" >
|
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" >
|
6) In C++, you can directly convert string to an integer using the ‘stoi();‘ command.
CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a = "2665" ;
int t = stoi(a);
cout<<t;
}
|
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[5] = {};
for ( int i = 0; i < 5; i++)
cout << a[i] << " " ;
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[5] = {};
for ( int i = 0; i < 5; i++)
cout << a[i] << " " ;
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a[5];
memset (a, 0, sizeof (a));
for ( int i = 0; i < 5; i++)
cout << a[i] << " " ;
cout << endl;
memset (a, -1, sizeof (a));
for ( int i = 0; i < 5; i++)
cout << a[i] << " " ;
cout << endl;
memset (a, 5, sizeof (a));
for ( int i = 0; i < 5; i++)
cout << a[i] << " " ;
}
|
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.
Output
0 0 0 0 0
-1 -1 -1 -1 -1
84215045 84215045 84215045 84215045 84215045
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...