Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

lafore_robert_objectoriented_programming_in_c

.pdf
Скачиваний:
47
Добавлен:
27.03.2023
Размер:
8.94 Mб
Скачать

STL Algorithms and Member

A PPENDI X

Functions

F

 

IN THIS APPENDIX

• Algorithms 896

• Member Functions 907

• Iterators 909

Appendix F

896

This appendix contains charts showing the algorithms and container member functions available in the Standard Template Library (STL). This information is based on The Standard Template Library by Alexander Stepanov and Ming Lee (1995), but we have extensively condensed and revised it, taking many liberties with their original formulation in the interest of quick understanding.

Algorithms

Table F.1 shows the algorithms available in the STL. The descriptions in this table offer a quick and condensed explanation of what the algorithms do; they are not intended to be serious mathematical definitions. For more information, including the exact data types to use for arguments and return values, consult one of the books listed in Appendix H, “Bibliography.”

The first column gives the function name, the second explains the purpose of the algorithm, and the third specifies the arguments. Return values are not systematically specified. Some are mentioned in the Purpose column and many are either obvious or not vital to using the algorithm.

In the Arguments column, the names first, last, first1, last1, first2, last2, first3, and middle represent iterators to specific places in a container. Names with numbers (like first1) are used to distinguish multiple containers. The name first1, last1 delimits range 1, and first2, last2 delimits range 2. The arguments function, predicate, op, and comp are function objects. The arguments value, old, new, a, b, and init are values of the objects stored in a container. These values are ordered or compared based on the < or == operators or the comp function object. The argument n is an integer.

In the Purpose column, moveable iterators are indicated by iter, iter1, and iter2. When iter1 and iter2 are used together, they are assumed to move together step by step through their respective containers (or possibly two different ranges in the same container).

TABLE F.1

Algorithms

 

Name

Purpose

Arguments

 

 

 

Non-mutating Sequence Operations

for_each

Applies function to each

first, last, function

 

object.

 

find

Returns iterator to first

first, last, value

 

object equal to value.

 

find_if

Returns iterator to first

 

 

object for which predicate

first, last, predicate

 

is true.

 

 

 

 

STL Algorithms and Member Functions

 

TABLE F.1 Continued

 

 

 

 

 

 

 

 

 

 

 

 

 

Name

Purpose

Arguments

 

 

 

 

 

 

adjacent_find

Returns iterator to first

first, last

 

 

adjacent pair of objects

 

 

 

 

that are equal.

 

 

 

adjacent_find

Returns iterator to first

first, last, predicate

 

 

adjacent pair of objects

 

 

 

 

that satisfy predicate.

 

 

 

count

Adds to n the number of

first, last, value, n

 

 

objects equal to value.

 

 

 

count_if

Adds to n the number of

first, last, predicate, n

 

 

objects satisfying predicate.

 

 

 

mismatch

Returns first non-equal pair

first1, last1, first2

 

 

of corresponding objects in

 

 

 

 

two ranges.

 

 

 

mismatch

Returns first pair of

first1, last1, first2,

 

 

corresponding objects in

predicate

 

 

two ranges that don’t

 

 

 

 

satisfy predicate.

 

 

 

equal

Returns true if corresponding

first1, last1, first2

 

 

objects in two ranges are all

 

 

 

 

equal.

 

 

 

equal

Returns true if corresponding

first1, last1, first2,

 

 

objects in two ranges all

predicate

 

 

satisfy predicate.

 

 

 

search

Checks whether second range is

first1, last1, first2,

 

 

contained within the first.

last2

 

 

Returns start of match, or

 

 

 

 

last1 if no match.

 

 

 

search

Checks whether second range is

first1, last1, first2,

 

 

contained within the first,

last2, predicate

 

 

where equality is determined

 

 

 

 

by predicate. Returns start

 

 

 

 

of match, or last1 if no match.

 

 

 

 

Mutating Sequence Operations

 

copy

Copies objects from range 1

first1, last1, first2

 

 

to range 2.

 

 

897

F

F AND STL

UNCTIONS A M LGORITHMS EMBER

898

Appendix F

 

TABLE F.1 Continued

Name

Purpose

Arguments

copy_backward

Copies objects from range 1

first1, last1, first2

 

to range 2, inserting them

 

 

backwards, from last2 to

 

 

first2.

 

swap

Interchanges two objects.

a, b

iter_swap

Interchanges objects pointed

iter1, iter2

 

to by two iterators.

 

swap_ranges

Interchanges corresponding

first1, last1, first2

 

elements in two ranges.

 

transform

Transforms objects in range

first1, last1, first2,

 

1 into new objects in range

operator

 

2 by applying operator.

 

transform

Combines objects in range 1

first1, last1, first2,

 

and range 2 into new objects

first3, operator

 

in range 3 by applying

 

 

operator.

 

replace

Replaces all objects equal

first, last, old, new

 

to old with objects equal

 

 

to new.

 

replace_if

Replaces all objects that

first, last, predicate,

 

satisfy predicate with

new

 

objects equal to new.

 

replace_copy

Copies from range 1 to

first1, last1, first2,

 

range 2, replacing all

old, new

 

objects equal to old with

 

 

objects equal to new.

 

replace_copy_if

Copies from range 1 to range

first1, last1, first2,

 

2, replacing all objects that

predicate, new

 

satisfy predicate with

 

 

objects equal to new.

 

fill

Assigns value to all

first, last, value

 

objects in range.

 

fill_n

Assigns value to all

first, n, value

 

objects from first to

 

 

first+n.

 

 

 

 

STL Algorithms and Member Functions

 

TABLE F.1 Continued

 

 

 

 

 

 

 

 

 

 

 

 

 

Name

Purpose

Arguments

 

 

 

 

 

 

generate

Fills range with values

first, last, gen

 

 

generated by successive

 

 

 

 

calls to function gen.

 

 

 

generate_n

Fills from first to first+n

first, n, gen

 

 

with values generated by

 

 

 

 

successive calls to

 

 

 

 

function gen.

 

 

 

remove

Removes from range any

first, last, value

 

 

objects equal to value.

 

 

 

remove_if

Removes from range any

first, last, predicate

 

 

objects that satisfy

 

 

 

 

predicate.

 

 

 

remove_copy

Copies objects, excepting

first1, last1, first2,

 

 

those equal to value,

value

 

 

from range 1 to range 2.

 

 

 

remove_copy_if

Copies objects, excepting

first1, last1, first2,

 

 

those satisfying pred,

pred

 

 

from range 1 to range 2.

 

 

 

unique

Eliminates all but the first

first, last

 

 

object from any consecutive

 

 

 

 

sequence of equal objects.

 

 

 

unique

Eliminates all but the first

first, last, predicate

 

 

object from any consecutive

 

 

 

 

sequence of objects satisfying

 

 

 

 

predicate.

 

 

 

unique_copy

Copies objects from range 1

first1, last1, first2

 

 

to range 2, except only the

 

 

 

 

first object from any

 

 

 

 

consecutive sequence of

 

 

 

 

equal objects is copied.

 

 

 

unique_copy

Copies objects from range 1

first1, last1, first2,

 

 

to range 2, except only the

predicate

 

 

first object from any

 

 

 

 

consecutive sequence of

 

 

 

 

objects satisfying

 

 

 

 

predicate is copied.

 

 

899

F

F AND STL

UNCTIONS A M LGORITHMS EMBER

Appendix F

900

TABLE F.1 Continued

Name

Purpose

Arguments

reverse

reverse_copy

Reverses the sequence of

first, last

objects in range.

 

Copies range 1 to range 2,

first1, last1, first2

reversing the sequence of

 

objects.

 

rotate

Rotates sequence of

first, last, middle

 

objects around iterator

 

 

middle.

 

rotate_copy

Copies objects from range 1

first1, middle1, last1,

 

to range 2, rotating the

first2

 

sequence around iterator

 

 

middle1.

 

random_shuffle

Randomly shuffles objects

first, last

 

in range.

 

random_shuffle

Randomly shuffles objects

first, last, rand

 

in range, using random-

 

 

number function rand.

 

partition

Moves all objects that

first, last, predicate

 

satisfy predicate so they

 

 

precede those that do not

 

 

satisfy it.

 

stable_partition

Moves all objects that

first, last, predicate

 

satisfy predicate so

 

 

they precede those that do

 

 

not, and also preserves

 

 

relative ordering in the

 

 

two groups.

 

 

Sorting and Related Operations

 

sort

Sorts objects in range.

first, last

sort

Sorts elements in range, using

first, last, comp

 

comp as comparison function.

 

stable_sort

Sorts objects in range,

first, last

maintains order of equal

 

elements.

 

stable_sort

Sorts elements in range,

first, last, comp

 

using comp as comparison

 

 

function, maintains order of

 

 

equal elements.

 

 

 

 

STL Algorithms and Member Functions

 

TABLE F.1 Continued

 

 

 

 

 

 

 

 

 

 

 

 

 

Name

Purpose

Arguments

 

 

 

 

 

 

partial_sort

Sorts all objects in range,

first, middle, last

 

 

places as many sorted values

 

 

 

 

as will fit between first

 

 

 

 

and middle. Order of objects

 

 

 

 

between middle and last is

 

 

 

 

undefined.

 

 

 

partial_sort

Sorts all objects in range,

first, middle, last,

 

 

places as many sorted values

predicate

 

 

as will fit between first

 

 

 

 

and middle. Order of objects

 

 

 

 

between middle and last is

 

 

 

 

undefined. Uses predicate

 

 

 

 

to define ordering.

 

 

 

partial_sort_copy Same as partial_sort (first,

first1, last1, first2,

 

 

middle, last), but places

last2

 

 

resulting sequence in

 

 

 

 

range 2.

 

 

 

partial_sort_copy Same as partial_sort (first,

first1, last1, first2,

 

 

middle, last, predicate),

last2, comp

 

 

but places resulting

 

 

 

 

sequence in range 2.

 

 

 

nth_element

Places the nth object in

first, nth, last

 

 

the position it would

 

 

 

 

occupy if the whole range

 

 

 

 

were sorted.

 

 

 

nth_element

Places the nth object in the

first, nth, last, comp

 

 

position it would occupy if

 

 

 

 

the whole range were sorted

 

 

 

 

using comp for comparisons.

 

 

 

lower_bound

Returns iterator to first

first, last, value

 

 

position into which value

 

 

 

 

could be inserted without

 

 

 

 

violating the ordering.

 

 

 

lower_bound

Returns iterator to first

first, last, value, comp

 

 

position into which value

 

 

 

 

could be inserted without

 

 

 

 

violating an ordering based

 

 

 

 

on comp.

 

 

901

F

F AND STL

UNCTIONS A M LGORITHMS EMBER

902

Appendix F

 

TABLE F.1 Continued

Name

Purpose

Arguments

upper_bound

Returns iterator to last

first, last, value

 

position into which value

 

 

could be inserted without

 

 

violating the ordering.

 

upper_bound

Returns iterator to last

first, last, value, comp

 

position into which value

 

 

could be inserted without

 

 

violating an ordering based

 

 

on comp.

 

equal_range

Returns a pair containing the

first, last, value

 

lower bound and upper bound

 

 

between which value could

 

 

be inserted without violating

 

 

the ordering.

 

equal_range

Returns a pair containing

first, last, value, comp

 

the lower bound and upper

 

 

bound between which value

 

 

could be inserted without

 

 

violating an ordering based

 

 

on comp.

 

binary_search

Returns true if value is in

first, last, value

 

the range.

 

binary_search

Returns true if value is

first, last, value, comp

 

in the range, where the

 

 

ordering is determined by

 

 

comp.

 

merge

Merges sorted ranges 1 and

first1, last1, first2,

 

2 into sorted range 3.

last2, first3

merge

Merges sorted ranges 1 and 2

first1, last1, first2,

 

into sorted range 3, where

last2, first3, comp

 

the ordering is determined

 

 

by comp.

 

inplace_merge

Merges two consecutive

first, middle, last

 

sorted ranges, first,

 

 

middle and middle, last

 

 

into first, last.

 

 

 

 

STL Algorithms and Member Functions

 

TABLE F.1 Continued

 

 

 

 

 

 

 

 

 

 

 

 

 

Name

Purpose

Arguments

 

 

 

 

 

 

inplace_merge

Merges two consecutive

first, middle, last, comp

 

 

sorted ranges, first,

 

 

 

 

middle and middle, last

 

 

 

 

into first, last, where the

 

 

 

 

ordering is based on comp.

 

 

 

includes

Returns true if every object

first1, last1, first2,

 

 

in the range first2, last2

last2

 

 

is also in the range first1,

 

 

 

 

last1. (Sets and multisets

 

 

 

 

only.)

 

 

 

includes

Returns true if every object

first1, last1, first2,

 

 

in the range first2, last2

last2, comp

 

 

is also in the range first1,

 

 

 

 

last1, where ordering is

 

 

 

 

based on comp. (Sets and

 

 

 

 

multisets only.)

 

 

 

set_union

Constructs sorted union of

first1, last1, first2,

 

 

elements of ranges 1 and 2.

last2, first3

 

 

(Sets and multisets only.)

 

 

 

set_union

Constructs sorted union of

first1, last1, first2,

 

 

elements of ranges 1 and 2,

last2, first3, comp

 

 

where the ordering is based

 

 

 

 

on comp. (Sets and

 

 

 

 

multisets only.)

 

 

 

set_intersection

Constructs sorted

first1, last1, first2,

 

 

intersection of elements

last2, first3

 

 

of ranges 1 and 2. (Sets

 

 

 

 

and multisets only.)

 

 

 

set_intersection

Constructs sorted

first1, last1, first2,

 

 

intersection of elements

last2, first3, comp

 

 

of ranges 1 and 2, where

 

 

 

 

the ordering is based on

 

 

 

 

comp. (Sets and

 

 

 

 

multisets only.)

 

 

 

set_difference

Constructs sorted difference

first1, last1, first2,

 

 

of elements of ranges 1 and

last2, first3

 

 

2. (Sets and multisets only.)

 

 

903

F

F AND STL

UNCTIONS A M LGORITHMS EMBER

904

Appendix F

 

TABLE F.1 Continued

Name

Purpose

Arguments

set_difference

Constructs sorted difference

first1, last1, first2,

 

of elements of ranges 1 and

last2, first3, comp

 

2, where the ordering is

 

 

based on comp. (Sets and

 

 

multisets only.)

 

set_symmetric_ difference

Constructs sorted symmetric

first1, last1, first2,

difference of elements of

last2, first3

ranges 1 and 2. (Sets and

 

multisets only.)

 

set_ symmetric_

Constructs sorted difference

first1, last1, first2,

difference

of elements of ranges 1 and

last2, first3, comp

 

2, where the ordering is

 

 

based on comp. (Sets and

 

 

multisets only.)

 

push_heap

Places value from last-1 into

first, last

 

resulting heap in range first,

 

 

last.

 

push_heap

Places value from last-1

first, last, comp

 

into resulting heap in range

 

 

first, last, based on

 

 

ordering determined by comp.

 

pop_heap

Swaps the values in first

first, last

 

and last-1; makes range

 

 

first, last-1 into a heap.

 

pop_heap

Swaps the values in first

first, last, comp

 

and last-1; makes range

 

 

first, last-1 into a heap,

 

 

based on ordering

 

 

determined by comp.

 

make_heap

Constructs a heap out of the

first, last

 

range first, last.

 

make_heap

Constructs a heap out of the

first, last, comp

 

range first, last, based on

 

 

the ordering determined by

 

 

comp.

 

sort_heap

Sorts the elements in the

first, last

 

heap first, last.