Example of a C++ vector whose elements are functions.
Finally I have built a C++ vector whose elements are functions that accept and modify arguments. With this method it is very easy to iterate over a collection of functions. ( In this example all function elements of the vector have the same kind of arguments, but even if the arguments of the functions are very different, you can at least make the functions read their arguments from a disk file.)
I am listing the code below, at least on the surface it is compiling and executing without error, but if you can find any hidden errors please let me know.
I haven't yet learned the lambdas of C++11, but if you know how to use lambdas or other advanced methods to create better data structures that make it possible to parametrize functions in a manner that can be used to iterate over a family of functions, please let me know. Another point of lambdas might be the capability to generate or "write" many thousands of functions automatically (as code!?!? ) real time, depending on the events that are happening. Perhaps, this way, a library of functions can be generated automatically or at least semi-automatically according to incoming experimental data, for later use, and then a map data structure can also be used to keep track of the family of functions that will be placed in a vector data structure. Since the map is in some sense the dual of the vector or array, this can make it possible to write a very effective program. If you have many thousands of functions to choose from in the logic of the program, you cannot afford to write that many lines of if statements manually, but iteration over the functions will do that indirectly.
Basically I have imitated part of this code from the Internet, which creates an array whose elements are functions:
Array of functions? - C++ Forum
Apparently, the secret of their code above, is to use a typedef statement to introduce the form of the functions at the very beginning, and I was able to imitate almost exactly the same syntax to create a vector whose elements are functions.
Here is the self-contained program that I wrote to create a vector of functions:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <cstring>
#include <sstream>
#include <string>
#include <string.h>
#include <math.h>
#include <vector>
#include <map>
using namespace std;
typedef void (*FunctionWithArrayAsArgument) (double a [], double coefficient, int size);
void function1(double a [], double coefficient, int size)
{
for(int i=0; i<size; i++)
{
a[i]=coefficient*a[i]*pow(i,1);
}
}
void function2(double a [], double coefficient, int size)
{
for(int i=0; i<size; i++)
{
a[i]=coefficient*a[i]*pow(i,2);
}
}
void function3(double a [], double coefficient, int size)
{
for(int i=0; i<size; i++)
{
a[i]=coefficient*a[i]*pow(i,3);
}
}
int main()
{
int size = 5;
double Array[size];
double MyCoefficient =2;
for(int i=0; i<size; i++)
{
Array[i]=i;
}
vector<FunctionWithArrayAsArgument> Vector_Of_Functions;
Vector_Of_Functions.push_back(function1 );
Vector_Of_Functions.push_back(function2 );
Vector_Of_Functions.push_back(function3 );
cout<< "Before function calls the Array is initialized as follows:" << endl;
for (int j=0; j<size; j++) // Here "size" is the number of scalar elements of the array argument of functions,
{ // it's not the size of the vector whose elements are functions.
cout << Array[j] << endl;
}
vector<FunctionWithArrayAsArgument>::iterator end = Vector_Of_Functions.end();
for(vector<FunctionWithArrayAsArgument>::iterator itr=Vector_Of_Functions.begin(); itr != end; ++itr )
{
size_t Index_Of_Function = itr - Vector_Of_Functions.begin();
Vector_Of_Functions[Index_Of_Function](Array, MyCoefficient, size);
cout << "After the vector function number " <<Index_Of_Function <<
" is called the Array elements are as follows: " << endl;
for (int j=0; j<size; j++)
{
cout << Array[j] << endl;
}
}
}