e of any integral type the return type is determined using the type of minimum of sets so let's just go ahead so we'll move on to my compiler we will remove this stuff from here and as in the previous cases we are also going to be needing this library so the first thing we need to do above this main function is that we have to create a template using the template keyword you've already covered template in the previous section so you have an idea of how templates actually work so we are going to define into class a and then a class b and right down here what we are going to do using this template is that we are going to create a function that is the minimum function in which we are going to as i told you that it is going to return the minimum value for class a it is going to be a a which means a variable a of data type a then for the class b what we are going to do is that we are going to initialize a variable b now this was basically the job of auto to declare those two variables so now what declaration type over here is going to do is we are going to write in declaration type and this basically is going to check for the condition that is a is less than v then if it is then what it's going to do is that it's going to check it using the afl's conditions and then in the body of the function what we are going to do is that we are going to return a is less than b and we are also going to specify the afl's right in here so to talk about the driver code that is the main function code what we are going to do in here is that we are going to see out whatever result is returned from the above function and first what we are going to do is that we are going to pass into values to that function that is the minimum function so we are going to let's say call in the function and pass in let's say the first value is an integer value and let's say the second value is a float value now you're going to add nline space now what it's going to do basically is since we have used template so you know from your previous knowledge that this a can be of any data type this b can be of any data type so this 3v pass in was an integer and this 4.5 pass in was a float it doesn't care whatever the value was sent to it because it is to receive any kind of data value since it is a template now if we just write and return 0 over here and run this code it is this function over here is going to return the minimum value in this case so let's just run it and you can see that it printed out 3 which is our minimum value in this case so this is how basically you can use the auto keyword and the declaration type in a single function that is also called automatic type declaration so that's it with this tutorial in the next tutorial we will cover more features that were added in c++11 so up till now thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is umar khan and i welcome you to a new section here on this course on advanced c++ in which we are going to be seeing the new features that are added in c++11 so the content covered in these tutorials are basically the additions that are made to c++11 these additions are lambda expressions automatic type declaration and declaration type uniform initializations deleted and defaulted functions null pointer delegating constructors r-value references and new smart pointer classes so we are going to be seeing all of these and we'll start on with the introduction tutorial in the next tutorial so that's it with this tutorial thank you so much guys for watching and i will see you guys in the next tutorial hey guys what's up this is umar khan and i welcome you to the very first tutorial on user defined literals in this tutorial we will cover the introduction to user defined literals we will see the syntax for how it is written and then finally when we will see some examples for how it is done in c++ so what is a user defined literal and what is its use in programming language so moving on to the definition user defined literals are a unique feature in all mainstream programming languages they empower you to combine values with units let me just show you a simple example to help you understand this definition so i will move on to my editor right here and i don't want to output anything i just want to show you that what have i actually spoken in the definition here so for example you have something like a long double and then you have weight and you have initialized it to 2.3 let's say now what is it is it in pounds is it in kilograms or is it in grams this 2.3 so with the user defined literals we can attach units to the values which has some advantages number one the code becomes readable and number two that the conversion computations are done at the compile time so if weight equals to 2.3 kgs for example if we have like weight equals to 2.3 kg and for example we want to compute some kind of ratio that is 2.3 kg divided by 1.2 lb now to compute this ratio actually it is necessary to convert 2.3 and 1.2 into a same unit now this is where user defined literals come into help user defined literals help us to overcome unit translation cost we can define user defined literals for user defined types and new form of literals for built-in types they help to make constraints in code more readable the value of a UDL or user defined literal is substituted with the actual value defined in the code by the compiler at the compile time user defined literals don't save much of a coding time but more and more calculations can be shifted to compile time for a faster and a safer execution so i hope that you've now understood what is definition and what is the advantage of using the user defined literals so moving ahead so moving ahead to the syntax now you can see right here that you have the syntax right on your screens that is first a built-in literals then a plus plus means concatenation sign then we have this underscore right here and then we have the suffix right here now if i just show you that how this syntax is actually implemented you can see some of the examples right here the first one is some binary values then we have an underscore this is actually the built-in literal as i showed you right here this is the built-in literal which is this one zero one one and then we have this underscore these plus plus shows that this is concatenated actually these plus plus don't have to be added when you are writing and finally we have this b which means binary and this is actually the suffix that defined actually this for example in the previous case of weight you can write in as something like let me just show you you can write it as 2.3 underscore kg and this is this 2.3 actually is going to be your built-in literal and then this is going to be the underscore sign and this kg which is actually defining this weight that what what is it this weight in actually in kgs in grams or whatever so this is actually defining this weight so this is the suffix for that and you can see two more examples 6 to 3 and then for example we say that it stands as 10 for seconds or something like that and then we have 58 and then underscore and cent is the syntax sorry not the syntax it's a suffix actually so this is actually the syntax and these examples define how this syntax is going to be implemented in your code now what is the key benefit of a user defined literal the c++ compiler maps the user defined literates to the corresponding literal operator this literal operator has of course to be implemented by the programmer now to talk about the magic in this case let's have a look at the very first user defined literal that is right here that is 1010101 underscore b that for sure represent the binary value now the compiler maps the user defined literals to the literal operators underscore b which means a long long integer binary value now a few special rules are still missing there has to be a space between the quotation marks and the underscore with the suffix underscore b now you have the binary values in the variable bin now the compiler does not find the corresponding literal operator the compilation is going to fail now in c++ 14 an alternate syntax is used for user defined literals they differ from the c++ 11 syntax because it requires no space therefore it is possible to use reserved keyword like underscore capital C as suffix and use a user defined literal of the form 11 underscore c now the compiler will map 11 underscore c to the literal operator that is underscore c which is an unsigned long long integer now the simple rule is now that you can use suffixes starting with an appleto so finally now let's move on to def c++ and cover some examples so here is my def c++ so let me just zoom it so that it is much more visible yep seems to be good enough so now let's first solve an example that is exactly related to something like this weight on stuff so outside this main function actually paste it again as i was telling you there that this has to be defined as kg and when ratio of this and this has to be found then these two needs to be in the same unit now this is what user defined literal is going to do for me so this is actually going to be defined in the main function right up here but not like this actually we are going to do it in a pattern but first you need to actually define here that what is going to return what for example if you're talking about kilogram then what is going to return if you are talking about grams then what is going to return if you're talking about for example milligrams then what is going to return so first you have to define that values and these has to be made right above this main function so that they are global to the main function then it can be used anywhere in your code so for example the first one is long double operator and let's say we are talking about underscore kg and what it's going to return is sorry what's going to receive is a long double x and then what is going to do with that long double x is it is going to receive here it is going to actually return x steric thousand which means so whatever this value is going to be received is going to be in kgs so what it's going to do is that it's going to return and turn it into grams first so multiplying your weight in kgs by thousand it's going to return it or convert it into grams and this is what this long double operator function not lon it's going to be long so this this is what this long double operator underscore kg function is going to return it's going to return the value in grams actually but this long double x was actually in kg since as i told you that this actually defines what value is going to be received so whenever you enter a value in kg this function is going to be called this value is going to be received which means since this function is called so this value has to be in kgs and it's going to return the value after converting it into grams so moving on to now the second function that we are going to define for grams for example we say that the value that was enter was in grams so for that purpose you're also going to define a long double underscore g which means grams and it is also going to be received in long double x which means that if we specify this underscore g with some values then this function is going to be called and when this underscore g is called which means that this x is going to be equal to a value in some grams so what it's going to do is going to simply return the value of x which means it is going to do nothing with that value since what we want is that we want every value to be converted into grams before performing any computations with it so when it is in grams we don't need to do anything with it so for the final one for example we have a value long double operator and underscore mg which means that the value is now in mg grams and what is in mg grams whatever that is received here in this argument so what it's going to do is that it's going to convert it into grams and return it so how it is going to convert it you know the formula for that x divided by 1000 is going to convert that value and it is going to be returned so now after all this is done we have every kind of value converted into the unit we want we have converted every value into grams so now we can actually define our main function right down here so in the main function we can now perform any kind of computation for example we have a long double weight that is equal to 3.6 underscore kg which means that the weight is in kg second what is going to do is let me just see out this weight then what we are going to do is that we are actually going to set the precision let's just see out the precise result using the set precision function and the set precision is going to be 8 and we are going to write in weight plus 2.3 whatever the weight is it's going to add 2.3 milligrams into it and finish that function as well so next what we need to do is that we need to actually find some ratios now so for example we want first to divide something like 32.3 underscore kg and we want to divide it by 2.0 let's say gram underscore g and finally n l so now what it's going to do is that kg is going to be converted into grams this is going to be remaining the very same unit and this computation is going to be performed let's perform one more computation and let's just perform a multiplication of two values let's say the first one is 32.3 underscore milligram this time and we want to multiply it with 2.0 underscore gram and finally we are going to write in return zero so if i now just run this code you will understand what is going on in here all right the error says that set precision was not declared in scope yes we did not declare it and to declare it actually we have to include one more library that is named as io manip now this library you have to include when you want to actually call this function set precision so this set precision is actually going to limit this output right here to eight values and let me just run it and then i'll explain each and every output one by one now you can see right here my output on the screen we've got four outputs since we have four cout statements and let's just see all of them one by one now you can see that the first output is 3600 you can see right here that the weight entered was 3.6 underscore kg now when this weight was cout you know that when this underscore kg is used actually this function right above at the top is going to be called and you know that this x is now in kgs which means 3.6 kg is going to be received here what it's going to do it's going to multiply 3.6 with 1000 and return this value so when it multiplied 3.6 with 1000 and return that value that value was actually equal to 3600 that was our first output now to talk about the second output that says set precision 8 weight plus 2.3 mg now weight was actually equal to 3600 gram and this 2.3 was divided by 1000 since milligram when called return x by 1000 and x is now 2.3 it is going to be divided by 1000 is going to be added with 3600 and its precision is going to be set to 8 so if you see the output right here it is 3600.0023 so this was our second output and if you talk about this third output it was 32.3 kg which means that 32.3 was multiplied with 1000 it make it equal to i guess 3 2 3 triple 0 something like that and this 2.0 was as it is because you know that when gram is passed it is going to simply return this value as it is it was passed so this is going to be divided by 2 and the output is going to be 16150 and similarly when this last computation was performed this 32.3 was divided by 1000 as you can see at this function right here and it was multiplied by 2.0 as it is because it is in grams and we got this output that was 0.0646 so this is what is the purpose of user define literals in c++ this is how they help us to actually define our types of kg grams or something like that convert them into the values we want have one unit for every type so that operations like multiplications division and something like that can become possible so let me just give you one quick example more and then we can quit on with this tutorial so let's just remove this stuff from here and let's just remove this stuff at the top right here as well now you guys have just a basic knowledge of mathematics then you know that what is actually iota iota are actually the imaginary values now how do you have to use them in your c++ programming language to for example if you are given two values for example let's just initialize these values so then i can speak about them so for example the first complex value is a double value and it is stored in a variable name set and it is equal to 3.0 plus 4.0 underscore i which means that this is actually the iota part of that value and for example we have one more value that is also double and it is stored in a variable named y and that is equal to 2.3 plus 5.0 underscore iota so actually these are our two values in which if you talk about this z this 3 is actually the real part and this 4.0 is actually the imaginary part and if you talk about y this 2.3 is actually the real part and this 5.0 is our imaginary part now from your basic mathematics knowledge you know that the basic operations that are performed on any two such values that contain a real value and an imaginary value as well so these are addition or you can just multiply those two numbers so for example you want to multiply this z with y then how the compiler is going to interpret this 4.0 underscore iota and this 5.0 underscore iota and how it is going to add this with a real value now this is the place where user defined literals come into place and they are going to define what this iota is actually so what is this iota is actually first let us define that at the top here and then we can perform the computations so we are going to just define our imaginary literals right here literal values so let's just write in so it is going to be constant expressions the type is going to be complex double and then this following the syntax underscore i which means that this function is going to be called when ever it sees an underscore i and this is going to receive a long double value in this d part and then what is going to do with that value is it's going to return a complex double value using 0.0 comma static casting of that double value that is d so this is actually something like your lambda expressions and these lambda expressions are going to be coming in the future tutorial where i will explain what is a lambda function so for now you need to just understand that a static cast of double value d is going to be done and it is going to convert it into that static cast so let's just now perform our operations so the first operation we want to perform is let's say z plus y and then simply n del and the second operation we want to perform is let's say z static y and n l as well and finally we already have a return zero so let's just run this code yep it says complex just don't have a name we again forget to add the library for that let's just remove this library and add the complex library that deals with complex number and aota values so let's just run it now and you can see right here that it runs perfectly we have got our output the first output is actually you can see right here that we actually added two numbers that was z plus y so this is actually the output of z plus y and this is the output of z static y so in case of z plus y 3.0 was added with 2.3 you can see right here that is 5.3 and 4 and 5 imaginary values will ever added and it gives us 9 and this is actually the multiplicative inverse type multiplication so you can just sort it out using your multiplication and you can see that the answer is going to be the same because you are not much smarter than your compiler so i guess that's it so now you know the use of user defined literals what is their purpose how they are going to be implemented how these global functions are going to help us in defining the values for our literals that are user defined so i guess that's it so now these are some of the restrictions you have to follow and a user defined literals can work only with the one given in this slide but the return value can be of any time it can work with constant characters unsigned long double char constant char constant char 16 constant char 32 constant and uh these are going to be just standard libraries for that purpose this 16 this underscore t this 32 actually serving as the size so that's why it's from such standard library of size so but yes it can return any other value you have to remember that these are the values you deal work with but the return value can be of any type so i guess that's it with this tutorial in the next tutorial we will cover a second topic of some other advanced topic that is going to be not related to user defined literals so that's it with user defined literals so thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is Amir Khan and i welcome you to a new tutorial here on advanced c++ and as i told you that in this tutorial we are going to be starting on with a new topic that is placement new operator in c++ now placement new is a variation new operator in c++ normal new operator does two things it allocates memory and it constructs an object in the allocated memory now what placement new operator does that it allow us to separate the above two things that is allocating memory and constructing an object in the allocated memory in placement new we can pass a pre-allocated memory and constructs an object in the past memory so now what is the difference between the new and the placement new difference number one is that normal new allocates memory in the heap and construct objects there whereas using the placement new operator object construction can be done at a known address the second difference is that with normal new it is not known that at which address or memory location the new operator is pointing to whereas the address or memory location that placement new operator is pointing to is known because it is a memory of a known address the third difference come at the deletion the deallocation is done using delete operation one allocation is done by the new but there is no placement delete but if it is needed one can write it with the help of a destructor which we will be seeing at the end of this tutorial when we will be covering how to delete the memory or you may say the buffer so this is actually the syntax for doing or you may say using your placement new operator we have the new operator then address then the type and then the initializer as we can see that we can specify an address where we want a new object of given type to be constructed in the address part that is all done and we can just type in the address of our type now when to prefer using the placement new operator as it allow us to construct an object on memory that is already allocated it is required for optimizations as it is faster not to reallocate all the time there may be cases when it is required to reconstruct an object multiple times so placement new operator might be more efficient in those cases so let me just show you an example to illustrate the placement new operator so let's move on to our compiler and in here what we can do is that first we need to actually define a buffer because as I told you that the memory is going to be off of a choice we have to specify the address we want to where we want to store any kind of data so we have to initialize some buffer in which we want to then specify the address of that buffer in which the data is going to be stored so we are going to write an unsigned character buffer and we are going to specify the size of it as well so the size of it is going to be equal to int steric 2 which means that the size of this buffer is going to be equal to the size of int which is 4 and steric 2 means 4 into 2 8 which means that the size of the buffer is going to be equal to 8 blocks so 8 blocks in the memory are going to be just allocated right here when we write in this code so the next thing is that we need to use the placement new operator in the buffer and initialize some values so we are going to write in integer values and they are going to be equal to new then you know that we have to specify the address and the address in this case is going to be this buf that is the buffer which we created and the second thing is that we need to define in the value which we want to store so int3 is going to store in our value so now let's store one more value and let's name it as qint and that is going to equal to new buf is going to be where it is going to be stored and this size of int and int5 what it's going to do is it is going to actually add this five but first it's going to leave four spaces means for example if we have eight blocks let me just show you now for example we have actually a set of eight blocks that were initialized using this because the size of int is four one two three four and when we multiply it with two which means that one two three four blocks more has to be created so we have these eight blocks right here so when we write in in static p int equal to new buffer which means this is the buffer right here so what this buffer is going to do is in this buffer we write in new buffer int3 which means that three is going to be stored in this buffer but when we are storing the value of this five right here first what we are going to do is that we are going to specify the location and then we are going to write in where we want to actually store it so size of int means that it's going to leave four spaces one two three and these are the four spaces actually one two three four this space is going to be counted as well because we have not specified that we have filled this so we are going to leave in four spaces and then we are going to store five in right here so this is how three and five are going to be stored in the memory now the next thing is that we have to actually point in some pointers at the location we have actually stored so that later on we can just print out our this is not going to be it's going to be buffer so we can just print out our addresses in that case so it is going to be int steric and for the first one it is going to be buffer plus zero because the first is this three right here this this pbuf is going to point towards the location where this p int is stored and you know that it is stored at the very first location so this is going to be equal to buf plus zero and for the second one that is the buffer of q what we are going to do is that we are going to write an int steric and the buffer name that is buf and we are going to add in size of int which means that this second pointer is going to point for example let me just show you it here this p buffer is going to point right in here this pbuf is going to point right into this three because we have written buf plus zero which means the zero location and for the q buffer it has to leave the size of four and then point right here so q buf is going to point right here so now we can just see out our addresses so that you can just see what's going on in here so we are going to write in c out let's just see out the buffer address first buffer address for let's say first we want to the buffer address of the p buffer so it is going to be p buf and let's unline it and secondly we have to just print out the integer so we are going to write in that p integer is whatever was stored in this pint and secondly we have to specify the q buffer as well so we are going to write in c out we are going to just specify p with it as well p buffer address and then we are going to write in q buffer address and in this case the q buf is going to be printed out finally we can just print out the integer as well so q integer is q int let's just add a line right here to distinguish that what we are talking about so let's just put a dot dot dot dot dot here so that these two can be just separated from each other so now since you know that we have initialized this three and five for the pointer so what this pint is going to give us it's not going to give us the integer it's going to give us the p integer address so we are going to write in p integer address here and it is also going to be q integer addresses and finally let's just put a line again and now we can just print out both the integers so c out first integer and this is going to be printed out using the steric and if you have the concept of pointers then you know that what is going on in here because when we have to just write in the and sign so that we are pointing towards the address and when we are writing in this steric then what do we mean and when something is initialized with a pointer then removing this steric from this pint and see printing it out on the console screen what is going to print and just writing in the steric and then printing out this pbuf, qbuf or pint or qint and what is going to print so this is all from the knowledge of your pointers topics so you need to go through them first if you don't get the concept of pointers because if you don't get the concept of pointers you're not going to understand what's going on in here so we are going to print out the second integer as well so the second integer is steric, qbuf and as you know that this p buffer and q buffer are actually pointing towards this integer 3 and this integer 5 respectively so when we write in steric buf pbuf since this is pointing towards 3 it is going to print out 3 and since this qbuf is pointing towards 5 so this steric qbuf is going to print out 5 for us so let's just run this code and i hope it ran perfectly yes it does so you can see right here that it says that pbuf address is right here is p integer address is this which as you can see is the very same because the buffer address and the integer address are both pointing towards the same location then the q buffer address is this the q integer address is this because q integer address and p buffer address are going to be the same because q integer is stored in the q buffer and then the first integer is 3 and the second integer is 5 so i hope it is clear let me just explain it to you with the help of a diagram which i added so in this diagram actually the example i just covered is explained so you can see that this is actually the buffer vision initialized using the size of int which is 4 multiplied by 2 which means that you can see here that 4 plus 4 these 8 blocks have been initialized for this buffer so now the first integer was 3 the second integer was 5 now we initialized p buffer to the 0th location and then this p buffer sorry this is going to be the q buffer i just i guess misprinted it is going to be the q buffer the q buffer is going to be pointing at plus 4 location which means that the size of int location is going to be skipped which means four locations are going to be skipped and the q buffer is going to point right in here the first integer is going to be stored right in here the second integer is going to be stored right in here the p buffer along with a steric sign when c out is going to generate 3 the q buffer with a steric sign when c out is going to generate a 5 the first integer pointer is going to generate an address for this integer. The second pointer in which this integer is stored is going to generate this address for the second integer and then simply writing in p-buffer is going to generate this address and simply writing in q-buffer is going to generate this address. Moreover, the second integer address and this q-buffer address is going to be the very same since they are pointing at the same location whereas the q-buffer sorry this p-buffer address and the first integer address is also going to be the same right in here as for second integer at the cube buffer. So I guess that is clear. So let me just cover one more example. So we'll move on to defc++ and we will just remove the stop from here. So let's just remove it from here. And now in this tutorial what we are going to do is that we are going to initialize a value for a variable and then using the new operator what we are going to do is that we are going to change the value of x and then you are going to see the impact that what happens before using the placement of new operator and what happens after using the placement new operator. So just before what we are going to do is that we are going to write in a variable named as x and we are going to make it equal to 10. Now the next step is that we are going to actually see out before using placement new operator and L and then what we are going to do is that we are going to see out the variable. We are going to write in the variable is x at this time and we are going to see out the address of that as well. So we are going to write in the address is and operator and this x right here and an and L. So now we are going to use the placement new operator. So what it's going to do is that let's initialize a new variable. Let's name it since we are talking about integer because x is an integer. So we are going to initialize it to integer and then we are going to store it in a variable let's say y. So this is going to equal to new. The first thing we need to do is that we need to give this new variable allocation. So what in this tutorial I want I want that this value should be overridden because I'm going to pass in the address where this x is stored. So we know that this x is stored at this location and x. So what we are going to do is that we are going to pass in and x as the location and then we are going to write in the value which we want to store at that location. So for example 100 is the value we want to store at that location. So now we are going to see out after using placement new operator and L. Let's just place one more space here. So now what we are going to do is that we are going to see out the variable s which is now going to be 100 in this case more or we are going to see out this y actually. So we are going to write in memory address is and L and thirdly what we are going to do is that we are also going to see out x address as well. So we can write in x address is not just and L it's going to be and x and then an end out. Now what is this going to specify here is yes we need to add address here as well for y. Yes we need to add x here as well. Yeah now perfectly fine and a column right here. Now what is this these two statements actually going to specify when these two addresses are going to be equal which means that the value has been actually overridden. Let me just run it for you. You can see that before using the placement new operator the variable is 10 and the address is something like e34 and after using the placement new operator the variable is now 100 but the address is the very same because we have placed this 100 at the location of the x and you can see that the x address is also the very same. Now what happened actually here is that it is now clear that the new value of x is assigned at the address of x with the help of placement new operator. This is clear by the fact that the value of this x which is the x address and the memory address are both equal. Now let me show you this with the help of a diagram finally you can see right here. You can see that this was step number one in which we initialize the value of x equal to 10 and this was an x that was fe8 address can be anything. So then what happened that in the step number two we initialized a new variable y or you may say mem or any variable you want to initialize it and we initialize it the address of x which means that the new value that is 100 is going to be stored at the location of x at which 10 is already stored. So what it's going to do is it is going to remove 10 from there store itself right there. So when this x was printed out it printed out 100 the address of x as well as the address of this mem or y variable was also equal which shows that the value has been overridden successfully. So that's how placement new operator is actually used in C++. Now moving towards the new thing of the placement new operator is how we can delete the allocated memory in this. So the operator delete can only delete the storage located in the heap. So when the placement new is used, delete operator cannot be used to delete the storage. In the case of memory allocation using placement new operator since it is created in the stack the compiler knows when to delete it and it will handle the deallocation of the memory automatically. If required one can write it with the help of a destructor and how it is done with the help of a destructor let me show you this with the help of an example. So let me just move on to my editor and just remove this stuff from here. So here the first thing we need to do before doing anything is that we need to declare a buffer and we have to give it a size let's say 100 is the size of that buffer. Now before doing anything in this main function what we have to do is that we have to make actually classes in this case because we are going to do this with the help of the constructor and destructor function and why we are using destructor I've already told you because we cannot use the delete operator directly for the placement new operator. So we will define a class named let's say deletion. This class is going to do a lot but in this case we are going to make it as deletion because the main purpose of using of solving this example is just to show you the deletion operation. So we are going to have private members and in the private members we are going to have double RE underscore and I am at a score. So these are the two variables we are going to have and in the public area we are going to have the constructor as well as the destructor. So first thing is going to be the constructor that is going to be named as deletion and it is going to contain some arguments that are going to the values of the values which we write initialized right here. So it is going to be double RE equals to zero and double IM equals to zero. Moreover we are also going to write in IM underscore IM. So these are going to be something like these are going to be something like default values and these are going to be the past in values. So this is going to be the body of the constructor function. Now in the body of this constructor function what we are going to do is that we are going to simply print out the values. So we are going to write in RE this and similarly we are going to print out the other values as well. So now we will define our destructor function. So right after this here we are going to define our destructor function and you know that a destructor function has a negation sign at the start like this and then we have to mention the name of the class that we are going to use that is the deletion class and then this is our body of the destructor and in this destructor what we are going to do is that we are going to see out the values again. So you will understand that by in both the constructor and destructor we have just printed out these values. So down here after the destructor function what we are going to do is that we are going to return the square root of these numbers. So what we are going to write in that we are going to define a function as double. This is going to be the name of the function and this function we are going to return the square root and you know the square root is going to be something like RE steric RE added with IM steric IM and then we will have a print function and in this print function what we are going to print out that we are going to print out the value of RE and the value of IM. So these are going to be our end values that are going to be printed out at the very end. So now moving towards the main function code we have already initialized our buffer let's just remove these spaces from here. All right good enough. So now in this main function what we are going to do is that we have already initialized our buffer on stack. Now we are going to initialize objects and define the values for actually these two variables that are the RE variable and the IM variable. So what we are going to write and we are going to write in deletion and we are going to make them as pointers since in a case of this because we are pointing towards the location of the buffer. So we want the values which we print out. So let me just initialize those values first and let's say the values are 4.2 and 5.3. So now why I have used this pointer because I want these values to point at a certain location in this buffer. So that's why since they are pointing towards a specific address in this buffer so that's why this has been defined as a pointer this object has been defined as a pointer. So whatever the values are they are going to point towards a specific location in the buffer. So that's why they are objects. So let's make one more object and it is going to equal to new deletion and now it's time finally to use over object sorry the placement new operator and placement new operator is going to be used like the name of the object is going to be PE and then we are going to write in new and then we are going to specify the location at which we want to store our object and then finally we are going to call in this 2.6 comma 3.9 because these are going to be the second values that are going to be passed in. So finally we are now going to be using the objects so we are going to write in pc print then we are going to write in pd 0.print then pd 1.print then we are going to write in pe print. Now as you can see right here that the second object that was the deletion pd equals to new deletion 2 this means that these are going to be locations from 0, 1 and 2 so that's why we have written as 0 and 1 right in here we have not just used the second location which we are going to not use because this means two locations and pd 0 and pd 1 are those two locations. So now we have used these objects to call the print function so now we are going to release our objects and call the destructor and then release the memory so we are going to write in delet PC which is this object right here so after releasing the objects by calling the destructor function and then releasing the memory now we will call the destructor for object pd 0 and then we are going to release the memory and we are going to do the very same for pd 1 so we are going to write in delete not in this line let's make it in the new line so we are going to write in delete pd now we cannot make an explicit call to the destructor so we are going to write in pe deletion so if i just now run this code first let me just remove these spaces now run this code all right let's say sqrt was not declared in the scope yes we need to initialize the libraries for that right here and the library for that is going to be equal to hash include cmath and this is going to just solve all the problems so let's now run this code and you can see that it ran perfectly let me just scroll it down so that you can see that what is actually printed out so for the first call you know that when the object was initialized right at the top here the constructor function was called and 4.2 and 5.3 were passed in then for this 00 and this 00 because pd 0 and pd 1 were called and you can see that no values were passed in you can see right here the constructors are called two times for first for for deletion zero and then for deletion one but no values were passed in so as you can see right here that when no values are passed in the re and im are initialized to the values that are default values that we have defined right here as 00 so for pd 0 and pd 1 that these four zeros are present the first zero is for re then for im then again for re then for im these two zeros are for pd 0 and these are for pd 1 so then after that the deletion pe using the new buffer was called in and the constructor was also called in so this 2.6 and 3.9 were called in and then when pc print was called in these 5.2 and 5.3 4.2 and 5.3 sorry were called in and then again for pd 0 and pd 1 these four zeros were printed out and then when pe print was called in this 2.6 and 3.9 were called in and then these are the values down right here that are called in for this deletion function actually deletion PC is going to generate these four values and then deletion PD is going to generate these sorry deletion PC is going to generate these two values deletion PD is going to generate these four values because deletion PD contains pd 0 and PD 1 so PD 0 has these two values then pd1 has these two values and then pe and calling the destructor function deletes 2.6 and 3.9 from the scenes. So to give you a summary of what's going on in here you have clearly seen that here the destructor is explicitly called because here it cannot be packaged within the delete operator because delete will need to release the memory which you do not have here and it cannot be implicit as it is a dynamic process which we want to manage ourselves. So this is how deletion is done using the placement new operator in C++ so let's move ahead. So the next thing is what is segmentation fault. Now though the placement new operator should be used with care the address which is passed can be a reference or a pointer pointing to a valid memory location it may show error when the address passed is a pointer such as a null pointer a pointer that is not pointing to an indication and it cannot be a void pointer unless it points to some location. So let me give you a simple example of a C++ program to illustrate segmentation fault while using the placement new operator. So let's move on to def C++ and let's just remove all this stuff from the main function as well as this class right here. As well as we don't need cmath as well now so let's just remove it as well. So now what we are going to do is that we are going to define a pointer sorry not a pointer variable that is going to equal to 10 and then what we want is that we want a pointer let's name it as IPT and it is going to contain the address of whatever this value I stored in this is going to print out the address for us this pointer is actually pointing towards the location for example if you print out this it is going to print out the location where I store so this is the pointer that is pointing to that location where this 10 is stored at this time so we are going to define one more pointer let's name it as I1 and this is where the new operator is going to be used sorry not the new operator the placement new operator so we are going to write in new then we are going to write in IPT and we are going to write in int9 which means that we are overwriting this 10 because IPT is pointing at the location where this I store so when we write in IPT which means that the pointer that the address at which this 10 is stored at that address we now want to store 9 so if we now talk about this this is actually correct so we are going to write in correct here because this was a variable I that stores 10 and we initialize a pointer that pointed towards the location of where this I store and then we initialize one more pointer and using that pointer we used actually the placement new operator in which in the location part we specified that pointer which was pointing where this I was stored so now at this time this 9 is going to actually replace this 10 so this is a completely fine practice but now to talk about the incorrect method that is going to generate a segmentation fault for us what is that going to be it's going to be incorrect as the IP may not be a valid address for example if we write in int IP and then I write in int second pointer I2 and I'll make it as new IP and int4 so now as you can see that in this case this IPT was pointing at a location but now this is simply a pointer pointing to no location if you write in something like here and of I then it is going to be perfectly fine but in this case when this is not here and a colon is placed right here this means that this is a simple pointer not a location this is a pointing device but it's not pointing to some location as I told you before that the errors can be generated if a pointer is a null pointer a pointer is not pointing to any location and it can be avoid pointer until and unless it points to some location so in this case we have this pointer IP but it may not be a valid address because it is a pointing device but it is not pointing to some location as in this case this IPT was pointing at a certain location where this 10 is stored so using this IP as a location to store this for is going to generate a segmentation fault so if I just run this code you can see that it ran does not generate it any error but if you can just see you something right here something interesting but print it out let's say IPT in this case since it is used as a location and then I print out c out IP since it is also the location not a location in this case is a pointer it is not a location as I told you it is since this is incorrect if I just run it you can see the difference you can see right here that this IPT which was pointing at I location that address has been printed out but secondly you can see that nothing has been printed because there is no such location so this is an incorrect method in this case so now to talk about the second fault that can be here is right here so the correct method and then we are going to write in incorrect method all right to talk about the correct method let's just suppose that we have white pointer let's say VD and it is equal to end of I which means this very I in which then is stored and then we write in int steric I3 equals to new VD the pointer and we store in a value let's say 12 so this is completely okay since this is a white pointer but it is pointing to a location so this I3 is going to store this 12 sorry this I3 is going to point at the location where this 12 store and this is going to this VD is going to be the location in this case so if we just see out this VD and write in end L and run this code this was the address problem for previous part and this was not generated because of I guess this let's just remove it from here now run it you can see right here that that address has been printed out so now to talk about the incorrect method let's suppose that we have a variable let's say int a and we write in int the fifth pointer and we say that it is equal to new this a and we want to store a value let's say 6 so now it is not a valid address as well since this a is actually nothing it's simply a variable that also need a memory so we cannot write in something like this if we use it something like this when int i equals to 10 and then we write it in steric IPT we need a pointer that pointed the location of this then we can use this for example I'm talking that if we write in in steric let's say z equals to and of a and then we use not this a again this pointer actually we write in the Z here then it is going to be perfectly all right but in this case since we have this scenario this is completely wrong so if we just print out c out a since we are defining it as a location you can see that it is straight away generated and error so not allowed so let's just just remove this incorrect method from here as well so this is what segmentation fault is these are the two correct methods and if I just control that this and this is the second incorrect method so I guess that is clear so that was how segmentation fault can occur in your code so now moving towards the end slides now what are the advantages and this what disadvantages of using the placement new operator now the first is that the address of memory allocation is known beforehand which means that this is quite a big advantage of the placement new operator now the placement new operator is useful when when building a memory pool a garbage collector or simply when performance and exception safety are paramount which is a king counted as an advantage of the placement new operator moreover there is no danger of allocation failure since the memory has already been allocated and constructing an object on a pre allocated memory buffer takes very less times which means not only this is time efficient but moreover the allocation failure in the memory does not occur which is which are both the advantages of using the placement new operator and moreover this feature becomes useful by working in an environment with limited sources so these are very handy advantages I guess I discussed four advantages or five advantages and these are all the advantages of using the placement new operator over the new operator because these are all the disadvantages in new operator because the memory is not known beforehand we do not have a memory pool we have a danger of allocation memory since the allocation memory has not been yet allocated and the allocation on the memory that is not allocated takes a lot of time and this feature is not useful when we have limited resources because the new operator can not be used when we have limited resources so using the placement new operator these five advantages come into hand so I guess you know now what is the purpose of placement new operator how to create one how to use one how to define memory how to delete one what is segmentation fall and what are the advantages of placement new operator over the new operator so that's it with this tutorial thank you so much guys for watching and I'll see you guys in the next tutorial where we will be discussing about a new dimension of the topic so bye bye till then hey guys what's up this is Umar Han and I welcome you to a very new topic here in this section of one C plus plus and in this tutorial we are going to be studying the copy and swap idiom now before studying the copy and swap idiom what we are going to do is that we are going to study about the normal overloaded assignment operator and then we are going to study the drawbacks that occur in the overloaded assignment operator and then we are going to see that how these drawbacks are going to be solved by the copy and swap idiom so let me just show you an example of the use of the normal assignment overloaded assignment operator and you can see right here that we have an example already solved and in this example actually we are using the normal overloaded assignment operator so in this tutorial in this example as you can see right here that we have this two integer one is an integer value and one is an integer pointer and then we have these public constructors this is the first constructor that is actually the initializer list in which we have made s equal to zero size as then the ptr size is going to equal to the new in size and that's points corresponds towards the null pointer then we have this which is the copy constructor in this case and it is going to equal to an object of the constructor class and again the ptr size is going to equal to the new in size and it's going to correspond to a null pointer and in this constructor function that is actually the copy constructor function this over here is the construct copy constructor for constructor function so in this copy constructor what we are going to do is that we are going to call in the memory move function now what is the purpose of the memory move function the memory move function in c plus plus is actually used to copy a specified byte of data from the source to the destination now to talk about the parameter is it has three parameters actually the first one is the destination that is the pointer to the memory location where the content are copied to then we have the second one that is the source and it is from with that pointer to the memory location where the content are copied from and then we have the third argument that is the count where we see the number of bytes to copy from the source to the destination so you can see right here that this is actually the destination this is actually the source and finally this size steric size of int is the number of bytes that are going to be copied from this source where this ptr is pointing towards this destination where this object dot ptr is pointed which is right here so then we have this xn operator in which actually our main job is done so what is our main job in this case is that we are to actually call the overloaded assignment operator and that's where our overloaded assignment operator is being used so in this case this is the xn operator that is equal to the constant xn object which means that operator address operator is equal to the address of the object whereas the object is pointing where our data is actually copied so what we are going to check is that if this and what is this you already know what is this from our basic from your basic concepts that this is actually going to be the pointer that is actually the recent pointer so we say that if this is not equal to the location where this new memory is copied in so what we are going to do is that we are going to delete in the pointer the size is going to be equal to the object size the ptr is going to be size equal to new and size and it is going to correspond to another pointer the memory move function is going to be called and the ptr is going to be the source object dot ptr is going to be the destination and the size steric size of int is going to be the number of bytes that are going to be copied and what it is going to do is that it's going to simply return this and this is then going to be our destructor function if you want to use it so we just run this code you can see that it just ran perfectly now before going ahead you can see that the above assignment operator does two things the first one is that it does the self assignment check and where is this this is actually the self assignment check in which it checks that if the recent pointer is not equal to the memory in which the contents are to be copied so the second thing is going to check is if their assignment is not to self then it do three things it deallocates the memory assigned to the pointer it allocates new memory to the pointer and copying the values and finally it return the recent object that is this object that points to the very current location so now to talk about the drawbacks of the above approach the first drawback is the self assignment check as i told you the self allocation is done very rarely so the self assignment check is not relevant in most of the scenarios this just slows down our code the second drawback of the above approach is the memory deallocation and allocation as it can be seen that first the memory is deallocated leaving the pointer dangling and then the new chunk of memory is allocated now if due to some reason the memory is not allocated and an exception is thrown then the this pointer will be left a dangling pointer to a deallocated memory this scenario should be either the assignment is successful or the object should not be altered at all now this is where the job of the copy and swap idiom comes into account this approach allegedly solves the above issues and also provide a scope for the code reusability now let us see how this is done so we will just modify the code we already have here and our def c++ so let's just scroll above and this job is going to be the very same because this is the initializer function and then this is our function in which actually our constructor function actually in which our constructor's jobs are done because this is the copy constructor constructor in which our copying is done so this is going to be the same so right after this we are going to be using the swap function so the swap function looks something like right wide swap and why I've added this swap I will just explain in a moment and then the swap function takes in two objects actually and it takes in actually the pointers towards that object and it stores them here in as addresses so these addresses are then going to be used to swap the object sizes as well as the object pointers and when you swap the object sizes and the object pointers then this is going to actually swap your values so we are going to write an x and object one so this is object one and then we have x and object two now using these two objects what we are going to do is that we are going to swap object one size with object two size call in after this and what we are going to do next is that we are going to swap the pointers as well so we are going to write an object one dot ptr with object two dot ptr good enough so let's just place a call in here and now in the overloaded assignment operator the argument passed by values will call the copy constructor so right in here in this x and operator equals to const x and j we are going to just remove this set of statements and this is actually going to call our friend function so but before calling this friend function we need to just remove it and modify it as x object because we no no more need a memory we just need this obj that is actually printing out our memory part so if we just call our swap function because this is going to call our friend function and this is going to call it using the current object wherever where our pointer recent pointer is actually pointing at so this that that is going to be the memory where we are going to start from and we are going to copy this with obj at the location that is the destination location actually where we want to copy our memory and finally we are going to return this so that's i guess perfectly fine now and then we have this constructor function as usual so i guess that's cool enough so let me just run it now so it ran perfectly so now the memories have been swapped actually and that's where our copy and constructor function comes into hand now in the above example the parameter to the object are passed by values which calls the copy constructor to create the object local to the operator of the x-class and then the value of the temp object is swapped with the this object that is the left hand side of the assignment operator call now what are the advantages of using the copy and swap idiom the first advantage is that no more self assignment check is needed as the parameter is passed by value this means that no more memory memory deallocation is needed moreover a self allocation is very rare so the overhead of copying in case of self assignment should not be a problem anymore the second advantage is that now as copy constructor is used to create the temp object therefore the swapping will only be done if the temp object is at all created basically what we were doing manually there the compiler is going to do it for us in here the third one is the code reusability as i told you as we can see the operator does not have much code in its body rather we are using the copy constructor and swap function to do the job as if i'll just show you again you can see right here that we are again and again calling this swap function and we are actually using the code again and again which is actually a case of code reusability so i guess that's it with copy and swap idiom now you know that what are the advantages of using copy and swap idiom over a normal overloaded assignment operator so that's it with this tutorial in the next tutorial we are going to be starting a very new topic as in this section we are in every tutorial covering a new topic so in the next tutorial you will be seeing a new topic so that's it with copy and swap idiom thank you so much guys for watching and i'll see you guys in the next tutorial hey guys what's up this is umar han and i welcome you to another tutorial in this section on advanced c++ so in this tutorial we are going to be covering a new topic that is lambda expressions now lambda expressions in c++ were introduced in c++ 11 to allow us to write an inline function which can be used for short snippets of code that are not going to be reused and not worth naming in its simplest form lambda expressions can be expressed as shown on to you in the syntax on your screens you can see that it has a capture clause then the parameters then you to specify the return type and finally you have to define the definition of the method generally the return type in lambda functions are evaluated by the compiler itself and we don't need to specify that explicitly and the return type part can be ignored but in some complex case as in conditional statements compiler can't make out the return type and they need to specify that now various uses of lambda expressions with standard functions can be explained with the help of an example so let's just move on to a compiler here we are so the example i'm about to solve is going to be containing several paths the first part is that we are going to initialize a vector and then we are going to simply print it out using the lambda expressions secondly we will select a specific number from that vector and we will see the first occurrence of that number when it is greater than some number then we are going to sort that vector as well using lambda expressions then we are going to select a specific number and see that from how many number that number is greater the next we are going to just remove that redundant elements from that vector again then we are going to be calculating the factorial of a number using lambda expressions and finally we will find the square of a number using the lambda expression so it is going to be a very comprehensive example in which you will see all the uses of lambda expressions and i've covered all these scenarios because lambda expression is a very very very important topic so the first thing we need to do is that we need to initialize a vector it is going to be an integer vector whose name is going to be v and it is going to be equal to a set of numbers let's say four one three five two three one seven four i guess that's enough so this is going to be a vector in and this vector we are going to first print it out so the first thing we need to do is that we need to define a function here that is going to simply print out all the elements in that vector using the lambda expression for sure so the vector is going to be print vector and it's going to be a white function and in the argument it is actually going to receive that vector that has the name v and then what we are going to do is that we are going to use now lambda expression to print out the elements of that vector so we are going to use in for each loop for that purpose and then we are going to write in v dot begin v dot end and these are the expressions that were always used and finally int i so now in the body of that what we are going to do is that we are going to simply write in c out i so it is going to actually print out the elements for us so what actually is going on in here in this lambda expression is that we have actually used for each loop and in that for each loop we have actually written v dot begin which you know that it is going to actually grab the beginning of the vector which we have passed in right here and print start from the very first element v dot end is going to iterate it to the till the end loop and then int i in int i every time it is going to contain only one element so for this for each loop when it begins when this v dot begin is called it store the very first element that is for right in here and it come into this loop print out this for for us went back check for the next element which was not we don't end since this is the ending criteria for this for each loop so the next element that was stored in this int i would be this one then three then five and two and so on till four when this v dot end which is going to be this last element is going to stop this and the loop is going to stop so let's just run this code all right i guess a vector was not declared in the scope we have to include i guess library for that purpose that is hash include this is the library for that and one more thing we need to do right here is that we have to write an std so i guess that's good enough it's going to now run perfectly all right we have one more that says expected this yep i guess yes this this bracket don't need to be put here because for the for each loop this bracket has to be put after this body and a column right here this this is actually the ending bracket for this this ending bracket right here is for this int i and this is right for this this is how lambda expressions work on so i guess that's good enough so let's just run this code now and i think it's going to work perfectly fine now and yes it ran perfectly all we need to now do is that in the main function we have to just call this print function so that it just generates some output for us so we are going to write in print vector we need to be in small and what we need to do is that we need to pass in this vector so the name of this vector is right here we we pass it to this function print vector and now this vector and we has actually this vector which we have defined right in here so if you now run this code it is going to just generate an output for us as well so here is my output and as you can see that i have got all the elements that are four one three up till this four so this is how elements of a simple vector that are initialized in the sequence and then printed out in the sequence as well so let's just remove this end i from here add space here and i guess that's going to be much more better yeah that seems to be much more better way of presenting our output and just put a one line space here for our second output to find the occurrence of a first element greater than a specific element for example if we just take in this number four then what we are going to see is that we are going to find the first element that is actually greater than this four in this case it is actually five so our output is going to be something like the number that is greater than this four in this case it is going to be five so now let's just code this so in the main function what we are going to do is that we are going to be using the vector and using that what we are going to do is that we are going to be using an iterator p in which we are going to actually store our element and what we are going to do is that we are going to use in find if and the same conditions we don't begin and we are going to keep iterating until the very end so we are going to write in v and because we want to search for each and every element we want to check our first element for which we want to find a greater number so we want this four to be checked with each and every number so we're going to start from v dot begin and then i trade till v dot and and what we are going to search for is the number so that number is going to be stored in this int i and what we are going to simply do is that we are going to write in i is greater than four is going to be returned simple enough and here is where we are going to put in colon we just put a tap space so that it looks much more like the body path so this iterator p is going to store the value because this find if is going to start from v dot begin it's going to iterate till v dot end but since we have written in the body that return i is greater than four and i is in this case is going to be the value we want to check so the value we want to check since i for the first case is four so i is going to be four so it's going to start looking for this for any element that is greater than four so at the first case this four was greater than sorry this five was greater than this four so at the very first instant when it found this it returned it so the rest of this iterator loop was not executed and the first occurrences of any limit greater than this was printed out so in here we are going to simply see out first number greater than four is steric p and now you might be thinking that why i've not called this find if function because this is how lambda expressions work iterator p actually with this variable when i call in like this what it's going to do is that it's going to execute anything with this now what is the purpose of this find if is it a building function or is it not yes it is it is a building function and what it does is that it is going to search for an element for which any argument which we passed in becomes true since in this case the third argument that this is the zero this is the first this is the second and this is the third argument the third argument returned true so this loop is going to stop so what it's going to print is is going to print that number at which the condition becomes true so in this case the condition becomes true at this five right here so this p is actually going to point at this location so when i write in steric p it's going to print me out that number so if i just run this code yes i've messed the body brackets and this needs to be right in here all right good enough now let's just place it right over here as well so it's going to now work perfectly so you can see right here this is our first output that was printing out the simple vector and then this is my second output that says the first number greater than four is five so now towards the next part and in the next part what we are going to do is that we are simply going to sort this vector which we have right up here so we are going to sort it up starting from the greatest number and then moving on towards the smallest number so it's going to be sorted something like seven five then four then three and then two then one something like that so let's just code it as well so we will just code it right behind this so what we are actually going to now do is that we are going to make a function to sort the vector using lambda expression and lambda expression is for actually sorting in the non-decreasing order compiler can make out the return type as bool but we can just make it work our own way so how it is going to be done it's going to be done using sort function that is also build-in function and what it's going to do is it is going to start from the very start that is v dot begin and it is going to iterate till v dot end and then these are the bracket for the lambda expressions and then in this bracket this time what we are going to do is that we are going to write in constant int and a and constant int and b and they are going to correspond to a bool value now what is actually going on in here let me just code the body first and then i'll tell you what is going on in here so right before behind this what we are going to write in we are going to return a is greater than b and then this column right here now right above here what i just told you that whenever this condition is true then what it's going to do is that it's going to stop right in here and it's going to return that number for me so in this case right down here this sort function is going to actually sort starting from the very first element till the very last element and now what is the purpose of these two constant integer addresses actually here now these are actually going to store the two values and then if this condition is true which is return a is greater than b since in this case we want the elements to be sorted in the form of decreasing order so if this bool is actually true so what it's going to do is that which means that this is actually going to be true so when this is true this bool is going to be equal to true and the two values are going to be stored right in here and those values are then going to be sorted out using this function when these are not for example some values are always there that don't need to be sorted out for example in this case let's just see this case right here these four and one don't need to be sorted because four is already four and all already in the form we want them to be on the output screen there need to be elements in between them but for this case for the first comparison this condition is actually false which means that a is greater than b is false because a at this time is four whereas b at this time is one so this condition is false so this bool is false so we don't need to store some values in here and perform the sorting function values are stored in there four is stored in there one is stored in there so that's why they check in using this return a is greater than b but the sort function don't work for such values which are always in sorted form so for values for example three and five or you may say one and three such values need to be sorted out so in that case one is going to be stored in int a three is going to be stored in constant int and b and these are going to be sorted out using this sort function so when this function actually complete from v dot begin till v dot and then we already have our array sorted out all we need to do is that we need to print it out so how to print it out we have to actually call simply the print vector function and pass in our vector right from here a column right in here and let's just see out as well sorted vector is this print vector v and finally and l so now if i just run this code all right it says some error and this it says no match for greater reference here yep a very silly mistake we can just see out this print vector v because we are not actually returning something from up there so let's just remove this from here or let's just cut it out and we have simply called in this function and right above here we can just write in this statement sorted vector is and then this print vector v is going to simply print out each and everything for us we don't have to specify c out with a function here that's just silly of me so let's just run it now as you can see right here that we have the sorted vector right here seven five four four three three two one one so you can see that we have this simple vector then we sorted out this using the lambda expression and finally we sorted out our vector using the lambda expressions so now moving towards the next spot and in the next spot what we're going to see is that we are going to actually count the numbers that are greater than or equal to a specific number so how it is going to be done let's just call it so right down here we are going to initialize the variable name its count because what we want is that we want a count of a number that that is greater than or equal to some specific numbers we want that how many numbers are greater than a specific number so that are going to be stored in this count so we are going to write in count underscore if and then we are going to write in v dot begin then v dot end then this and finally we are going to write an int a that is actually going to store the number for us and then this is going to be the body and in this body what we are going to do is that we are going to return a is greater than or equal to let's say five so the number that we want to check is actually the five so write this call in here and write this call in here so one more thing before i run this code that i need to see out the number of elements greater than or equal to five are actually whatever the values of this count variable now let me just you one more thing before i run this code because the coding is complete just i want to tell you one more thing here now you can be seeing that this count if function this sort function then this find if function we are actually and then finally this function also as well this print vector function all of these are lambda functions so as you can see right here that when i have to use this print vector function i have to actually call it as you can see that i have called it twice once when i was printing simple the vector and secondly right down here when i was actually printing the sorted vector so and i don't call these functions right in here that that sort function the count if and then the find if function right above here now what is the reason for this because as you can see that this is the main function these functions that at the point of function this sort function and then right at the bottom the content function are actually in the sequence of this main function so these don't need to be called they are called by themselves that's why i'm always calling them as build-in function these are not something like building the library of the c++ they are not because these are actually lambda expressions they are called in by themselves they are executed by themselves that's why they are building they are not building the sense that they are something like store in the background of our c++ libraries like we have this int stored we have this return value that is something like a build-in value that is stored in our c++ libraries and variables or something like that this is something not like that completely not like that because this find it is a normal function but since it is a lambda expression right here so this don't need to be called when the sequence of execution comes here and it sees that it is actually talking about a lambda expressions so it execute the body of it as itself it don't need to call it but why do we call this function is because this is out of the body of the main function so when we have to use is the sequence of execution don't come here at any stage but it can come here at the stages when we call it so when we call it twice the sequence of execution moves right here and this function is called but if this function was right in between these in the main function we would not have the need to call this function but if we have used it since you know that we have called this function twice so if i just copy it from here and paste it instead of this and also this right here then this is not actually a good programming technique because in c++ or any other programming language code reusability is something so we have to reuse of a code so that's why we have written this set of statements which we want to execute again and again it is a lambda expression i'm not saying that it is not a lambda expression it is a lambda expression but they are written right at the top here so that we only have the need to call this function not repeat this entire set of code because that this would not be then a good programming technique so i guess uh now i need to run it and you can see right here that the number of elements greater than five or equal to five are two as you can see in this that is the first number and that is the second number these two elements are actually greater than or equal to five this seven is greater than five and this five is equal to five no other number is greater than or equal to five so this count was equal to two so the next thing in this tutorial we are going to do is that we are going to make a function for removing duplicates from this very list available that is in the sorted form now so what we are going to do is we are going to do it right here so we are going to write in p equals to unique and we are going to then again write in v dot beggin we are going to write in v dot end and then this is the sign for the lambda expressions and then again since in this we are also talking about comparisons so we need two values that is int a and int b and then we have this body right here and but we are going to do is that we are going to return a equal equals to b and a column right in here so i guess that's good enough let's just run it now but before i run it this actually just remove over elements so all we need to do is that we need to call our print vector as well now we have to pass in the same v and let's just print out a cout statement at the top here as well that says unique elements in the list are right at inhale because since this function it's going to start from the very start it's going to move at the very end and it is going to return the values that are actually equal so when i just run it it's going to print out the unique elements for me so you can see right here that it says that the unique elements in the list are seven five four three two one and then the repeated elements as you can see are put right at the bottom you can see that it first printed out the unique elements for me that at seven five four three two one and after that as you can see if you consider this sequence it has a four four then a three three and then a two one one but in here it looks for this element it has removed this four from here it has removed this three from here and it has moved it up till here and then finally it has actually concatenated this two one one right in here so good enough so the next part is how to find the factorial of a number now how to find the factorial of a number actually we have to store all the values into an array first for example if you want to find the factorial of ten then you need an array that stores elements from one to ten because you can actually not i trade this loop again and again so for that what we need is that we need an array that contains element from one to ten good enough now what we need to do is that we need to actually store it in a value as well so it is going to be equal to let's say the name of the function is tutorial or let's just call effect and it is going to take in the array from one to ten and it is going to start from the very first value and these are the new brackets for the lambda expression and finally we are going to actually store two values and i and nj and i and nj for the very first case is going to store one and two and what it's going to do with one and two is that it's going to simply return their product so it's going to multiply them and the next time this three is taken into account it is going to multiply this product with this three and at the end our factorial is going to be found so we can just see out factorial of ten is so let's just run this code all right it says that fact was not declared in the scope all right yes the name of this function has to be accumulate because these are some of the names that we have to use for lambda expressions these names right in here we will begin we don't end then this name of the function name count if these are actually something that we have to use in again and again so then finally we are going to put i guess nothing else let's just run this code and it says the factorial of ten is something very big value yes it is so you can just find it using your calculator as well and the final thing i told you was how to find actually the square of a number so for that i'm going to write an auto square square and i guess we don't need any spaces in between this and it is going to equal to and i and this int i is actually going to be the value which we are going to pass in ourselves and for the body of this what is going to do is that it's going to return i steric i which means that it's going to multiply it with itself which is actually the square because square of the number is multiplication of the number by itself so down here we are going to write in c out square of let's say we want to find the square of six is we will write the name as square and we're going to pass in six and i guess we need a space here as well so now let's just run it all right this don't need to be here now let's just run it and you can see it right here that the square of six is 36 which is perfectly correct now this auto here is actually going to automate the square whenever it is called so this i was paused in which was six so it returned six cross six that was 36 which was printed out using this c out statement now a lambda expression can have more power than an ordinary function by having access to variables from the enclosing scope we can capture external variables from enclosing scope using three ways the first one is by using capture by reference then capture by value and then capture by both now the syntax used for the capture of a variable are some signs for example we can use the and sign that is for capturing all the external variables by reference then if you want to capture by value you have to use the equal to sign that capture all the external variables by value then if you want to capture by both value as well as by reference then you have to write in a normal variable for example let's say x and then a variable after putting a comma sign and of y for example you can write in something like and of x and then sorry not x and then and of y so this is actually going to capture both by reference as well as by value so a lambda with empty capture clauses can access only those variables which are local to it now how this this capturing is actually done so if you can see right in hill write about in this for example let's say let's talk about this function in which we have actually printed out the number which in this case we were actually talking about the first occurrence of an element which is greater than this four so whatever we want to talk about some other element for example we want to talk about this five and see that which is the occurrence when this element is greater than something so for that what we are going to do is that you have to specify for example let's say int n and we say let's in n equal to five and then in these brackets we have to write in this n and in here we are going to write in i is greater than n so this is how we have actually captured variable so if i just run it now you can see right here that the first number greater than four is seven it is not actually now four it is actually now five so in the previous case the output was this five because we were talking about four the first number greater than the four was this five but the first number now greater than this five is actually seven so now we have actually captured the value that is in between any value in these brackets of the lambda expressions you can see right in here that now we have the first number greater than five is actually this seven so now to give you one more example we talk about let's say this count of case in which we were actually counting number of values that were as greater than a specific number so if we just write in an equal to sign here which means that now we are talking about capture all the external variables by value so when we put this equal to sign in here this denotes that we can access all the variables now it is going to count numbers that are greater than or equal to n actually so let's just run it again now one thing we need to check our stop we are not talking about this unique we are talking about this counter function so we have to put in this equal to here and now we are going to write in a is greater than or equal to n so now we are going to talk about capturing all the values by reference and we are going to actually access all the variables so in this case we have actually access this n so now in the previous case it was giving us two because the occurrence of the specific value was two but now in this case it's n which is five so if we just run this code so you can see right here that the number of elements greater than or equal to five are actually two so actually we have talked about the very same element let's just change it to six not six because six doesn't exist let's just change it to one and right here we are also going to change this equal to one and now run it so as you can see right here that this value is also changed and you can see right here that the number of elements greater than or equal to one are nine because all the elements are greater than or equal to one so this generated the total number of elements for us and you can see right here the first number of grades in it now it's actually talking about one so the first element in our list was four which was greater than one so it generated as four as an output so this is how we can actually capture some values so we have already solved our examples so one more thing before I sign up from this tutorial is that you need to understand that c++11 and the versions after the c++11 only supports lambda expressions if you are working with any compilers that are before that c++11 and they're after versions then they are not going to be compatible with lambda expressions and they are not going to run that for you so you need to keep this in mind that when you are running it you have to check the compatibility of your compiler that it is c++11 or the version after it so thank you so much guys for watching I'll see you guys in the next tutorial hey guys what's up this is Sumerhan and I welcome you to another tutorial here on this section on advanced c++ so in this tutorial we are going to be starting on with a new topic that is signal handling now what are signals signals are basically the interrupts that force an operating system to stop its ongoing task and attend this task for which the interrupt has been sent these interrupts can cause a service in any program of an operating system similarly c++ also offers various signals which it can catch and process in a program now as you can see right here we have a list of various signals and its operation that c++ provides the user to work with the first one is the signal int that produces the receipt for an active signal the second one is the sigterm that send a termination request to the program then we have the sigbus that is the bus error which indicates access to an invalid address then we have sigll that detects an illegal command then we have the sigalrm this is used by the alarm function to indicate the expiration of a timer then we have some more functions right here that is the sigabrit that operation is to termination of a program abnormally then we have sig stop the signal can be blocked cannot blocked handled and ignore and can stop a process then we have sigsegv that's operation is to detect invalid access to storage then we have sigfpe whose operation is to overflow operations or mathematically incorrect operations like divide by zero then finally we have sigusr1 and sigusr2 that are actually the user defined signals now we have this signal function the signal function is provided by the signal library and it is used to trap unexpected interrupts or events this is the syntax right here in front of you that is the signal the registered signal is the first argument and that signal handler is the second argument so the first argument is actually an integer and it represents the signal number and the second argument is actually the pointer to the signal handling function we must keep in mind that the signal that we would like to catch must be registered using a signal function and it must be associated with a signal handling function now the signal handling function whenever it is defined it is going to be a white type now let me just show you an example of how this signal function is going to be implemented and how an interrupt is going to be caused and it is going to just stop the flow of a program so they just move on to my editor right here and the first thing we need to do is that we need to actually initialize one more library right here that is for signal handling and it is hash include and the name of this library is c signal not in capitals all right so this is actually the library that is the c plus plus version of the standard c signal dot h header and it declares function and macros related to the signal handling library so now in the main function we are actually going to do the calling part but before that main function we are actually going to use our functions so for example we have one function that is named as signal handler and in this signal handler function what we are going to receive is the signal number that is going to cause an interrupt and what we are going to see out is simply we are going to write in interrupt signal and then we are going to print out its number that is the signal number and then we are going to write in received so it is going to simply print out the number that is going to be received. And then this is going to end. And then what it's going to do is after receiving this signal that it is going to clean up and close up some stuff here and terminate the program. So for that, we are going to write an exit. And signal number is going to identify the signal which we want to exit actually. So now down here in this main function, we just scroll it down so that it is much visible. Right in here what we are going to do is that we are going to register the signal using the S, I, G, I and T and the signal handler. So we are going to call in the signal function. The first thing that we need to do in here is that we need to use actually some library from right in here. So we are going to use in this signal initializer that is going to produce the received for an active signal. So if we move back to our def C++ right in here, we are going to write an S, I, G, I and T. And then what we are going to do is that we are going to call in signal handler. And finally, what we are going to do is that we are going to actually run a program. For example, let's say it's while one, what it's going to do is that it's going to see out going to sleep and it's going to sleep for one second. This is actually the time. And then finally, what we're going to do is that we are going to simply write in return zero here. So now if I just run this code, you're going to see something very interesting. What's the problem with this? It says that sleep was not declared in the scope. Yes, I think I just forget to include the library for that. And it is going to be hash include unistd.h. This is actually the library to run the sleep function. What is the purpose of actually the sleep function in here? It is going to actually print out this going to sleep after one millisecond. So now if I just run this code, you can see right here that going to sleep, going to sleep, going to sleep. But if I just press in control plus C, you can just note it down that you have to press control plus C to actually initiate that function. So if I just press in control plus C, you can see that this function was actually executed and it existed whatever the signal number was. So if I just put in a sleep timer here to just show you what happens actually here, sleep one, now run this code, you can see right here that it says going to sleep, going to sleep, going to sleep. But now if I just press control plus C, it says that interrupt signal to whatever let's just increase this timer so that it is visible for some time so that I can show you. Now you can see for now press control plus C, it says interrupt signal to receive. This is actually the signal number that is received. So after six millisecond, the program ended because this condition that says exit signal number two. So it was excited actually. So I hope this is clear that how the interrupt signal was actually caught in when we press in control C. So now moving ahead. So you know now how the signal function work, how you have to use the register signal and signal handler. And now the next thing is the raise function. The raise function is actually used to generate signals and the syntax for it is very simple. You try it and raise and the signal. This argument is right as mentioned in the list. So let's just solve an example for how this raise function actually work. So let's move on to our editor. And we will have the very same program in which we will have this white signal handler in which we will just print out this interrupt signal and then the signal number. And then we will also just exit the signal number and let's just decrease the sleep timer to three. And then right here in this main function, what we are going to do is that we are going to initialize a variable that is in i equals to zero. And then what we are going to do is that we are going to in this very same way call in this function. And let's just remove it from here. Let's just align it a bit. And now after calling this function, what we are going to do is that now we are going to initialize a while loop that is sliced using plus i i not plus i it's going to be plus plus i. And what we are going to do in this function is that we are going to see out going to sleep. And now we are going to use an if condition that if i equal equals to three, what is going to do is that it's going to raise the signal. And what is going to be the signal it is going to be s i g i n t and then a sleep function right in here for one second. And then finally our return zero is already here. So let's just put away these lines and run this code. Now as you can see that when the code is executed, it produces this output and it would come out automatically that the interrupt signal two is received because now actually we have raised a signal when i was equal to three. So it printed out going to sleep three times. We don't have to press control plus see this time because what the race function did was using right in here. It raised a signal and initialized that signal. So when this function was called in this white signal handler was automatically called in the previous case, this signal race function was called in when we press in control C. But now we have actually done it using a code and we have used the race function for that purpose and right in s i g i n t. So when this function is called in when i equal to three, which means that three times going to sleep after one second is going to be printed first. And then it's going to print out interrupt signal to receive and it is going to terminate that program. So I guess that is clear. So this is how you can just use or use the signal function. And this is how you can just use the race function to actually initialize your signal. So these are some of the other functions as well which you can use. So you can see the examples of them yourself. These are quite easy. You have to use them in the very same fashion as I explained this first time right first word right here. So I guess that's it with this tutorial. Thank you so much guys for watching and I'll see you guys in the next tutorial. Hey guys, what's up? This is Umar Khan and I welcome you to another tutorial here on advanced C++. In this tutorial we are going to be starting on with a new topic that is command line arguments. Now to tell you anything about this command line argument and what is the purpose of command line arguments in C or C++. You need to know one thing right here. Now you know that there are many kind of functions you can use in C++. You can use build in function. You can use manual function. And then we have a function named as min that actually controls the flow of your code. So the most important function of a C program or a C++ program is that main function. It is mostly defined with a return type of integer and mostly without arguments. Now we can also give command line arguments in C and C++. Command line arguments are given after the name of the program in command line shell of the operating system. To pass command line arguments we typically define main with two arguments. First argument is the number of command line arguments and second is a list of command line arguments. Now this is how the syntax actually look like. You have the main function then you have two arguments. First is the integer argc and then we have the character a double pointer and then argv. Now if you talk about the first one that is int argc this is actually the argument count. It is an integer and it stores the number of command line arguments that are passed by the user including the name of the program. So if you pass a value to a program value of the argc would be two. Now why it would be two because the first value in the argc is going to be for the argument and the second one is going to be for the program name. Then we have the second argument that is char then steric steric and argv. This is actually an argument vector which is an array of character pointers listing all arguments. If argc is greater than zero the array elements from argv zero to argv until argc minus one will contain the pointer to strings. Now argv zero in every case is going to be the name of the program after which all the arguments are going to be initialized. So let me just show you a very simple code and then you can see the difference between these two arguments and how they are actually going to be used. So here we are in defc plus plus. Now in the main function what we are going to do is that we are going to pass in two arguments actually. Right here the first one is going to be argc that is the argument count as I told you and then we have this character steric steric argv that is the argument vector. Now in this function what we are going to do is that we are going to simply see out and we are going to write in you have entered argc which is actually the argument count so it's going to generate as the argument count and then we are going to write in arguments. So it is going to be a simple statement that is going to say that you have generated for example let's say if you have two arguments then it's going to say that you have entered two arguments. So now what we are going to do in this main function is that we are going to initialize a for loop and in this for loop what we are going to do is that we are going to write in i equals to zero i is less than argc and then i plus plus is going to be a colon and it is going to be a pre-argument not a post one so it's going to be plus plus i not i plus plus all right. So now in the body of this main function what we are going to do is that we are going to simply see out argv that is our argument vector and here and i and finally what we are going to do is that we are going to write in return zero after this. Now for example we have two arguments then the value of this i is going to be equal to two because this argc is actually this argument count so if we have two argument this loop is going to be running for i equal to zero and then i equal to one and after that this condition is going to be false because now then two is not less than two because two equal to two so i equal to zero this condition is going to run perfectly and this for loop whatever it is whatever this statement is it is going to be executed and then for i equal to one this statement is again going to be executed and what this statement is actually because as I told you that this argv is actually the argument vector so for two arguments what is going to do is that argv zero and then argv one is going to be printed out now always remember that argv zero in every case is going to be the name of the program and then argv one until argc minus one since for two this argc is not going to this condition however here is not going to run because this condition is going to be false so it is going to run until argc minus one so every other value other than i equal to zero until argc minus one is going to be the elements in the argument vector so if you just run this code now write it is scanning this all right it says you have entered one argument since now we have not called this function because this function is going to be called from your command line and where is that command line actually this command line is actually going to run from your Linux machine so if you run this code from your Linux machine by entering this coordinates and let me just write you the code for that so that you can just run it so you can write in in your Linux machine main return dot cpp minus o means output and then main and then you are going to write in main and then you can write in anything like let's write your name anything else so what is going to do is that is going to run your code for you in this case what is this omer and this han is going to be the argument which means that we have passed in two arguments to this main function so in this case it is going to generate that you have entered three arguments why it is going to say that you have entered three arguments because first is the name of the program as i told you then this omer is the first first argument and then this is the second argument so zero one two makes equal to three arguments so it is going to generate you have entered three arguments so if you write in something like anything like i am cool so then it's going to say that you have entered four arguments so you can just run it from your Linux machine and then you can see that how it is going to run so you have seen this now no other platform dependent formats are also allowed by c and c plus plus standard for example unix and microsoft visual c plus plus have a third argument given as the program's environment otherwise accessible through get env in standard library dot h so for that you can refer the c programs now to talk about the properties of command line arguments first property is that they are passed to the main function they are the parameters or arguments that are supplied to the program when it is invoked thirdly they are used to control the program from outside instead of hard coding those values inside the code fourth one is that argv argc is a null pointer then argv holds the name of the program as i told you and then argv one until argc minus one points to the first command line argument and then so on until the last argument so you pass all the command line arguments separated by a space but if argument itself has a space then you can pass such arguments by putting them inside double codes or single codes now to talk about the last thing that is what are the changes in output if you pass in different arguments for example if you pass no argument and call it without argument one the above code is compiled and executed without passing an argument it is going to say that you have entered only one argument and that argument in that case is going to be actually the name of the program as i told you when i ran that code but run it again you can see right here that it says that you have entered for an argument because we have passed in nothing to this now if you pass in three arguments what it's going to say is that you have passed in four arguments in which the argv zero is going to be the name of the program and then argv one argv two and argv three are going to be the first second and third argument respectively similarly if you pass in a single argument then what is going to say is that it's going to say that you have actually passed in two arguments the first one is going to be the name of the program that is argv zero and argv one is going to be the first argument in this case then for example you want to pass an argument that already has a comma in between then you can use quotation marks for that purpose for example you can quote a larger statement in single quotation marks for example you can just write in like i am and comma then so this kind of arguments in which you have a comma in between you can put them in quotation marks and it is going to treat them as a single argument so in this case it is going to also say that you have passed in two arguments first argument is the name of the program and the second argument is right in here in this quotation marks so i hope it's clear so that's it with this tutorial and that's how you can use command line arguments you can try it in your Linux machine and it's going to run very perfectly and you're going to enjoy it so that's it with this tutorial thank you so much guys for watching and i'll see you guys in the next tutorial