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

In daily C++ development, we often encounter the following needs:

If you are still writing by hand for loops, then is the standard library tool you should master.

The C++ standard library's The header provides a set of function templates for numerical computation; these functions can perform various numerical operations on elements in containers, such as sum, product, minimum, maximum, etc. These function templates are very powerful and can be applied to any type of container, including arrays, vectors, and lists.

When using the functions in the header, you need to include this header in your C++ program:

#include 

Common functions

Function name C++ version Core Features Simplified function prototype (using vector as an example) Typical use cases
accumulate C++98 Compute the cumulative sum of a sequence (custom binary operation allowed) int accumulate(iterator beg, iterator end, int init); Sum an array, compute cumulative values
inner_product C++98 Compute the inner product of two sequences (multiply corresponding elements then accumulate, custom operation allowed) int inner_product(iter1 b1, iter1 e1, iter2 b2, int init); Vector dot product, weighted sum
partial_sum C++98 Compute the prefix sum of a sequence (the nth result = sum of the first n elements, custom operation allowed) void partial_sum(iter b, iter e, iter res); Generate prefix-sum arrays, cumulative statistics
adjacent_difference C++98 Compute the difference between adjacent elements of a sequence (the nth result = elem[n]-elem[n-1], custom operation allowed) void adjacent_difference(iter b, iter e, iter res); Compute differences, detect sequence changes
iota C++11 Fill a sequence with consecutively increasing values (starting from init, +1 each) void iota(iter b, iter e, T init); Generate consecutive integer sequences, initialize ordered containers
reduce C++17 Parallel version of accumulate (similar to accumulate, supports parallel execution, unordered reduction) T reduce(iter b, iter e, T init = T{}); Parallel summation for large data volumes, improve computational efficiency
transform_reduce C++17 Transform + reduce (transform elements first, then accumulate, supports parallel) T transform_reduce(iter1 b1, iter1 e1, iter2 b2, T init); Parallel inner product, sum after transform
inclusive_scan C++17 Parallel prefix sum (includes current element, similar to partial_sum, supports parallel) void inclusive_scan(iter b, iter e, iter res); Generate prefix-sum arrays in parallel
exclusive_scan C++17 Parallel prefix sum (excludes current element; the nth result = sum of the first n-1 elements) void exclusive_scan(iter b, iter e, iter res, T init); Generate "excluding self" prefix sums in parallel

1. accumulate

accumulate function is used to compute the total sum of all elements in a container. It takes three parameters: the container's begin iterator, end iterator, and an initial value.

Syntax:

template 

T accumulate(Iter first, Iter last, T init);

Example:

Example

#include

#include

#include

int

main

(

)

{

    std

::

vector

<

int

v

=

{

1

,

2

,

3

,

4

,

5

}

;

   

int

sum

=

std

::

accumulate

(

v.

begin

(

)

, v.

end

(

)

,

0

)

;

    std

::

cout

<<

"Sum: "

<<

sum

<<

std

::

endl

;

// Output: Sum: 15

   

return

0

;

}

2. inner_product

inner_product function is used to compute the sum of the products of corresponding elements in two containers.

Syntax:

template 

T inner_product(Iter1 first1, Iter1 last1, Iter2 first2, T init);

Example:

Example

#include

#include

#include

int

main

(

)

{

    std

::

vector

<

int

v1

=

{

1

,

2

,

3

}

;

    std

::

vector

<

int

v2

=

{

4

,

5

,

6

}

;

   

int

product_sum

=

std

::

inner_product

(

v1.

begin

(

)

, v1.

end

(

)

, v2.

begin

(

)

,

0

)

;

    std

::

cout

<<

"Product Sum: "

<<

product_sum

<<

std

::

endl

;

// Output: Product Sum: 32

   

return

0

;

}

3. partial_sum

partial_sum function is used to compute the partial sums of elements in a container and store the results in another container.

Syntax:

template 

OutIter partial_sum(InIter first, InIter last, OutIter result);

Example:

Example

#include

#include

#include

int

main

(

)

{

    std

::

vector

<

int

v

=

{

1

,

2

,

3

,

4

}

;

    std

::

vector

<

int

result

(

v.

size

(

)

)

;

    std

::

partial_sum

(

v.

begin

(

)

, v.

end

(

)

, result.

begin

(

)

)

;

   

for

(

int

i

:

result

)

{

        std

::

cout

<<

i

<<

" "

;

// Output: 1 3 6 10

   

}

   

return

0

;

}

4. adjacent_difference

adjacent_difference function is used to compute the differences between adjacent elements in a container and store the results in another container.

Syntax:

template 

OutIter adjacent_difference(InIter first, InIter last, OutIter result);

Example:

Example

#include

#include

#include

int

main

(

)

{

    std

::

vector

<

int

v

=

{

1

,

2

,

3

,

4

}

;

    std

::

vector

<

int

result

(

v.

size

(

)

1

)

;

    std

::

adjacent_difference

(

v.

begin

(

)

, v.

end

(

)

, result.

begin

(

)

)

;

   

for

(

int

i

:

result

)

{

        std

::

cout

<<

i

<<

" "

;

// Output: 1 1 1

   

}

   

return

0

;

}

5. std::gcd

Use std::gcd to compute the greatest common divisor of two integers:

Example

#include

#include

int

main

(

)

{

   

int

a

=

48

;

   

int

b

=

18

;

   

int

result

=

std

::

gcd

(

a, b

)

;

 

// Compute the GCD of 48 and 18

    std

::

cout

<<

"GCD: "

<<

result

<<

std

::

endl

;

 

// Output 6

   

return

0

;

}

6. std::lcm

Use std::lcm to compute the least common multiple of two integers:

Example

#include

#include

int

main

(

)

{

   

int

a

=

48

;

   

int

b

=

18

;

   

int

result

=

std

::

lcm

(

a, b

)

;

 

// Compute the LCM of 48 and 18

    std

::

cout

<<

"LCM: "

<<

result

<<

std

::

endl

;

 

// Output 144

   

return

0

;

}

7. std::iota

Example

#include

#include  // Include the numeric header

#include

int

main

(

)

{

    std

::

vector

<

int

v

(

5

)

;

// Create a vector with 5 elements

   

// Use std::iota to fill the vector, starting from 1

    std

::

iota

(

std

::

begin

(

v

)

, std

::

end

(

v

)

,

1

)

;

   

// Output the filled vector

   

for

(

int

i

:

v

)

{

        std

::

cout

<<

i

<<

" "

;

   

}

    std

::

cout

<<

std

::

endl

;

// Output: 1 2 3 4 5

   

return

0

;

}

Use std::iota to fill sequence values in a range.

template

void iota(ForwardIt first, ForwardIt last, T value);

8. Find the maximum and minimum values

min_element and max_element function is used to find the maximum and minimum values in a container.

Example

#include

#include

#include

#include // To use std::min_element and std::max_element

int

main

(

)

{

   

// Define a vector containing integers

    std

::

vector

<

int

v

=

{

3

,

1

,

4

,

1

,

5

,

9

}

;

   

// Compute the minimum and maximum values

   

int

min_val

=

std

::

min_element

(

v.

begin

(

)

, v.

end

(

)

)

;

   

int

max_val

=

std

::

max_element

(

v.

begin

(

)

, v.

end

(

)

)

;

   

// Compute the sum

   

int

sum_val

=

std

::

accumulate

(

v.

begin

(

)

, v.

end

(

)

,

0

)

;

   

// Compute the average

   

double

avg_val

=

static_cast

<

double

(

sum_val

)

/

v.

size

(

)

;

   

// Output result

    std

::

cout

<<

"Min: "

<<

min_val

<<

std

::

endl

;

    std

::

cout

<<

"Max: "

<<

max_val

<<

std

::

endl

;

    std

::

cout

<<

"Sum: "

<<

sum_val

<<

std

::

endl

;

    std

::

cout

<<

"Average: "

<<

avg_val

<<

std

::

endl

;

   

return

0

;

}

The output is:

Min: 1

Max: 9

Sum: 23

Average: 3.83333