# Some Cool C++ Tricks

In this post we discuss some cool tricks available in C++. Many of these tricks belong to C++11. so use C++11 in order to test anything here.

**1. Assign value by a pair of \(\{\}\) to a container**

I see lots of programmers write code like this one:

pair<int, int> p; p = make_pair(3, 4);

while you can just do this:

pair<int, int> p; p = {3, 4};

even a more complex pair such as this one

pair<int, pair<char, long long> > p; p = {3, {'a', 8ll}};

What about **vector, deque, set** and other containers?

vector<int> v; v = {1, 2, 5, 2}; for (auto i: v) cout << i << ' '; cout << '\n'; // prints "1 2 5 2"

deque<vector<pair<int, int>>> d; d = {{{3, 4}, {5, 6}}, {{1, 2}, {3, 4}}}; for (auto i: d){ for (auto j: i) cout << j.first << ' ' << j.second << '\n'; cout << "-\n";} // prints 3 4 5 6 - 1 2 3 4 -

set<int> s; s = {4, 6, 2, 7, 4}; for (auto i: s) cout << i << ' '; cout << '\n'; //prints 2 4 6 7

list<int> l; l = {5, 6, 9, 1}; for (auto i: l) cout << i << ' '; cout << '\n'; // prints 5 6 9 1

array<int, 4> a; a = {5, 8, 9, 2}; for (auto i: a) cout << i << ' '; cout << '\n'; // prints 5 8 9 2

tuple<int, int, char> t; t = {3, 4, 'f'}; cout << get<2>(t) << '\n';

**This technique doesn’t work for stack and queue.**

**2. Get rid of so many includes**

Simply use
#include <bits/stdc++.h>. This library includes many of libraries we do need in contest like **algorithm**, **iostream**, **vector** and many more. Believe me you don’t need to include anything else!

**3. 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 which returns gcd of two numbers.**e.g.**__gcd(18, 27) which returns 7.**__builtin_ffs(x):**This function returns 1 + index of least significant 1-bit of x. If x == 0, returns 0. Here x is an int, this function with suffix ‘l’ gets a long argument and with suffix ‘ll’ gets a long long argument.**e.g.**__builtin_ffs(10) = 2 because 10 is ‘…10 1 0’ in base 2 and first 1-bit from right is at index 1 (0-based) and function returns 1 + index.**__builtin_clz(x):**This function returns number of leading 0-bits of x which starts from most significant bit position. x is unsigned int and like previous function this function with suffix ‘l gets a unsigned long argument and with suffix ‘ll’ gets a unsigned long long argument. If x == 0, returns an undefined value.**e.g.**__builtin_clz(16) = 27 because 16 is ‘ … 10000’. Number of bits in a unsigned int is 32. so function returns 32 — 5 = 27.**__builtin_ctz(x):**This function returns number of trailing 0-bits of x which starts from least significant bit position. x is unsigned int and like previous function this function with suffix ‘l’ gets a unsigned long argument and with suffix ‘ll’ gets a unsigned long long argument. If x == 0, returns an undefined value.**e.g.**__builtin_ctz(16) = 4 because 16 is ‘…1 0000 ‘. Number of trailing 0-bits is 4.**__builtin_popcount(x):**This function returns number of 1-bits of x. x is unsigned int and like previous function this function with suffix ‘l’ gets a unsigned long argument and with suffix ‘ll’ gets a unsigned long long argument. If x == 0, returns an undefined value.**e.g. __builtin_popcount(14) = 3**because 14 is ‘… 111 0’ and has three 1-bits.

**4. Usages of Move**

When you work with STL containers like vector, you can use move function to just move container, not to copy it all.

vector<int> v = {1, 2, 3, 4}; vector<int> w = move(v); cout << "v: "; for (auto i: v) cout << i << ' '; cout << "\nw: "; for (auto i: w) cout << i << ' '; // Output: v: w: 1 2 3 4

As you can see v moved to w and not copied.

**4. C++0x Raw Strings**

C++ supports UTF-8 strings, Raw strings and more. Here I want to show raw strings. We define a raw string as below:

string str = "Hello\tWorld\n"; string r_str = R"(Hello\tWorld\n)"; cout << str << r_str; //Output: Hello World Hello\tWorld\n

In* raw string* literal is a string in which the escape characters (like \n \t or \” ) of *C++* are not processed.

This article is contributed by Praveen. If you like eLgo Academy and would like to contribute, you can mail your article to admin@elgoacademy.org. See your article appearing on the eLgo Academy page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.