Favor the Function Object to the Function Pointer in STL Algorithms

Favor the Function Object to the Function Pointer in STL Algorithms

Many of the STL algorithms have two versions, with one version taking one less parameter than what the second version takes. The one with lesser parameters implies its default logic by using the operator defined for the underlying data type of its argument type.

For example, if you use the sort() algorithm on a vector of strings using the version which takes two paramter then it will sort the vector alphabetically in ascending order using the operator < defined for string. But what if you want to sort it on the basis of the length of the strings? In that case, you need to use the version of sort() algorithm which takes three parameters. Pass either a function pointer or a function object as its third argument, which returns true and false according to your logic.

Both the function pointer and function object will accomplish the task, but the function object is the preferrable choice for two reasons:

  1. The invocation of a function through a function pointer is an indirect invocation which prevents the compiler from inlining said function?even if it is a good candidate for inlining. When you use the function object, if the operator() is defined inline and the compiler deems it a suitable candidate for inlining, then the compiler will inline it, thus enhancing performance.
  2. With the function object, you have the choice to keep member data which may help during the operation. It also gives flexibilty to users of intiliazing objects, allowing for different values to be used in the operation. For instance, you may define a function object which helps in finding a string whose length is greater than six or ten by just passing a number to its ctor.
Share the Post:
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

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as