Higher Order Functions in C++

07 Jul, 2021
Higher-order functions are functions that take functions as an argument. It is used in functional languages which is not used in C++ are, although this is slowly changing since C++11 gave us lambdas and ‘std::function’... and frequently people don’t realize that ‘std::function’ is not a tool that fills all use cases. In this article, we will explain how to pass a function as an argument to a different calling function.
Syntax:
return_type function_name(function<return_type fun_name(argument_list), other function arguments)
Parameters: In the above function_name takes a function named as fun_name as an argument. Both the function_name and fun_name can have some additional arguments.
Below is the illustration of the same:
C++14
// C++ program to illustrate the higher// order function in C++#include <bits/stdc++.h>using namespace std; // Function that will be passed as an// arguments to calling functionbool Parser(string x){    // Check if string start    // with alphabet 'R'    return x[0] == 'R';} // Function that takes function as// an argumentsvector<string> Parse(vector<string> a,                     function<bool(string)> Parser){    vector<string> ans;     // Traverse the vector a    for (auto str : a) {        if (Parser(str)) {            ans.push_back(str);        }    }     // Return the resultant vector    return ans;} // Driver Codeint main(){    vector<string> dict = { "geeks", "Rxop",                            "Rka", "for" };     // Function Call for Higher    // Order Functions    vector<string> ans = Parse(dict, Parser);     // Print the results    for (auto str : ans) {        cout << str << " ";    }     return 0;}
Rxop Rka
 
Advantages of Higher Order Functions:
By the use of the higher-order function, many problems can be solved. For Example, to build a function that takes a list and another function as its input applies that function to each element of that list, and returns the new list. In Haskell, this could be done really easily using the inbuilt higher order function called map. Definition of the map is:
map :: (a -> b) -> [a] -> [b]  
map _ [ ] = [ ]  
map f (x : xs) = f x : map f xs
Here,
The first line is the function initialization.
The elements symbol stands for “is of the type”.
[a] represents a list of similar elements, the entity has written after last -> is always the return type of the function.A function in Haskell always returns only one entity.
(a->b) defines a function from ‘a’ to ‘b’. We used recurrence to define map, [] denotes an empty list and _ denotes “anything”.
Second-line depicts that if the empty list and any function is input then the output will be empty list.
x: xs is used to take out elements one by one from the list, x is the first element (head) and xs is remaining list (tail). : sign stands for concatenation. So in a nutshell, the third line is taking each element from the list and applying function ‘f’ on them, and concatenating it with the remaining list.
anikakapoor
simmytarika5
Functions
STL
Articles
C++
C++ Programs
STL
Functions
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Docker - COPY Instruction
Time complexities of different data structures
SQL | DROP, TRUNCATE
Difference Between Object And Class
Implementation of LinkedList in Javascript
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
std::sort() in C++ STL
Bitwise Operators in C/C++