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:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.