A Container for Pointers to Functions

A Container for Pointers to Functions

Many times, you’ll find you need to create a container for pointers to functions. The example below demonstrates the syntax for a vector of pointers to functions. For simplicity’s sake, only one function, is_positive(double), has been defined. Notice how the vector of pointers to functions is declared, how the next pointer is added to the vector, and how an element of the vector is invoked using an iterator. The benefit of this technique is twofold: utilizing the power of the STL containers and the flexibility of pointers to functions.

bool is_positive(double a){ return a > 0.0;}#include #include int main(){    std::vector fp;// declaration    for( long i = 0; i < 10; ++i )        fp.push_back(&is_positive);// adding    std::vector::iterator it;    double a = -4.0;    for( it = fp.begin(); it != fp.end(); ++it )             std::cout <<(*it)(a++)<< " ";//invocation    std::cout << std::endl;    return 0;}// the program output is "0 0 0 0 0 1 1 1 1 1"
Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing