From 08ebd7d6723623f48bd5285047207e4d3490f3d3 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Sun, 22 Jun 2025 18:23:06 +0800 Subject: [PATCH 1/7] Add backticks in "Function Objects in the C++ Standard Library" article --- docs/standard-library/function-objects-in-the-stl.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/standard-library/function-objects-in-the-stl.md b/docs/standard-library/function-objects-in-the-stl.md index e1055bf6f6..b69161ed48 100644 --- a/docs/standard-library/function-objects-in-the-stl.md +++ b/docs/standard-library/function-objects-in-the-stl.md @@ -7,13 +7,13 @@ ms.assetid: 85f8a735-2c7b-4f10-9c4d-95c666ec4192 --- # 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. +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: +To create a function object, create a type and implement `operator()`, such as: ```cpp class Functor @@ -34,7 +34,7 @@ int main() } ``` -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. +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 From f9fb8ef6890ef3674d7f977dae12ae73977414bd Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Sun, 22 Jun 2025 18:50:17 +0800 Subject: [PATCH 2/7] Tighten wording in "Function Objects in the C++ Standard Library" article --- docs/standard-library/function-objects-in-the-stl.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/standard-library/function-objects-in-the-stl.md b/docs/standard-library/function-objects-in-the-stl.md index b69161ed48..d92c8e6556 100644 --- a/docs/standard-library/function-objects-in-the-stl.md +++ b/docs/standard-library/function-objects-in-the-stl.md @@ -9,7 +9,7 @@ ms.assetid: 85f8a735-2c7b-4f10-9c4d-95c666ec4192 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. +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. ## Creating a Function Object @@ -51,7 +51,7 @@ The second template argument is the function object `less`. This function object ## Function Objects and Algorithms -Another use of functional objects is in algorithms. For example, the `remove_if` algorithm is declared as follows: +Another use of function objects is in algorithms. For example, the `remove_if` algorithm is declared as follows: ```cpp template @@ -61,7 +61,7 @@ ForwardIterator remove_if( 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 [``](../standard-library/functional.md) header for the argument `pred` or you can create your own. +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 [``](../standard-library/functional.md) header for the argument `pred` or you can create your own. ## See also From ef934d9568129bc66b1b4d24c5c5e32b6523f544 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Sun, 22 Jun 2025 18:53:23 +0800 Subject: [PATCH 3/7] Trim relative links in "Function Objects in the C++ Standard Library" article --- docs/standard-library/function-objects-in-the-stl.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/standard-library/function-objects-in-the-stl.md b/docs/standard-library/function-objects-in-the-stl.md index d92c8e6556..7fa8c3ac9f 100644 --- a/docs/standard-library/function-objects-in-the-stl.md +++ b/docs/standard-library/function-objects-in-the-stl.md @@ -38,7 +38,7 @@ The last line of the `main` function shows how you call the function object. Thi ## Function Objects and Containers -The C++ Standard Library contains several function objects in the [``](../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: +The C++ Standard Library contains several function objects in the [``](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: ```cpp template `](../standard-library/functional.md) header for the argument `pred` or you can create your own. +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.md) header for the argument `pred` or you can create your own. ## See also -[C++ Standard Library Reference](../standard-library/cpp-standard-library-reference.md) +[C++ Standard Library Reference](cpp-standard-library-reference.md) From ce4459dc19f9162a73e93cf5a01c86133187cc77 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Sun, 22 Jun 2025 18:57:06 +0800 Subject: [PATCH 4/7] Add links in "Function Objects in the C++ Standard Library" article --- docs/standard-library/function-objects-in-the-stl.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/standard-library/function-objects-in-the-stl.md b/docs/standard-library/function-objects-in-the-stl.md index 7fa8c3ac9f..e03f0c28c8 100644 --- a/docs/standard-library/function-objects-in-the-stl.md +++ b/docs/standard-library/function-objects-in-the-stl.md @@ -38,7 +38,7 @@ The last line of the `main` function shows how you call the function object. Thi ## Function Objects and Containers -The C++ Standard Library contains several function objects in the [``](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: +The C++ Standard Library contains several function objects in the [``](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: ```cpp template From f8ed85fa8441070724203316849278b6c24fd8c5 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Sun, 22 Jun 2025 19:03:20 +0800 Subject: [PATCH 5/7] Improve example in "Function Objects in the C++ Standard Library" article --- docs/standard-library/function-objects-in-the-stl.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/standard-library/function-objects-in-the-stl.md b/docs/standard-library/function-objects-in-the-stl.md index e03f0c28c8..446cc5f233 100644 --- a/docs/standard-library/function-objects-in-the-stl.md +++ b/docs/standard-library/function-objects-in-the-stl.md @@ -16,10 +16,10 @@ Function objects provide two main advantages over a regular function call. The f To create a function object, create a type and implement `operator()`, such as: ```cpp -class Functor +class LessThanFunctor { public: - int operator()(int a, int b) + bool operator()(int a, int b) { return a < b; } @@ -27,14 +27,14 @@ public: int main() { - Functor f; + LessThanFunctor less_than; int a = 5; int b = 7; - int ans = f(a, b); + bool ans = less_than(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. +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. ## Function Objects and Containers From 9e2c8ec8e44aea055ebc9581ee20589a36a316b9 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Sun, 22 Jun 2025 19:08:30 +0800 Subject: [PATCH 6/7] Update `set` and `remove_if` syntax in "Function Objects in the C++ Standard Library" article --- docs/standard-library/function-objects-in-the-stl.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/standard-library/function-objects-in-the-stl.md b/docs/standard-library/function-objects-in-the-stl.md index 446cc5f233..325b48aae0 100644 --- a/docs/standard-library/function-objects-in-the-stl.md +++ b/docs/standard-library/function-objects-in-the-stl.md @@ -42,9 +42,9 @@ The C++ Standard Library contains several function objects in the [` ```cpp template , - class Allocator=allocator> -class set + class Compare = std::less, + class Allocator = std::allocator> +class set; ``` 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. @@ -54,11 +54,11 @@ The second template argument is the function object [`less`](less-struct.md). Th Another use of function objects is in algorithms. For example, the [`remove_if`](algorithm-functions.md#remove_if) algorithm is declared as follows: ```cpp -template +template ForwardIterator remove_if( ForwardIterator first, ForwardIterator last, - Predicate pred); + UnaryPredicate 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 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.md) header for the argument `pred` or you can create your own. From d709f242e836a1bde6ada34f029522ae7e704ad8 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Sun, 22 Jun 2025 19:09:38 +0800 Subject: [PATCH 7/7] Update metadata in "Function Objects in the C++ Standard Library" article --- docs/standard-library/function-objects-in-the-stl.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/standard-library/function-objects-in-the-stl.md b/docs/standard-library/function-objects-in-the-stl.md index 325b48aae0..4289ec032e 100644 --- a/docs/standard-library/function-objects-in-the-stl.md +++ b/docs/standard-library/function-objects-in-the-stl.md @@ -1,9 +1,8 @@ --- -description: "Learn more about: Function Objects in the C++ Standard Library" title: "Function Objects in the C++ Standard Library" -ms.date: "03/15/2019" +description: "Learn more about: Function Objects in the C++ Standard Library" +ms.date: 06/22/2025 helpviewer_keywords: ["functors", "C++ Standard Library, functors", "C++ Standard Library, function objects", "function objects"] -ms.assetid: 85f8a735-2c7b-4f10-9c4d-95c666ec4192 --- # Function Objects in the C++ Standard Library