Skip to content

Commit 4e3ab75

Browse files
authored
Merge pull request #5453 from Rageking8/improve-function-objects-in-the-stl-article
Improve "Function Objects in the C++ Standard Library" article
2 parents d62a01e + d709f24 commit 4e3ab75

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed
Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,67 @@
11
---
2-
description: "Learn more about: Function Objects in the C++ Standard Library"
32
title: "Function Objects in the C++ Standard Library"
4-
ms.date: "03/15/2019"
3+
description: "Learn more about: Function Objects in the C++ Standard Library"
4+
ms.date: 06/22/2025
55
helpviewer_keywords: ["functors", "C++ Standard Library, functors", "C++ Standard Library, function objects", "function objects"]
6-
ms.assetid: 85f8a735-2c7b-4f10-9c4d-95c666ec4192
76
---
87
# Function Objects in the C++ Standard Library
98

10-
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.
9+
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.
1110

12-
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.
11+
Function objects provide two main advantages over a regular 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.
1312

1413
## Creating a Function Object
1514

16-
To create a function object, create a type and implement operator(), such as:
15+
To create a function object, create a type and implement `operator()`, such as:
1716

1817
```cpp
19-
class Functor
18+
class LessThanFunctor
2019
{
2120
public:
22-
int operator()(int a, int b)
21+
bool operator()(int a, int b)
2322
{
2423
return a < b;
2524
}
2625
};
2726

2827
int main()
2928
{
30-
Functor f;
29+
LessThanFunctor less_than;
3130
int a = 5;
3231
int b = 7;
33-
int ans = f(a, b);
32+
bool ans = less_than(a, b);
3433
}
3534
```
3635

37-
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.
36+
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 `LessThanFunctor` type. This similarity between calling a function object and a function is how the term function object came about.
3837

3938
## Function Objects and Containers
4039

41-
The C++ Standard Library contains several function objects in the [`<functional>`](../standard-library/functional.md) header file. One use of these function objects is as a sorting criterion for containers. For example, the `set` container is declared as follows:
40+
The C++ Standard Library contains several function objects in the [`<functional>`](functional.md) header file. One use of these function objects is as a sorting criterion for containers. For example, the [`set`](set-class.md) container is declared as follows:
4241

4342
```cpp
4443
template <class Key,
45-
class Traits=less<Key>,
46-
class Allocator=allocator<Key>>
47-
class set
44+
class Compare = std::less<Key>,
45+
class Allocator = std::allocator<Key>>
46+
class set;
4847
```
4948
50-
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.
49+
The second template argument is the function object [`less`](less-struct.md). 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.
5150
5251
## Function Objects and Algorithms
5352
54-
Another use of functional objects is in algorithms. For example, the `remove_if` algorithm is declared as follows:
53+
Another use of function objects is in algorithms. For example, the [`remove_if`](algorithm-functions.md#remove_if) algorithm is declared as follows:
5554
5655
```cpp
57-
template <class ForwardIterator, class Predicate>
56+
template <class ForwardIterator, class UnaryPredicate>
5857
ForwardIterator remove_if(
5958
ForwardIterator first,
6059
ForwardIterator last,
61-
Predicate pred);
60+
UnaryPredicate pred);
6261
```
6362

64-
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>`](../standard-library/functional.md) header for the argument `pred` or you can create your own.
63+
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 shifted such that it's beyond the new end returned by `remove_if`. You can use any of the function objects declared in the [`<functional>`](functional.md) header for the argument `pred` or you can create your own.
6564

6665
## See also
6766

68-
[C++ Standard Library Reference](../standard-library/cpp-standard-library-reference.md)
67+
[C++ Standard Library Reference](cpp-standard-library-reference.md)

0 commit comments

Comments
 (0)