C++ programming and STL facts
-
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 pairpair<int, <char,int> > p = { 3, {'a', 10 } };chevron_rightfilter_none -
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'}; -
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 <bits/stdc++.h>chevron_rightfilter_none -
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 <bits/stdc++.h>usingnamespacestd;intmain(){intr = __gcd(10, 15);cout << r;}chevron_rightfilter_noneOutput:5
Note that this works only in GCC.
-
Convert integer to string directly in c++ using direct command
to_string();#include <bits/stdc++.h>usingnamespacestd;intmain(){inta = 2665;string t = to_string(a);}chevron_rightfilter_noneOutput: -
Convert from string to integer
stoi(t);#include <bits/stdc++.h>usingnamespacestd;intmain(){string a ="2665";intt = stoi(a);}chevron_rightfilter_noneOutput: -
A set always keeps elements in ascending order:
set s;// For descending orderset <int, greater> s;chevron_rightfilter_none -
Every variable declared outside of functions are static and have the default value of 0
#include <bits/stdc++.h>usingnamespacestd;inta[5];intmain(){// Values in a[] are 0for(inti = 0; i < 5; i++)cout << a[i] <<" ";// Values in b[] are garbagecout << endl;intb[5];for(inti = 0; i < 5; i++)cout << b[i] <<" ";}chevron_rightfilter_noneOutput:
0 0 0 0 0 4196880 0 4196368 0 -731594144
-
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 <bits/stdc++.h>usingnamespacestd;intmain(){// Values in a[] are 0inta[5] = {};for(inti = 0; i < 5; i++)cout << a[i] <<" ";// Values in b[] are garbagecout << endl;intb[5];for(inti = 0; i < 5; i++)cout << b[i] <<" ";}chevron_rightfilter_noneOutput:0 0 0 0 0 4196896 0 4196368 0 354948080
-
Number of set bits in binary representation of a number x is
__builtin_popcountll(x)#include <bits/stdc++.h>usingnamespacestd;intmain(){intx = 5;cout << __builtin_popcountll(x);}chevron_rightfilter_noneOutput:2
Note : This works only in GCC.
-
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 <bits/stdc++.h>usingnamespacestd;intmain(){inta[5];// all elements of A are zeromemset(a, 0,sizeof(a));for(inti = 0; i < 5; i++)cout << a[i] <<" ";cout << endl;// all elements of A are -1memset(a, -1,sizeof(a));for(inti = 0; i < 5; i++)cout << a[i] <<" ";cout << endl;// Would not workmemset(a, 5,sizeof(a));// WRONGfor(inti = 0; i < 5; i++)cout << a[i] <<" ";}chevron_rightfilter_noneOutput:0 0 0 0 0 -1 -1 -1 -1 -1 84215045 84215045 84215045 84215045 84215045
Recommended Posts:
- Interesting Facts about C++
- C++ bitset interesting facts
- Some Interesting facts about default arguments in C++
- Interesting facts about switch statement in C
- Interesting facts about data-types and modifiers in C/C++
- Some interesting facts about static member functions in C++
- Facts and Question related to Style of writing programs in C/C++
- Web Programming in C++
- Socket Programming in C/C++
- C++ Programming Basics
- Introduction to C++ Programming Language
- Object Oriented Programming in C++
- C++ tricks for competitive programming (for C++ 11)
- Fast I/O for Competitive Programming
- Some useful C++ tricks for beginners in Competitive Programming
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

