Tuesday 31 December 2019

std::next_permutation(arr.begin(),arr.end());

What is next_permutation(a.begin(),a.end()) ?

Next permutation is like what its name says, finds the next permutation of a and assigns this permutation to a.

next_permutation is often used in brute force solutions.

So here's how to use it :

do{
   // do something
} while(next_permutation(a.begin(),a.end()));

The complexity of this code is O(number of elements in a !).

(Note : ! = factorial)

Extra : prev_permutation

prev_permutation(a.begin(),a.end()) has very similar functionalities. In fact, the only difference is that prev_permutation takes the previous permutation of a and assignes this permutation to a.

Thanks for reading and Happy New Year !

Friday 27 December 2019

C++ Tutorial - Variables

So what is a variable ?

In computer science (not only in C++) a variable is a storage location. In C++, every variable has a data type, a name and a value.


Exemple :

int val = 100;
double a;

Here we have :

- data type :      int ; double
- name :            val ; a
- value :           100 ; 0

By default values are set to 0.

What can the name of the variable be ?

First of all, the name need to respect some conventions or else your program will recieve a Compilation Error verdict.

Here are the conventions :

- Your variables must begin with a lowercase or uppercase letter.
- Each character except the first character must be either a number, a character (lowercase or uppercase) or a underscore.

The name of your variables should be meaningful because you need to know what is assigned to the variable.

It is recommended to put a uppercase letter in front of each word except the first one, for exemple :

int maxWeight = 42;

Your variables shouldn't be to long and shouldn't be too short. (about 3 - 11 characters)

Why do we need variables ?

Variables are extremely important because you can store a value and reuse this value afterwards. Without variables, you can basically do almost nothing.

What are the possible data types ?

The possible data types for decimal numbers are : bool, char, unsigned char, short, int, unsigned int, long long, unsigned long long,  __int128_t, __m128d.

Data types for floating numbers are : float, double, long double and __float128.

Please note that unsigned integers do not support negative numbers (you'll get a random number if you use unsigned integers with negative numbers) ! So, be careful when you use them !

Here you can find the limits of these data types for decimal numbers except the last two :

nameexpressesvalue*
CHAR_BITNumber of bits in a char object (byte)8 or greater*
SCHAR_MINMinimum value for an object of type signed char-127 (-27+1) or less*
SCHAR_MAXMaximum value for an object of type signed char127 (27-1) or greater*
UCHAR_MAXMaximum value for an object of type unsigned char255 (28-1) or greater*
CHAR_MINMinimum value for an object of type chareither SCHAR_MIN or 0
CHAR_MAXMaximum value for an object of type chareither SCHAR_MAX or UCHAR_MAX
MB_LEN_MAXMaximum number of bytes in a multibyte character, for any locale1 or greater*
SHRT_MINMinimum value for an object of type short int-32767 (-215+1) or less*
SHRT_MAXMaximum value for an object of type short int32767 (215-1) or greater*
USHRT_MAXMaximum value for an object of type unsigned short int65535 (216-1) or greater*
INT_MINMinimum value for an object of type int-32767 (-215+1) or less*
INT_MAXMaximum value for an object of type int32767 (215-1) or greater*
UINT_MAXMaximum value for an object of type unsigned int65535 (216-1) or greater*
LONG_MINMinimum value for an object of type long int-2147483647 (-231+1) or less*
LONG_MAXMaximum value for an object of type long int2147483647 (231-1) or greater*
ULONG_MAXMaximum value for an object of type unsigned long int4294967295 (232-1) or greater*
LLONG_MINMinimum value for an object of type long long int-9223372036854775807 (-263+1) or less*
LLONG_MAXMaximum value for an object of type long long int9223372036854775807 (263-1) or greater*
ULLONG_MAXMaximum value for an object of type unsigned long long int18446744073709551615 (264-1) or greater*
Source : cplusplus.com

These values can vary depending on your compiler and its version.

The main differences between these data types are precision and overflow limits.

Note that __int128_t, __float128 and __m128d are somehow a little special because they are GNU extensions. If you want to know more about their up and downs please consider looking at this article.

What is a global variable and a local variable ?

A global variable is a variable that is declared outside the main() function and a local variable is declared inside the main() function.


Thanks for reading !



std::next_permutation(arr.begin(),arr.end());

What is next_permutation(a.begin(),a.end()) ? Next permutation is like what its name says, finds the next permutation of a and assigns ...