C++ Programming and STL Facts - GeeksforGeeks

C++ Programming and STL Facts
sqrt, sqrtl and sqrtf in C++
Power Function in C/C++
tolower() function in C
Measure execution time with high precision in C/C++
Measure execution time of a function in C++
How to measure time taken by a function in C?
time() function in C
Program to reverse a string (Iterative and Recursive)
Print reverse of a string using recursion
Write a program to print all permutations of a given string
Print all distinct permutations of a given string with duplicates
Permutations of a given string using STL
All permutations of an array using STL in C++
std::next_permutation and prev_permutation in C++
Lexicographically next permutation in C++
How to print size of array parameter in C++?
How to split a string in C/C++, Python and Java?
boost::split in C++ library
Tokenizing a string in C++
getline() Function and Character Array in C++
getline (string) in C++
How to use getline() in C++ when there are blank lines in input?
scanf() and fscanf() in C
getchar_unlocked() – Faster Input in C/C++ For Competitive Programming
Vector in C++ STL
Arrays in C/C++
std::sort() in C++ STL
The C++ Standard Template Library (STL)
Initialize a vector in C++ (6 different ways)
C++ Programming and STL Facts
sqrt, sqrtl and sqrtf in C++
Power Function in C/C++
tolower() function in C
Measure execution time with high precision in C/C++
Measure execution time of a function in C++
How to measure time taken by a function in C?
time() function in C
Program to reverse a string (Iterative and Recursive)
Print reverse of a string using recursion
Write a program to print all permutations of a given string
Print all distinct permutations of a given string with duplicates
Permutations of a given string using STL
All permutations of an array using STL in C++
std::next_permutation and prev_permutation in C++
Lexicographically next permutation in C++
How to print size of array parameter in C++?
How to split a string in C/C++, Python and Java?
boost::split in C++ library
Tokenizing a string in C++
getline() Function and Character Array in C++
getline (string) in C++
How to use getline() in C++ when there are blank lines in input?
scanf() and fscanf() in C
getchar_unlocked() – Faster Input in C/C++ For Competitive Programming
Vector in C++ STL
Arrays in C/C++
std::sort() in C++ STL
The C++ Standard Template Library (STL)
Initialize a vector in C++ (6 different ways)
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 1pair<int, int> p = make_pair(3, 4); // Method 2pair<int, int> p = { 3, 4 }; // Method 3pair<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!
 
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
// CPP Program to demonstrate the GCD function #include <bits/stdc++.h>using namespace std; int main(){    int r = __gcd(10, 15);    cout << r;     return 0;}
  
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 <bits/stdc++.h>using namespace std; // Driver Codeint main(){    int a = 97;    string t = to_string(a);    cout << t;}
  
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;}
  
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 <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] << " ";     return 0;}
  
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;}
  
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 <bits/stdc++.h>using namespace std; int main(){    int x = 5; // 0101    cout << __builtin_popcountll(x);     return 0;}
  
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
// 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] << " ";}
  
0 0 0 0 0 
-1 -1 -1 -1 -1 
84215045 84215045 84215045 84215045 84215045 
 
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 
anshikajain26
whitenoise
cpp-pair
cpp-set
cpp-string
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Inheritance in C++
Map in C++ Standard Template Library (STL)
Multidimensional Arrays in C / C++
Socket Programming in C/C++
C++ Classes and Objects
Operator Overloading in C++
Bitwise Operators in C/C++
Virtual Function in C++
Iterators in C++ STL
Constructors in C++