Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ standard library

The C++ standard library

The C++ standard library's The header provides a set of function templates that allow us to use function objects as parameters passed to algorithms, or as return values of algorithms. Function objects are those that overload operator() , allowing them to be called like ordinary functions.

In C++, a function object is a special class that overloads operator() to allow the object to be called like a function. This lets us pass behavior as an object, increasing code flexibility and reusability.

Syntax

To use 's functionality, you need to include this header in your C++ program:

#include 

Common function objects

The header defines some commonly used function objects, including:

Example

Use std::function

std::function is a template class that can store, invoke, and copy any callable object, such as a function, lambda expression, or function object.

Example

#include

#include

void

greet

(

)

{

    std

::

cout

<<

"Hello, World!"

<<

std

::

endl

;

}

int

main

(

)

{

    std

::

function

<

void

(

)

f

=

greet

;

// Use a function

    f

(

)

;

// Output: Hello, World!

    std

::

function

<

void

(

)

lambda

=

[

]

(

)

{

std

::

cout

<<

"Hello, Lambda!"

<<

std

::

endl

;

   

}

;

    lambda

(

)

;

// Output: Hello, Lambda!

   

return

0

;

}

Use std::bind

std::bind allows us to create a callable object that, when invoked, binds the given arguments to a function or function object.

Example

#include

#include

int

add

(

int

a,

int

b

)

{

   

return

a

b

;

}

int

main

(

)

{

   

auto

bound_add

=

std

::

bind

(

add,

5

, std

::

placeholders

::

_1

)

;

    std

::

cout

<<

bound_add

(

10

)

<<

std

::

endl

;

// Output: 15

   

return

0

;

}

In this example,std::placeholders::_1 is a placeholder that, when calling bound_add , is replaced by the actual argument.

Using comparison function objects

Comparison function objects can be used in algorithms, such as std::sort.

Example

#include

#include

#include

#include

bool

compare

(

int

a,

int

b

)

{

   

return

a

<

b

;

}

int

main

(

)

{

    std

::

vector

<

int

v

=

{

5

,

3

,

9

,

1

,

4

}

;

    std

::

sort

(

v.

begin

(

)

, v.

end

(

)

, compare

)

;

// Use a custom comparison function

   

for

(

int

i

:

v

)

{

        std

::

cout

<<

i

<<

" "

;

// Output: 1 3 4 5 9

   

}

    std

::

sort

(

v.

begin

(

)

, v.

end

(

)

, std

::

less

<

int

(

)

)

;

// Use the standard library comparison function object

   

for

(

int

i

:

v

)

{

        std

::

cout

<<

i

<<

" "

;

// Output: 1 3 4 5 9

   

}

   

return

0

;

}

header is a very powerful part of the C++ standard library; it provides rich function objects and tools, enabling us to write more flexible and reusable code. By using function objects, we can pass behavior as parameters, or store them in containers, achieving more advanced programming techniques.