C++ Fuctifier Usage: What is a Funger in C++? How to use functors?

What is a functor in C++? The word functor is very famous in C++. A functor is much like a class that behaves like a function or method. It will let you make some function-like objects and can use operator(). In simple terms, functors are those objects that can be used as functions or similar. These are useful when the user wants to pass more than 1 argument in a function and the function doesn’t allow it. Today, we’re going to learn about functors in C++ in Ubuntu 20.04 and combine some examples of C++ functor usage. How to use C++ functors? First, use the “Ctrl+Alt+T” shortcut on Ubuntu 4desktop to open the shell terminal.

Example 01:

Let’s start our first example by creating a new C++ file in the shell. The well-known command “touch” will be used for this purpose. Once you’ve made a C++ file, you’ll need an editor to open it. The “Gnu Nano” editor is one of the most recommended. So we’ve been using the “nano” editor to open it and create C++ code in it.

What is a functor in C++?

Once the file is opened, launch your code by including an Input-Output flow header at the top of it. A standard namespace has been given; The “Func” class has begun. Operator() has been implemented in this class, which looks like a function, but it’s a call to the functor operator(). This operator looks like it takes two arguments, and if “x” is greater than “y”, it returns true or false to the main method. The main method defines three integer type variables, and the integer values are initialized as “x” and “y”.

Another integer variable “res” is declared to get the return value from the funcrel. This “f(x,y)” looks like a function call, but it’s not. This syntax is used here to call the operator() of the funclot. If the variable “x” is greater than “y”, 1 is returned; Otherwise, the variable “res” is 0. The “cout” statement is used here to print the resulting value.

C++ functor usage example: After using Ctrl+S to save the code, use the Ctrl+X shortcut to save the code. Now, it’s time to compile the C++ code. For compilation, make sure you have configured the g++ compiler on your Ubuntu 20.04 system. So, here we use the g++ command to compile the “functor.cc” file. After this, ‘./a.out’ has been used to execute the file. The execution result shows “1”. This means that the variable ‘x’ is greater than the variable ‘y’.

Let’s update our C++ code. Open the same file using the nano editor on the shell as shown below. The only change you need to make is to replace the “greater than” symbol in operator() with the “less than” symbol. The rest of the code will remain the same. Save the updated file and keep it while using Ctrl+S and Ctrl+X continuously.

Once the file is saved, the code must be compiled again. So, again, use the g++ compiler command to do this. After compilation, the execution of this newly updated file returns 0 because the variable “x” is not less than the variable “y”, i.e. 13 and 6.

How to use C++ functors? Let’s say you want to add two integers without using a function. Fusals are here to help. It can do this using the plus operator in it. So, open the C++ file “functor.cc” in the editor, i.e., GNU Nano, and update its code again. The “Nano” command will be used for this purpose. Once we have this file open, let’s update it in the same place that we did earlier. We replaced the “less than” sign with a “plus” sign between the variables. This is by adding the value of two variables and returning that value to the object call.

The function call saves this value to another variable “res” and displays it on the shell with the help of the “cout” clause. The same code will be used for subtraction using the “-” symbol. Press “Ctrl+S” again to save your code. Now, use Ctrl+X to return to the shell terminal.

What is a functor in C++?

After exiting the C++ file, use the G++ compiler and the name of the C++ file to compile the new code in it again, the functor.cc, as shown below. After compilation, run the updated code file with the help of the “./a.out” directive, as shown in the image below. Due to the sum of two integer type values, i.e., 13 and 6, the output shows a value of 19.

C++ Functor Usage Example 02:

Let’s use a new example to illustrate the function of a functor object. With the help of the “touch” directive, create a new C++ file or use an old one, i.e. functor.cc. Use the nano command to open the created file in the GNU Nano Editor, as shown in the image at the beginning of this article. Now, add the header file of the input and output streams at the very top of the file. The standard namespace must be declared with the keyword “using”. The class “Func” has been declared, which contains a public type object. The first is a constructor named “Func”, which takes an integer type argument in its arguments. The parameter values of this integer type will be saved to the variable “a” with the help of “_a” and “(a)”. This constructor doesn’t do much more.

After this, the operator of the functifier is used here to subtract the values of the two integer types. The “Func” class defines the private data type variable “_a”. Here comes the main method. First, we create an object “f1” of class “Func” and pass it a value of type integer, i.e. “13”. Now, after creating this object “f1”, the constructor “Func” of class “Func” is executed and the value of 13 is saved to the integer variable “_a”. After that, the “cout” statement is used here to display the objects of the class. There is nothing to confuse.

When an object is called, this means that here the funx operator() is called and assigned a value of “6”. With this, the fungi uses operator() to calculate the subtraction of the two variables and return it to the main program. Let’s save the code file and exit. Use “Ctrl+S” and “Ctrl+X” here.

Example of C++ functor usage

Now the code for the C++ functor.cc file has been saved; Compile it before executing. Use the same “g++” compiler command to make it error-free. The compilation was successful, and we entered the execution phase. When executing this file with the “./a.out” command, we get 7 as a subtraction result.

How to use C++ functors? Summary

What is a functor in C++? So, that’s all there is to using functors in C++ when working on an Ubuntu 20.04 system. In the C++ functor usage example above, we’ve used operator() to call the functor. We’ve also seen how to use Functor with classes and their objects in C++. We believe that all the examples are easy to do and can help you easily understand the concept of Functors on Ubuntu 20.04 Linux.