Function Objects in the C++ Standard Library (2024)

  • Article

A function object, or functor, is any type that implements operator(). This operator is referred to as the call operator or sometimes the application operator. The C++ Standard Library uses function objects primarily as sorting criteria for containers and in algorithms.

Function objects provide two main advantages over a straight function call. The first is that a function object can contain state. The second is that a function object is a type and therefore can be used as a template parameter.

Creating a Function Object

To create a function object, create a type and implement operator(), such as:

class Functor{public: int operator()(int a, int b) { return a < b; }};int main(){ Functor f; int a = 5; int b = 7; int ans = f(a, b);}

The last line of the main function shows how you call the function object. This call looks like a call to a function, but it's actually calling operator() of the Functor type. This similarity between calling a function object and a function is how the term function object came about.

Function Objects and Containers

The C++ Standard Library contains several function objects in the <functional> header file. One use of these function objects is as a sorting criterion for containers. For example, the set container is declared as follows:

template <class Key, class Traits=less<Key>, class Allocator=allocator<Key>>class set

The second template argument is the function object less. This function object returns true if the first parameter is less than the second parameter. Since some containers sort their elements, the container needs a way of comparing two elements. The comparison is done by using the function object. You can define your own sorting criteria for containers by creating a function object and specifying it in the template list for the container.

Function Objects and Algorithms

Another use of functional objects is in algorithms. For example, the remove_if algorithm is declared as follows:

template <class ForwardIterator, class Predicate>ForwardIterator remove_if( ForwardIterator first, ForwardIterator last, Predicate pred);

The last argument to remove_if is a function object that returns a boolean value (a predicate). If the result of the function object is true, then the element is removed from the container being accessed by the iterators first and last. You can use any of the function objects declared in the <functional> header for the argument pred or you can create your own.

See also

C++ Standard Library Reference

Function Objects in the C++ Standard Library (2024)

FAQs

Function Objects in the C++ Standard Library? ›

A function object, or functor, is any type that implements operator(). This operator is referred to as the call operator or sometimes the application operator. The C++ Standard Library uses function objects primarily as sorting criteria for containers and in algorithms.

What functions are in C++ Standard Library? ›

  • C++ atof() Converts String to Double.
  • C++ strtol() Converts a string to number.
  • C++ strtoull() converts string to unsigned long long int.
  • C++ malloc() Allocates a block of unitialized memory.
  • C++ realloc() reallocates a block of previously allocated memory.
  • C++ srand() ...
  • C++ free() ...
  • C++ getenv()

Are C++ functions objects? ›

A function object is any object for which the function call operator is defined. C++ provides many built-in function objects as well as support for creation and manipulation of new function objects.

What are the components of the standard library in C++? ›

The C++ Standard Template Library (STL) is a collection of algorithms, data structures, and other components that can be used to simplify the development of C++ programs. The STL provides a range of containers, such as vectors, lists, and maps, as well as algorithms for searching, sorting and manipulating data.

How to create an object of a function in C++? ›

Create an Object

In C++, an object is created from a class. We have already created the class named MyClass , so now we can use this to create objects. To create an object of MyClass , specify the class name, followed by the object name.

What is the main function in standard C++? ›

The main() function

Every program must have one function named main , and the following constraints apply: No other function in the program can be called main . main cannot be defined as inline or static . main cannot be called from within a program.

What does a C++ functions consist of? ›

A C++ function consist of two parts: Declaration: the return type, the name of the function, and parameters (if any) Definition: the body of the function (code to be executed)

Does C++ have built in functions? ›

In-built functions in C++ are those functions that are part of C++ standard libraries. The purpose of inbuilt functions is to provide common and essential functionality that is frequently required in the programming.

What are considered objects in C++? ›

In C++, an object is an instance of a class that encapsulates data and functionality pertaining to that data. Suppose a class named MyClass was created, so now it can be used to create objects.

What is included in standard library? ›

Standard libraries typically include definitions for commonly used algorithms, data structures, and mechanisms for input and output. Depending on the constructs made available by the host language, a standard library may include: Subroutines. Macro definitions.

What does a C++ library contain? ›

The Standard C++ library provides an extensible framework and contains components for language support, diagnostics, general utilities, strings, locales, standard template library (containers, iterators, algorithms, and numerics), and input/output.

What are different C++ standard libraries? ›

The Standard C++ Library can be categorized as follows: The Language Support Library. The Diagnostics Library. The General Utilities Library. The Standard String Templates.

Are functions objects in C++? ›

A function object, or functor, is any type that implements operator(). This operator is referred to as the call operator or sometimes the application operator. The C++ Standard Library uses function objects primarily as sorting criteria for containers and in algorithms.

What is the difference between function pointer and function object? ›

A function pointer allows a pointer to a function to be passed as a parameter to another function. Function Objects (Functors) - C++ allows the function call operator() to be overloaded, such that an object instantiated from a class can be "called" like a function.

How do you create an object of a function? ›

You can use Object.create() to mimic the behavior of the new operator. function Constructor() {} o = new Constructor(); // Is equivalent to: o = Object.create(Constructor.prototype); Of course, if there is actual initialization code in the Constructor function, the Object.create() method cannot reflect it.

What is the function of the STL? ›

The STL provides a set of common classes for C++, such as containers and associative arrays, that can be used with any built-in type and with any user-defined type that supports some elementary operations (such as copying and assignment).

What are the inbuilt functions in C++? ›

List of top 10 inbuilt functions in C++
  • pow()
  • sqrt()
  • min()
  • max()
  • swap()
  • gcd()
  • toupper()
  • tolower()
Apr 18, 2023

What are the components of stl in C++? ›

Components of the STL
  • Containers - Data structures designed to efficiently store information across a variety of generic types.
  • Iterators - Methods by which to traverse/access the data within the containers.
  • Algorithms - Basic algorithmic capabilities, such as sort, find, max, min, replace.

Top Articles
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6005

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.