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

lafore_robert_objectoriented_programming_in_c

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

 

 

 

STL Algorithms and Member Functions

 

TABLE F.1 Continued

 

 

 

 

 

 

 

 

 

 

 

 

 

Name

Purpose

Arguments

 

 

 

 

 

 

sort_heap

Sorts the elements in the

first, last, comp

 

 

heap first, last, based on

 

 

 

 

the ordering determined by

 

 

 

 

comp.

 

 

 

min

Returns the smaller of two

a, b

 

 

objects.

 

 

 

min

Returns the smaller of two

a, b, comp

 

 

objects, where the ordering

 

 

 

 

is determined by comp.

 

 

 

max

Returns the larger of two

a, b

 

 

objects.

 

 

 

max

Returns the larger of two

a, b, comp

 

 

objects, where the ordering

 

 

 

 

is determined by comp.

 

 

 

max_element

Returns an iterator to the

first, last

 

 

largest object in the range.

 

 

 

max_element

Returns an iterator to the

first, last, comp

 

 

largest object in the

 

 

 

 

range, with an ordering

 

 

 

 

determined by comp.

 

 

 

min_element

Returns an iterator to the

first, last

 

 

smallest object in the

 

 

 

 

range.

 

 

 

min_element

Returns an iterator to

first, last, comp

 

 

the smallest object in

 

 

 

 

the range, with an

 

 

 

 

ordering determined by

 

 

 

 

comp.

 

 

 

lexicographical_

Returns true if the

first1, last1, first2,

 

compare

sequence in range 1 comes

last2

 

 

before the sequence in

 

 

 

 

range 2 alphabetically.

 

 

 

lexicographical_

Returns true if the

first1, last1, first2,

 

compare

sequence in range 1 comes

last2, comp

 

 

before the sequence in

 

 

 

 

range 2 alphabetically,

 

 

 

 

based on ordering

 

 

 

 

determined by comp.

 

 

905

F

F AND STL

UNCTIONS A M LGORITHMS EMBER

906

Appendix F

 

TABLE F.1 Continued

Name

Purpose

Arguments

next_permutation

Performs one permutation

first, last

 

on the sequence in the

 

 

range.

 

next_permutation

Performs one permutation

first, last, comp

 

on the sequence in the

 

 

range, where the ordering

 

 

is determined by comp.

 

prev_permutation

Performs one reverse

first, last

 

permutation on the

 

 

sequence in the range.

 

prev_permutation

Performs one reverse

first, last, comp

 

permutation on the

 

 

sequence in the range,

 

 

where the ordering is

 

 

determined by comp.

 

 

Generalized Numeric Operations

accumulate

Sequentially applies

first, last, init

 

init = init + *iter

 

 

to each object

 

 

in the range.

 

accumulate

Sequentially applies

first, last, init,

 

init = op(init, *iter)

op

 

to each object

 

 

in the range.

 

inner_product

Sequentially applies

first1, last1,

 

init=init+(*iter1)*(*iter2)

first2, init

 

to corresponding values

 

 

from ranges 1 and 2.

 

inner_product

Sequentially applies

first1, last1,

 

init=op1(init,op2(*iter1,*iter2)) first2, init, op1,

 

to corresponding values

op2

 

from ranges 1 and 2.

 

partial_sum

Adds values from start of

first1, last1,

range 1 to current iterator,

first2

and places the sums in

 

corresponding iterator in

 

range 2.

 

*iter2 = sum(*first1, *(first1+1),

*(first1+2), ..*iter1)

 

 

 

STL Algorithms and Member Functions

 

TABLE F.1 Continued

 

 

 

 

 

 

 

 

 

 

 

 

 

Name

Purpose

Arguments

 

 

 

 

 

 

partial_sum

Sequentially applies op to

first1, last1,

 

 

objects between first1 and

first2, op

 

 

current iterator in range 1,

 

 

 

 

and places results in

 

 

 

 

corresponding iterator in

 

 

 

 

range 2.

 

 

 

 

answer = *first;

 

 

 

 

for(iter=first+1; iter != iter1; iter++)

 

 

op(answer, *iter);

 

 

 

 

*iter2 = answer;

 

 

 

adjacent_

Subtracts adjacent

first1, last1,

 

difference

objects in range 1 and

first2

 

 

places differences in

 

 

 

 

range 2.

 

 

 

 

*iter2 = * (iter1+1) - *iter1;

 

adjacent_

Sequentially applies

first1, last1,

 

difference

op to adjacent objects

first2, op

 

 

in range 1 and places

 

 

 

 

results in range 2.

 

 

*iter2 = op(*(iter1+1),*iter1);

Member Functions

The same names are used for member functions that have similar purposes in the different containers. However, no container class includes all the available member functions. Table F.2 is intended to show which member functions are available for each container. Explanations of the functions are not given, either because they are more or less self-evident, or because they are explained in the text.

907

F

F AND STL

UNCTIONS A M LGORITHMS EMBER

908

Appendix F

 

TABLE F.2

Member Functions

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Multi-

 

Multi-

 

 

Priority

 

 

Vector

List

Deque

Set

set

Map

map

Stack

Queue

Queue

 

 

 

 

 

 

 

 

 

 

 

operator==

x

x

x

x

x

x

x

x

x

 

operator!=

x

x

x

x

x

x

x

x

x

 

operator<

x

x

x

x

x

x

x

x

x

 

operator>

x

x

x

x

x

x

x

x

x

 

operator<=

x

x

x

x

x

x

x

x

x

 

operator>=

x

x

x

x

x

x

x

x

x

 

operator

=

x

x

x

 

 

 

 

 

 

 

operator[]

x

 

x

 

 

x

 

 

 

 

operator*

 

x

x

 

 

 

 

 

 

 

operator->

 

x

x

 

 

 

 

 

 

 

operator

()

 

 

 

x

x

x

 

 

 

 

operator

+

 

 

x

 

 

 

 

 

 

 

operator

-

 

 

x

 

 

 

 

 

 

 

operator++

 

x

x

 

 

 

 

 

 

 

operator--

 

x

x

 

 

 

 

 

 

 

operator

+=

 

 

x

 

 

 

 

 

 

 

operator

-=

 

 

x

 

 

 

 

 

 

 

begin

 

x

x

x

x

x

x

x

 

 

 

end

 

x

x

x

x

x

x

x

 

 

 

rbegin

 

x

x

x

x

x

x

x

 

 

 

rend

 

x

x

x

x

x

x

x

 

 

 

empty

 

x

x

x

x

x

x

x

x

x

x

size

 

x

x

x

x

x

x

x

x

x

x

max_size

 

x

x

x

x

x

x

x

 

 

 

front

 

x

x

x

 

 

 

 

 

x

 

back

 

x

x

x

 

 

 

 

 

x

 

push_front

 

x

x

 

 

 

 

 

 

 

push_back

x

x

x

 

 

 

 

 

 

 

pop_front

 

x

x

 

 

 

 

 

 

 

pop_back

 

x

x

x

 

 

 

 

 

 

 

swap

 

x

x

x

x

x

x

x

 

 

 

STL Algorithms and Member Functions

TABLE F.2 Continued

Multi-

Multi-

Priority

Vector List Deque Set set

Map map

Stack Queue Queue

insert

x

x

x

x

x

x

x

 

 

erase

x

x

x

x

x

x

x

 

 

find

 

 

 

x

x

x

x

 

 

count

 

 

 

x

x

x

x

 

 

lower_bound

 

 

 

x

x

x

x

 

 

upper_bound

 

 

 

x

x

x

x

 

 

equal_range

 

 

 

x

x

x

x

 

 

top

 

 

 

 

 

 

x

 

x

push

 

 

 

 

 

 

x

x

x

pop

 

 

 

 

 

 

x

x

x

capacity

x

 

 

 

 

 

 

 

 

reserve

x

 

 

 

 

 

 

 

 

splice

 

x

 

 

 

 

 

 

 

remove

 

x

 

 

 

 

 

 

 

unique

 

x

 

 

 

 

 

 

 

merge

 

x

 

 

 

 

 

 

 

reverse

 

x

 

 

 

 

 

 

 

sort

 

x

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Iterators

Table F.3 lists the type of iterator required by each algorithm.

TABLE F.3 Type of Iterator Required by Algorithm

 

 

 

 

 

Random

 

Input

Output

Forward

Bidirectional

Access

 

 

 

 

 

 

for_each

x

 

 

 

 

find

x

 

 

 

 

find_if

x

 

 

 

 

adjacent_find

x

 

 

 

 

count

x

 

 

 

 

909

F

F AND STL

UNCTIONS A M LGORITHMS EMBER

910

Appendix F

 

TABLE F.3 Continued

 

 

 

 

 

Random

 

Input

Output

Forward

Bidirectional

Access

 

 

 

 

 

 

count_if

x

 

 

 

 

mismatch

x

 

 

 

 

equal

x

 

 

 

 

search

 

 

x

 

 

copy

x

x

 

 

 

copy_backward

x

x

 

 

 

iter_swap

 

 

x

 

 

swap_ranges

 

 

x

 

 

transform

x

x

 

 

 

replace

 

 

x

 

 

replace_if

 

 

x

 

 

replace_copy

x

x

 

 

 

fill

 

 

x

 

 

fill_n

 

x

 

 

 

generate

 

 

x

 

 

generate_n

 

x

 

 

 

remove

 

 

x

 

 

remove_if

 

 

x

 

 

remove_copy

x

x

 

 

 

remove_copy_if

x

x

 

 

 

unique

 

 

x

 

 

unique_copy

x

x

 

 

 

reverse

 

 

 

x

 

reverse_copy

 

x

 

 

 

rotate

 

 

x

 

 

rotate_copy

 

x

x

 

 

random_shuffle

 

 

 

 

x

partition

 

 

 

x

 

stable_partition

 

 

 

x

 

sort

 

 

 

 

x

stable_sort

 

 

 

 

x

STL Algorithms and Member Functions

TABLE F.3 Continued

 

 

 

 

 

Random

 

Input

Output

Forward

Bidirectional

Access

 

 

 

 

 

 

partial_sort

 

 

 

 

x

partial_sort_copy

x

 

 

 

x

nth_element

 

 

 

 

x

lower_bound

 

 

x

 

 

upper_bound

 

 

x

 

 

equal_range

 

 

x

 

 

binary_search

 

 

x

 

 

merge

x

x

 

 

 

inplace_merge

 

 

 

x

 

includes

x

 

 

 

 

set_union

x

x

 

 

 

set_intersection

x

x

 

 

 

set_difference

x

x

 

 

 

set_symmetric_

 

 

 

 

 

difference x

x

 

 

 

 

push_heap

 

 

 

 

x

pop_heap

 

 

 

 

x

make_heap

 

 

 

 

x

sort_heap

 

 

 

 

x

max_element

x

 

 

 

 

min_element

x

 

 

 

 

lexicographical_

 

 

 

 

 

comparison

x

 

 

 

 

next_permutation

 

 

 

x

 

prev_permutation

 

 

 

x

 

accumulate

x

 

 

 

 

inner_product

x

 

 

 

 

partial_sum

x

x

 

 

 

adjacent_

 

 

 

 

 

difference

x

x

 

 

 

 

 

 

 

 

 

911

F

F AND STL

UNCTIONS A M LGORITHMS EMBER

Answers to Questions and Exercises

IN THIS APPENDIX

Chapter 1

914

Chapter 2

914

Chapter 3

917

Chapter 4

921

Chapter 5

924

Chapter 6

928

Chapter 7

932

Chapter 8

937

Chapter 9

943

Chapter 10

949

Chapter 11

954

Chapter 12

960

Chapter 13

963

Chapter 14

964

Chapter 15

969

Chapter 16

974

A PPENDI X

G

Appendix G

914

Chapter 1

Answers to Questions

1.procedural, object-oriented

2.b

3.data, act on that data

4.a

5.data hiding

6.a, d

7.objects

8.false; the organizational principles are different

9.encapsulation

10.d

11.false; most lines of code are the same in C and C++

12.polymorphism

13.d

14.b

15.b, d

Chapter 2

Answers to Questions

1.b, c

2.parentheses

3.braces { }

4.It’s the first function executed when the program starts

5.statement

6.

// this is a comment

/* this is a comment */

7. a, d