Вы находитесь на странице: 1из 1329

Programming

Principles and Practice


Using ++

Second edition

Bja rne Stroustru

...
.... ....
Addison
Wesley

Upper Saddle River, NJ Boston lndianapolis San Francisco


New York Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City


++


-
2016
32.973.26-018.2. 75
83
681.3.07
""
. ..
. . ..
"" :
lnfo@wtlliamspu!shlng.com, http://www.wtlliamspu!shing.com
, .
83 : ++,
2- . : . . - . : " .. ", 20 1 6. - 1 328 . :
. - . . .
ISBN 978-5-8459- 1 949-6 (.)
32.973.26-018.2. 75

.

.
, .
Addlso-Wesley Pullshlg . !.
Authorlzed traslatlo from the Eglish laguage edltlo plshed Addlso-Wesley
Pllshig . !. Copyright 2014 Pearso Educatio. !.
All rlghts reserved. No part of thls pulicatlo may reproduced. stored l retleval system.
or trasmltted. l any form. or meas, electrolc, mechalcal. pttocopylng. recordlng, or
otherwlse. without the prlor wrltten permisslo of Plisher.
Russia lagage edltlon pullshed W llliams Pullshlng s accordlg to the rmt
with R&I Eterprlses Iteratloal, Copyrlght 2016

.-r

:
++
2-

.. .
..
.. m.
.. .

26.10.2015. 7 0 \ / 16.
lmes.
. . . 107.07. .-. . 66,7.
300 . No 6311




142300, . . . . . .1

". . ", 127055, . , . , . 43. . 1

ISBN 978-5-8459-1949-6 (.) "". 2016


JSBN 978-0-321-99278-9 (.) .
Pearso EdLtcatlo. / . 2014

25
. 33
1. , 53

1. 81
2. Hello, World! 83
3. , 99
4. 129
5. 173
6. 217
7. 267
8. : 301
9. : 351

11. 391
1 . 393
11. 429
12. 463
13. 495
14. 539
15. 575
16. 607

111. 639
17. 641
18. 687
19. , 729
20. 777
21. 827

IV. 873
22. 875
23. 925
24. 965
25. 1003
6

26. 1073
27. 1107

V. 1159

. 1161
. 1219
. Visual Studio 1283
. FLTK 1289
.
1295
1305
1313
1316
1327

25
28
29
ISO 30
31
31

. 33
0 . 1 . 34
0.1.1. 35
0.1.2. , .. 37
0.1.3. 38
0.2. 39
0.2.1. 42
0.2.2. 44
0.2.3. 45
0.3. 46
0.4. 46
0 . 5 . 47
0.6. 47
0.7. 48
48
"" 49
! 51

1. , 53
1 . 1 . 54
1 .2. 55
1 .3 . 58
1 .4. 62
1 . 5 . 62
1.5.1. 63
1.5.2. 64
1.5.3. 65
1.5.4. 68
1.5.5. 69
1.5.6. 71
1.5.7. ? 73
1 .6. 73

1. 81
2. Hello, World! 83
2.1. 84
2.2. 85
2.3. 88
2.4. 91
2 . 5 . 92
3. , 99
3. 1 . 100
3.2. 102
3.3. 104
3.4. 106
3.5. 109
3.5. l. : 111
3.6. 113
3.6. l. : 114
3.7. 115
3 . 8 . l17
3.9. 119
3. 9. l. 120
3.9.2. 121
4. 129
4. 1 . 130
4.2. 132
4.3. 135
4.3. l. 136
4.3.2. 138
4.3.3. 140
4.4. 141
4.4. l. 143
4.4.2. 149
4 . 5 . 154
4.5. l. 156
4.5.2. 157
4.6. 158
4.6. l. 160
4.6.2. 160
4.6.3. 161
4.6.4. 164
4. 7. 166
5. 173
5. 1 . 174
5 . 2 . 176
9

5 .3. 177
5.3.1. 1 78
5.3.2. . 179
5.3.3. 180
5.4. 1 81
5 . 5. 1 82
5.5. 1. 1 83
5.5.2. 185
5.5.3. 187
5.6. 188
5.6.1. 189
5.6.2. . 1 90
5.6.3. 1 92
5.6.4. 1 96
5. 7. 1 97
5.8. 200
5.9. 201
5.9.1. 203
5. 1 0 . - 207
5. 10.1. 209
5. 1 1 . 21 0

6. 217
6. 1 . 21 8
6.2. 21 9
6.2.1. 220
6.2.2. 220
6.3. 223
6.3.1. 224
6.3.2. 226
6.3.3. 228
6.3.4. 230
6.3.5. ! 232
6.4. 233
6.4.1. : 238
6.4.2. 239
6.5. 241
6.5.1. 241
6.5.2. 242
6.5.3. 246
6.5.4. 248
6.6. 249
6. 7. 254
6.8. 255
6.8.1. Token stream 257
10

6.8.2. 259
6.8.3. 260
6.9. 26 1

7. 267
7. 1 . 268
7.2. 268
7.3. 270
7.4. 275
7.5. : 0J 276
7.6. 278
7.6.1. 278
7.6.2. 280
7.6.3. 281
7.6.4. 283
7. 7. 285
7.8. 288
7 .8.1. 288
7.8.2. 293
7.8.3. 296
7.8.4. ? 296

8. : 301
8 . 1 . 302
8 . 2 . 303
8.2. 1. 308
8.2.2. 308
8.2.3. 3l
8.3. 310
8.4. 313
8 . 5 . 319
8.5.1. 319
8.5.2. 321
8.5.3. 322
8.5.4. 323
8.5.5. 325
8.5.6.
328
8.5. 7. 33 l
8.5.8. 332
8.5.9. nstr- 337
8.6. 338
8.6. l. 340
8.6.2. I 340
8 .7. 342
8. 7.1. using using 343
11

9. : 351
9.1. , 352
9.2. JUiacca 354
9.3. 354
9.4. JUiacca 356
9.4.1. 356
9.4.2. - 358
9.4.3. 360
9.4.4. - 362
9.4.5. 365
9.4.6. 365
9.5. 367
9.5. 1. "" 369
9.6. 370
9.7. JUiacco 371
9.7.1. 372
9.7.2. 375
9. 7.3. 376
9. 7.4. - 379
9. 7. 5. 381
9.8. Date 383

11. 391
1. 393
1.1. 394
10.2. - 396
10.3. 398
l 0.4. 399
10.5. 401
l 0.6. - 403
10.7. 407
10. 7.1. 409
1. 7.2. 412
l 0.8. , 413
10.9. . 414
10.10. JUI 415
10. l l. 417
10.11.1. 418
10.11.2. 420
10.11.3. 424
1 1. 429
11. l. 430
11.2. 431
1 1.2.1. 431
12

11.2.2. 434
11.2.3. 435
11.2.4. 436
11.2. 5. 437
11.3. 438
11.3. l. 439
11.3.2. 440
11.3.3. 443
11.4. 444
11.5. , 446
11.6. 447
11. 7. 449
11.8. 456
12. 463
12.1. ? 464
12.2. 465
12.3. 467
12.4.
471
12.5. 472
12.6. Shape 473
12.7. Shape 474
12. 7.1. main 474
12.7.2. 475
12.7.3. 477
12.7.4. 479
12.7.5. 480
12.7.6. 481
12.7.7. 484
12.7.8. 484
12.7.9. 486
12.7.10. 487
12.8. 488
12.8. 1. 490
13. 495
13.1. 496
13.2. Point Line 498
13.3. Lines 501
13.4. Color 504
13.5. Line _style 507
13.6. Open _polyline 509
13.7. Closed_polyline 510
13.8. Polygon 512
13.9. Rectangle 514
13

1 3 . 1 0. 519
13.11. t 521
13.12. Circle 523
1 3. 1 3 . Ellipse 525
13. 1 4. Marked_polyline 527
13. 1 5. Marks 529
1 3. 1 6. Mark 530
1 3. 1 7 . Image 532
14. 539
14. 1 . 540
14.1.1. 540
14.1.2. 542
14.1.3. 543
14.1.4. 545
14.2. Shape 546
14.2. 1. 54 7
14.2.2. 549
14.2.3. 552
14.2.4. 556
1 4.3. 557
14.3.1. 559
14.3.2. 56 1
14.3.3. 562
14.3.4. 564
14.3.5. 565
1 4.4. -
567
1 5. 575
1 5. 1 . 576
1 5.2. 576
1 5.3. Function 580
15.3.1. 582
15.3.2. 583
15.3.3. - 585
1 5.4. 586
1 5. 5 . 588
15.6. 594
15.6. 1. 596
15.6.2. 598
15.6.3. 598
15.6.4. 600
14

16. 607
16.1. 608
16.2. Next 609
16.3. 61 1
16.3. 1. 6 13
16.3.2. 6 16
16.3.3. - 617
1 6.4. Button Widget 618
16.4. 1. Widget 618
16.4.2. Button 620
16.4.3. In Out 620
16.4.4. Menu 62 1
1 6. 5. 622
1 6.6. 626
1 6.7. 627
16.8. GUI- 632

111. 639
17. 64 1
17. 1 . 642
1 7.2. 644
17.3. , 646
17.3. 1.0epaopsizeof 649
1 7.4. 650
17.4.1. 65 1
17.4.2. 653
17.4.3. 654
17.4.4. 656
17.4.5. 657
17.4.6. 658
1 7. 5 . 66 1
17.5.1. 663
17.5.2. 664
1 7.6. 665
1 7. 7. 666
17.8. : void* 668
17.9. 67 1
17.9. 1. 672
17.9.2. , 673
17.9.3. : 674
17.9.4. 676
17.9.5. 677
17. 1 0. this 679
17. 10. 1. 681
15

18. 687
18. 1. 688
18.2. 689
18.3. 691
18.3. l. 693
18.3.2. 694
18.3.3. , 696
18.3.4. 697
18.4. 700
18.4. l. 702
18.4.2. 703
18.5. 706
18.5.1. 707
18.6. 708
18.6. 1. 710
18.6.2. 712
18.6.3. 714
18.6.4. 716
18.7. : 719
18.7.1. string 719
18. 7.2. 720
18. 7.3. 721
19. , 729
19.1. 730
19.2. 734
19.2. 1. 734
19.2.2. reserve capaci ty 736
19.2.3. resize 736
19.2.4. push back
_ 737
19.2.5. 738
19.2.6. 740
19.3. 741
19.3.1. 741
19.3.2. 744
19.3.3. 747
19.3.4. 750
19.3.5. 751
19.3.6. 753
19.3.7. vector 753
19.4. 757
19.4. l . : 758
19.4.2. 760
19.5. 762
19.5. 1. 763
19.5.2. - 765
16

19 .5 .3 . 766
19 .5 .4 . unique_ptr 767
19 .5 .5 . 769
19 .5 .6 . RAII I vector 770

20. 777
20 .1 . 778
20 .1 .1 . 779
20 .1.2. 780
20 .2 . STL 783
20 . . 787
20.3 .1 . 790
20 .4 . 791
20.4 .1. 793
20.4.2. 794
20 .5 . vector 797
20.5.1. 799
20 .5 .2 auto 799
20 .6 . : 801
20 .6. 1. 803
20 .6 .2 . 804
20 . 7. vector , li s t s tring 808
20.7 .1. insert erase 810
20.8 . vector STL 813
20 .9 . STL 815
20 .10 . 817
20 .1 .1 . 820

21. 827
21 .1 . 828
21 .2 . : f ind () 829
21 .2.1. 832
21 .3 . : f i nd_i f () 833
21 .4 . 835
21.4 .1 . 836
21.4.2. 838
21.4 .3 . - 839
21 .5 . 840
21.5 .1. accumulate () 841
21 .5 .2. accumulate () 842
21 .5 .3 . inner_product () 844
21 .5 .4 . inner_product () 845
21 .6 . 846
21 .6 .1. map 84 7
21 .6.2. map 849
21 .6 .3. map 852
17

2 1.6.4. unordered_map 854


2 1.6.5. set 857
21.7. 859
2 1.7. 1. 859
2 1.7.2. 860
2 1. 7.3. set 863
21.7.4. copy _if 863
2 1 .8. 864
2 1 .9. 866

IV. 873
22. 875
22.1. , 876
22.1.1. 877
22. 1.2. 878
22. 1.3. 887
22.2. 89 1
22.2. 1. 892
22.2.2. 894
22.2.3. Algol 90 1
22.2.4. Simula 909
22.2.5. 91 1
22.2.6. ++ 9 15
22.2.7. 919
22.2.8. 920
23. 925
23.1. 926
23.2. 926
23.3. - 930
23.4. 931
23.4. 1. 937
23.5. 939
23.6. 94 1
23.6. 1. 944
23.7. 945
23.8. 947
23.8. 1. 948
23.8.2. 949
23.8.3. 950
23.8.4. 951
23.8.5. 951
23.8.6. 952
23.8.7. 954
18

23 .9 . 955
23 .10 . 960

24. 965
24 .1 . 966
24 .2 . , 967
24 .2 .1 . 970
24 .3 . 971
24 .4 . 972
24 .5 . Matrix 974
24 .5.1 . 975
24 .5 .2 . 978
24 .5 .3 . 981
24 .5 .4 . - 984
24 .5.5 . 984
24 .6 . : 985
24.6 .1 . 987
24.6 .2 . 988
24 .6 .3. 989
24 .7 . 991
24 .8 . 994
24 .9 . 995
24 .10 . 997

25. 1003
25 .1 . 1004
25 .2 . 1008
25 .2 .1 . 111
25 .2 .2 . 1012
25 .2 .3 . !013
25 .3 . !015
25 .3.1 . !017
25 .3.2 . 1020
25 .3 .3 . 1021
25.3.4 . 1023
25 .4 . , 1024
25.4 .1. 1024
25 .4 .2 . : 1025
25.4.3. : 1029
25 .4 .4 . 1032
25 .5 . , 1036
25 .5 .1 . 1036
25 .5 .2 . i tset 1041
25 .5 .3 . 1042
25 .5 .4 . !047
25.5.5. 1049
19

25 .5.6 . : 1051
25 .6 . 1056
25 .6 .1. ? 1058
25 .6 .2 . 1059
25 .6 .3 . 1065

26. 1073
26 . 1 . 1074
26 . 1 . 1 .
1076
26 .2 . 1076
26 .3 . 1076
26 .3 .1 . 1078
26 .3 .2 . 1078
26 .3 .3 . 1086
26 .3 .4 . 1094
26 .3 .5 . . 1095
26 .4 . 1097
26 .5 . 1098
26 .6 . 1099
26 .6 . 1 . 1101
26 .7 . 1103

27. 1 107
27 . 1 . ++: 1108
27 . 1. l.
++ 1 1 11
1112
27 . l.2. ++, 1112
27 . 1 .3. 1 114
27 .2 . 1115
27 .2. l. 1 116
27 .2 .2 . 1 116
27 .2 .3 . 1118
27 .2 .4 . - ++- 1 120
27 .2 .5 . 1 122
27 .3 . 1 123
27 .3 . l. struct 1 124
27 .3 .2 . 1 125
27 .3 .3 . 1 125
27 .3 .4 . 1 127
27 .3 .5. void* 1128
27 .3 .6 . 1129
27 .3 .7. 1130
27 .4 . 1130
27 .5 . 1132
27 .5 . 1 . const 1135
27 .5.2 . 1136
20

27 .5 .3 .: strcpy() 1136
27 .5 .4. 1137
27 .6 . -: stdio . h 1138
27.6 .1 . 1138
27 .6.2 . 1139
27.6.3 . 1141
27 .7 . 1142
27 .8 . 1 143
27 .8 .1 . . 1144
27 .8.2 . 1145
27 .8.3 . 1146
27 .9 . : 1 147

V. 1 159

. 1161
1 . 1 162
.1.1 . 1163
.1.2 . 1164
. 1 .3 . 1164
.2 . 1165
.2. 1 . 1165
.2 .2 . 1 167
.2 .3 . 1 168
.2 .4 . 1168
.2 .5 . 1 169
.2 .6 . 1 169
. 1169
. .1 . 1170
.4 . , 1170
.4 .1 . 1171
.4 .2 . 1172
.4 .3 . 1173
.5 . 1174
.5 .1. , 1178
.5 .2 . 1179
.5 .3. 1 18 1
.5 .4 . sizeof 1182
.5 .5 . 1182
.5 .6. new delete 1182
.5 .7 . 1183
.6 . 1184
7 . 1186
. 7.1 . 1187
. . 1187
.8 .1 . 1 188
21

.8 .2 . 1190
.8 .3 . 1191
.9 . 1191
.9 .1 . 1192
.9 .2. 1193
.9 .3 . 1194
.9 .4 . 1194
l . , 1195
. 1 . l. 1195
.11 . 1196
.12 . 1197
.12. l. 1197
.12 .2 . 1200
.12 .3 . , 1201
.12 .4 . 1204
.12.5 . 1208
.12 .6 . 1209
.13 . 1209
.13 . 1 . 1210
.13.2. 1211
.13 .3. - 1212
.14 . 1213
.15 . 1215
.16 . 1216
.17 . 1216
.17 .1 . #include 1217
.17 .2. #define 1217

. 1219
.1 . 1220
.1. 1 . 1221
.1.2. s t d 1224
.1.3. 1224
.2 . 1225
.2.1. 1225
.3 . 1227
.3.1. 1227
.3.2. 1229
.4 . 1231
.4 .1 . 1233
.4.2. - 1234
.4.3. . 1234
.4.4. 1235
.4.5. 1235
.4 .6 . 1236
.4.7 . 1237
22

.4.8. 1237
.4.9. 1238
.4.1. 1238
. 5. 1239
.5.1. 1240
.5.2. . 1241
.5.3. 1244
.5.4. 1244
.5.5. 1246
.5.6. 1247
.5.7. 1248
.5.8. min max 1249
.6. STL 1250
.6.1. 1250
.6.2. 1250
.6.3. pair tuple 1252
.6.4. 1253
.6.5. 1254
.7. - 1255
.7.1. - 1256
.7.2. 1258
.7.3. 1258
.7.4. 1259
.7.5. 1260
.7.6. 1260
.8. 1261
.8.1. 1262
.8.2. 1262
.8.3. 1264
.9. 1266
.9.1. 1266
.9.2. 1267
.9. 3. 1268
.9.4. valarray 1269
.9.5. 1269
.9.6. 1270
. l . 1270
. 1 1 . 1271
.11.1. 1271
.11.2. printf () 1272
. 11.3. - 1277
.11.4. 1278
.11.5. 1279
. 11. 6. 1280
. 12. 1281
23

. Visual Studio 1283


. 1 . 1284
.2 . Visual Studio 1284
. . 1285
.3.1. 1285
.3.2. std_ lib_ f acilities. h 1285
.3.3. ++ 1286
.3.4. 1286
.3.5. 1286
.3.6. 1287
.3.7. 1287
.4 . 1287
. FLTK 1289
. 1. 1290
.2. FLTK 1290
. . FLTK 1291
.4. FLTK Visual Studio 1292
.5. , 1292
.
1295
.1. 1296
.2. Widget 1297
. . Window 1298
.4 . Vector_ref 1300
.5. : Widget 1301
1305
13 13

1316
1327

- .
.
. .

.
. .
.

++. .


. .
( .
).
, .
(. . 15
. , 14 ) .
. .
.
. . :
.
.
- . . .
- .
u . .
".
?
. .

1 I (1801-1870) - .

. .
.
26

, " " ,
,
. ,
-
.

(, ,
) , .
, - -
,
.
,
. . .
.
, - .
++? .
, ++
.
. ++
.
. , + + ,
. ,
++,
. /
.
, ++ ,
. . # , Fortran
Java. ,
.

. , .
.
.
,
. ,
.
, -
, ,
. . ,
.
,
. .
.
27

, , , ,
. . ,
, . ,
. ,
. -
, . .
.
,
.
. , .
. , ,

. ,
.
-
. "
" .
. , ,
.

. ++
++.
, ,
.
.
: , ,
. ++
. . .
.
, " ++ " (.
, ) .
.
,
. ,
, . -
. ,
.
.
. ,
, ,
. ,
.
. .
28

, .
.
. , !
, - (
!),
.
++ . .
, .
,
. , - ,
,
.
,
, ..?
? , ! -
,
, .
.
, .
, .

.
, . ,
. .
.

. !



(1exas & University).
60% . 40%
.
. ,
.
.
, .
. ,
. .

. -
29

, .
!
,
. ,
,

.
,
. ,
( ).
: ( ) - ,
. (..
, ),
.
- -
, .
- ,
1 , 22. ,
,
.
, .
, .
, ,
,
- .
, "
",
,
,
.
, .
,
.


, ,
CllIA (Computer Science 1 0 1 ) . ,
.
, (
. .
, ..). ,
30

, ,
.
.
( , ,
), ,
.
, .
,
, (
), -
,
.
, .
, ..
.

150
++ ISO . ISO
1998 . ++ ++98 .
++11,
(,
, for ,
, -)
. , , " ++11 .
.
++11 ,
IS-, ++14,
++. , . ++ 11
-

++14 . ,
vector<int> vl;
vector<int> v2 {vl}; // ++14


vector<int> vl;
vector<int> v2 = vl; // ++98

++1 1
.

. , ++
( . www. stroustrup . / cornpilers. htrnl).
,
.
31


- ; , www . stroustrup . com/

Pr ogr amming ,
.
, -, ,
.


,

,




.

"" (Law
rence "Pete" Petersen) ,
.
.
, ,
, . "",
, " ".
,
(
ENGR 112, ENGR 113 CSCE 121),
, (Walter Daugherity),
(Hyunyounp; Lee), (Thresa Leyk). (Ronnle
Ward) (Jennifer Welch), .
, (Damian Dechev),
(Tracy Hammond), (Arne Tolstru p Madsen),
(Gabriel Dos Reis) , (Nlcholas
Stroustrup), . . (J. . van Wlnkel), (Greg
Versoonder), (Ronnie Ward) (Leor Zolman)
. r
(Moens Hansen)
32

, . (Al Aho),
(Stephen Edwards), (Brian Kernighan)
(Daisy Nguyen) , ,
.
(Art Werschulz)
,

- , (Nick Maclaren)
,
.

.
, Addison-Wesley
. ,
++ . .
(Richard Enbody), r (David Gustafson), -
(Ron McCarty) . (. Narayanaswamy) .
I (Peter Gordon)
( ) .
Addison-Wesley.
. (Linda
Begley), (m Arney Keane),
(Rob Mauhar), (Julie Nahil)
(BarbaraWood).
,
. ,
(L Joly)
(Michel Michaud),
.
(Brian Kernighan)
- (Doug Mcllroy) ,
. (Dennis
Ritchie) (risten Nygaard)
.

.4, ,,
, ,.,-

1.,t.- -f-'"&4.-

;
- ,
. ,
,
.
.
, ,
;
" " "
" . ,
,
.
34

0 . 1 . 0.3.
0.1 . 1 .
0.1 .2. , .. 0.4.
0.1 .3.
0.2. 0.5.
0.2. 1 . 0.6.
0.2.2. 0.7.

0.2.3. ""

0. 1 .
.
I, "",
++ .
.
, , .
, ,
.
, " ",
,
. , ,
,
,
(GUI).
III, " ",
++ (standard template library -

STL) .
(, vector, list map) ,
, , .
,
(, sort, find inneryroduct).
N, " ",
.
, ,
.
,
. ,
++ ,
0.1. 35


(integrated development environment IDE)
-

(graphical
user interface GUI).
-

,
.
, (
). ,
-
++. -
, ,
.
, .
,
( . 0.2).
.
,
, ,
,
.
" " ( ).

"".

"".

0.1 . 1 .
. ,
.
"" , ,
"" - ,
, .
, .

- .
,
.
. ,
, ,
36

, .
,
.
, . .
(. ,
- ).
. ,
, ,
. ,
" "
, . .
, .
,
- , .
, , -.
.
" .
!" ,
.
. ,
. - ,
, ,
. ( )
, , .
,
.

. , ,
, .

, ,
, , " ".
, , ,
, - .
, : ,

.
.
,
, . -
.
" ", ..
, ,
0.1. 37

. ,
- ; ++
The ++ Programming Language. Fourth Edition (Addi
son-Wesley, 20 1 3) ISO ++.

0.1 .2. , ..
-
,
.
.
. ,
, .
.
.
, .
. ,
, .
. , ,
,
. ,
.
, , .
.
.
, .
, ,
.
, , .
,
( , ).
,
.
(, ),
, " ".
,
.
, ;
.
,
, ,
.
38

. . .
, .
- , ,
, - .
;
, .
.
, .
.
,
.
.
-"
++. ,
, ,
.
- .
.
.

0.1 ..

++, ? , !
- , ,
. ,
.
, ,
, (, ,
) .
. ,
,

.
, .

- (, Bjame
Stroustrup. h ++ Programming Language, Special Edition (Addison-Wesley,
2000)), ,
(, Qt
GUI
, ,
++. . , .
0.2. 39

++. - . : " ", 2002 . -


384 . : . ++ . - . :
, 2002. - 400 . ; . , " " .
- .
. - . : , 2004. - 366 . ) .
0 . 6 " "
.

.
( -
),
.

0 . 2.

? ,
, .
.

( )

- (1 / 0)

//

++

(
). ,
.
40 .n

. . . ,
.
++,
. , ++ -

. .
. ++,
++ .
, , ,
.
. ,
.
( ), .

, .
"
++.
. , vector
.
,
. - ,

. ,
, ,
.
.
. (
, I?)
. , ,
.
.
. (. - ,
1-11) . -
.
, .
,
, .
. ,
. ,
, , ,
" . , ! ,
, .
,
0.2. 41

, . , ;
.
.
.
. - "
; ,
!" .
. ? .
. ,
. .
, , .
, !

- . ,
++
. ,
. .
,
, .
, . .
. ,
, , . .
, .
.
.
.
.
.
, . . .
.
, . . . .
,
.
" " . ,

.
. .

.
.
++
. . ,
.
42

.
. , -
. -,
. -
- . - .
- www . s trous trup . /
Programm i ng.
, "" .
- ,
, .
, ,
,
. ,
, ,
50 .

.
,
. -
,
. ,
, .
, () .

D.2. 1 .
.
.
", , -
" .
,
. ,

.
, .
,
, . ,
, ,
.
.
. ,
; ,
. , , .
0.2. 43

, , ,
.

" 113 ' .


++
,
, ,
. ++
, ,

.
" .
.
,
, r
.
" - .
.
"

" " (
).
,
. .
. ,
.
" ". .
,

,
, . .

. .
.
" 1" .
.

. ,
. .
"
", (
) ,
.
44

"w
". . "
", , " " :


.
" -
" . - -
,
. ,
,
.
(,
),
6 1 2.
" ".

. - ,
, - ,
.
, .
, .
.
.
, ,
:
.

0.2.2.
. ,

.
. -
, .
. ,
,
,
.
,
( 5 6), ( 9.4.3) -
0.2. 45

( 9.7 , 1 4. 1 1 4.2),
.

. ,
,
. -
. ,
- . "", .

, .
++.
. . ++,
,
, .

0.2. .
, ++
. ++
, !


.
ISO Standard ++,
. ,

++
.
.
++ .

.
,
Vlsual Studio Microsoft ++
Windows, .
.
.
; .
, ,
, , my_filel .
my_file2 . cpp. GNU ++
Unix Llnux, :
46 D

q++ - my_.Proqram my_filel . cpp my_file2 . cpp


my_.Proqram

, .

..
?
, ! ,
, , .
, .
. -
, .. .
, , .
,
. , ,
.
, . .
.
- (
).
(

) . . .
, ,
, .
,
, .

0.4.
-
, . ,
, , ,
. ,
:
,
;
. ,
.
, ""
.
0.5. 47

0.5.
;
.
.
; .
, , , ,
. .
, ,
, , ..
.
- www . stroustrup . com/Prograrnrning.

0 . 6 .
, , ,
.
Becker, Pete, ed. The ++ Standard. ISO/IEC 1 4882:20 1 1 .
1. Blanchette, Jasmin, and Mark Summerfield. ++ GUI Programming with
Qt 4, Second Edition. Prentice Hall, 2008. ISBN 0 1 32354 1 60.
2. Koenlg, Andrew, and Barbara . . Acce/erated ++: Practical Pro
grwnming /. Addison-Wesley, 2000. ISBN 020 1 70353.
3. Meyers, Scott. Elfective ++: 55 Specific Ways to Improve Your Progrwns
and Desigrts, hird Edition. Addison-Wesley. 2005. ISBN 032 1 334876.
4. Schmidt, Douglas . , and Stephen . Huston . ++ Network Programming,
Volume 1 : Masterin.g Complexity with and Patterns. Addison-Wesley,
200 1 . ISBN 020 1 604647 .
5. Schmidt, Douglas . , and Stephen . Huston. ++ Network Progrwnming,
Volume 2: Systematic Reuse with and Frameworks. Addison-Wesley,
2002. ISBN 020 1 795256.
6. Stroustrup, Bjarne. h Design and Evolution qf ++. Addison-Wesley,
1 994. ISBN 020 1 543303.
7. Stroustrup. Bjarne. "Learning Standard ++ as New Language." /++
Users Joumal, 1 999.
8. Stroustrup, Bjarne. h ++ Programming Lan.guage, Fourth Edition. Addi
son-Wesley, 20 1 3 . ISBN 032 1 563840.
9. Stroustrup, Bjarne. Tour ojC++. Addison-Wesley, 20 1 3 .
ISBN 032 1 9583 1 4.
1 0. Sutter, Herb. Excepiional ++: 4 7 Engineerin.g Puzz/es, Programmin.g Prob
lerrts, and Solutions. Addison-Wesley. 1 999. ISBN 020 1 6 1 5622.
.
48

. 7.
w ,
?" ,
. . ,
w"
.



++.



.
,
,
,
, .
.
,
++
. ,
-
ISO ++,
.
, .
, w urm ++" " 11
++", .
, ,

.
, . &,

(Computer Science Research Center of Bell Labs;
Unix, ++. ),
- & Labs-Research.
(U .S.
National Academy of Engineering).
(Association for Computing Machinery - ).
(lnstitute of Electrical and Electronics
Engineers - IEEE). .
0. 7 . 49

2005
(Willlam Procter Prize for Scientific Achievement),
Sigma Xi .
20 1
-, , -
Rigmor og Carl Holst-Knudsens Videnskapspris. 20 1 3

, -. .
. . .
. .
( . .
) ( . , ) .
.
.
, .
-
www . s t rous t rup . . , .
2

" "
2006 : " -
.

(Thxas & ) .

(Thaching
Excellence Awards). 1 996
(Distinguished
Thaching Award)
(Alumni Association for the
College of Engineertng) .
(Wakonse Program for Thaching Excel
Ience).
(Academy for Educator Development).
. .
,
-

2 - http : / /www . s t roust rup . com/bs_fa q . html


, ,
, . .
, .
. . . -
50

. 1 97 1 1 973
-, (Field Artillery
Officer's Advanced Course at Fort Sill, Oklahoma). 1 979
- 1 978 1 98 1 1 985
1 989
ClllA.
1 99 1 ,
1 999 .
,
, .

(Georgia Thch),
.
.

.
. , .
, .
,
, , , .
".
, 2007 .
.

,
,
. , .
( )
,
( ).
, . .
, , .
. ,
, ,
.
! 51

!
. , .
, ,
.
.
.
-
. ,
, ,
, .
,
, .

.
:
E-maH: i n f o @w i l l i amspu i shing .
VVVV:VV h t tp : / /www . wi l l i amspu i s h ing . com

: 1 27055, . . , . 43, 1
: 03 1 50, , / 1 52
,

, ,
, ,
.

,
.
,
,

, .
54 1 , .n

1 .1 . 1 .5.3.
1 .2. 1 .5.4.
1 .3. 1 .5.5.
1 .4. 1 .5.6.
1 .5.7.
1 .5 .
1 .6.
1 .5 . 1 .
1 .5.2.

1 . 1 .
,
. ,
,
. ,
, . .
. .
, ,
, .
,
.
, .
,
.
.
.
?
?
. ?
.
?
, ,
..? ?
?
- ,

, ! ,
,
- ,
,
, ,
1 .2. 55

, - !
, ,
. , ( .
, ),
.

.
-
. .

1 .2.
.
, . -
, .
,
, , , ,
, .
. ,

, .
? ;
. , .
, , , ,
.
( )
? ,
, - , - -
- . , (
. ),
. ,
, .
.
,
. - ,
, ,
, :
, ..
,
! ,
, ( , , . . ) ;
,
56 1 . r

. ,
,
( ).
, ,
r .
,
. , ,
, .
,
.
?
. , . -
, ,
. ,
,
, .
; ,
. ( ,
, ..).
, ,
, .
, , ; ,
,
( )
-
. ,
, ( ,
) . ?
;
, (
, )
. (, ,
, )
.
;

(. . ) .
. ,
,
.
.
, .
, ,
1 . 2. 57

:
.
.
.
, , .
" ";

.
. . , .
,
.
,

. , .
, ,
, .
, , ,
.
, - . . -
. ,
. ,
, ,
.
.
(,
, , ..) .
- , ,
.
,
,
. .
.
. .

.
, .
, .
. ,
, . , !
,
. ,
.
, .
58 1 .

,
. .
( ),
, ,
. ,
.

1 ..
.
,
.
. , -
, - - ,
- . .
.
h " "
. , ,
- .
,
. ( - )
, . ,
, .
, ,
, , , ,
.
.
, ?
, ! ,

, ,
. . . ,
. . -.
, , ,
, .
, , , ,
.
, , ,
, . , ,
,
. .
, :
1 .3. 59

, ,
.
, .
.
. ,
. ,
( )

: , , ,
, , , , ,
. , ,
, ( .
- ) .
, ,
.
- - . .
.
. ,
. .
, ,
- .

. .
( )
- ,
. , , -
, ,
. .

.
,
, .
.
- .
, : , ,
. . ,
, .
, ,
, .
,
, -
. . .
. , -
60 1 ,

,
- . JI
-
, .
, - -

. -
- ! -
.

. .
,
, .
. JI
, ,
.
,
, , ,
. , . ,
,
- .
(, ), .
, .
, ? -
,
. ,
- ,
- ,
, , .
, ,
. (Dilbert),
,
(
) . ,
.
JI ?
,
, . ,
, ,
,
, , . ,
,
.
1 . 3. 61

, , ,
.
,
, ,
. -
. .
,
, , ,
.
- ,
.
?

. ( ) -
,
. , ,
, - (,
),
. , ,
.
,
.
, ,
, , ,

. ,
.
, -
.
,

(
) .
-
- ,
.
,
.
,
-
.
.
.
62 1 .

1 .4.

.
, ,
,
. ,
, , , , ,
, .
1 995 ClllA "
", : "
.
, ,
; , ;
; ;
". ,
: " ,
, ,
.
;
(, ) ,
(, )
, . .

,
" .
-
,
,
. - ,
;
,
. , ,
, -
. .
: , -
. .

1 .5.
.
. . ,
1 . 5. 63

. , ,
- , -
, .

. , ,
, ,
.
. .
, ,
. -
.
,
. ,
.
,
. .

1 .5 . 1 .

.
.

. ,
, ,
-.
( ,
). ,
.
- (
64 1 ,

) .
. ,
, .

1 .5.2.

.
.

,
.
. . ,
.

, .
, .
. .
,
.
. ,
-
, .
, .

CAD/CAM
(computer-aided design and computer-aided manufacture).
.
.
1 00 (.
) . , . -
1 .5. 65

, ,
. ,
( ), .
. .
-
( ),
, ,
. -
,
. , , -
(397 56 ),
.
. ;
,
.
,
(
). ,
(global positioning system - GPS), ,
. ,
( , . . )
( ), .
,
.
,
? 25 .
-
. , .
. ,
- , .
, ;
.

1 .5..

( , -, F -
-).
,
. ,
, , , ,
66 1 ,

. ,
,
.
( ""
). , .
, ( )
, .
-.
: ,
. - .
, ,
- , ,
:
.
,
, . , ,
- - . ,
"!" .
,
. ? ?
?
,
,
. ,
( )
. " ".
20 . ;
( , ) (
,
1 .5. 67

).
:
99 ,9999 % (,
20 20 ).

.
,
,
,
, ,
, , , .

.
? ?
?
, ,
, .
.
, , -
,
. .
, ,
.
.

, ,
-
- , (
).
,
,
.
68 1 ,

1 .5.4.


( ).

,
. -
; ,
,

.

. ,

.
, , ,
.
() -
,
.
, .
, ,
(, ) .
, ,
. ,

,
.
,
.
? ?
?
? ? .
1 .5. 69

"" "" ,
.
(. ,
, , ,
, , ..)
.

1 .5.5.

.


: ,
.
. , .

. ,
, .
- ,
-. ,
Google, Arnazo Microsoft,
( , .
). ,
.
Google (
), " " (data
ceter) 25 50.
,
60 1 00 2 .
70 1 ,

20 1 l N York imes ,
Google - 260
( -).

3 24 .
l 2x l 0 1 5 24 l 0 1 5 8- .
4 .
4x l 01 8 .
. .
. .
,

. () -
. () - , . .
. Google
. .
-
. Amazon
( ). Amadeus (
) (-). .
.
,
, .

. , , .
, . .
, . . (
, ).
.
: ()
,
. .
1 2:30?
?
. ?
1 996
?
2 1 2
20 1 2 ?

?
1 .5. 71

- .
(
) ( . ).
- ( - ,
).
.

(information processtng). -
, - ,
,
:
. ,
(,
" " " " )
, ,

. ,
, ,
,
. ,
: , , ,
. ,
,
, .

1 .5.6.
r.
, ,
. , ,
,
. ,
NASA .
72 1 .

, ,
.
, .
. , . , ..
. .
-
. (Spirit) 6 .
( Opportunity)
20 1 4 . ,
.
25-
;
,

. ,
, -
.
, ,
, -
, .
. .
, .
.
, ,
. ,
. .
D/. ,
,
,

( ).
.

,
.

-.
,
( ,
). (
"" ,
"").
1 .6. 73

1 .5. 7 . ?


++? -
.
. ,
++
, . . .
++. -. .
, , .
. ++,
- www . s troustrup . com/ appl icat ions . html.

1 . 6.
?
?
( ) .
. , ,
, - .
. . ..
; ,
. , ,
. , .
. . .
; , -
. .
.
(.
) .
,
.
. .
:
r. .. ,
.
"" ( ), .
, .
.
- (, , ).
..
.
74 1 .

, ,
. - -
( -
), ,
. ,
. - !
, . ?
, , :
, .
? ;
,

. ,
. , ,
, ,
- ,
. , , ,
,
. , " -
", .
" ". ,
,
(
,
).
, "
- ":
, , . , -
, .
,
. -
. ,
, , .
.

. ? ?
? ?
?
. ?
? ?
?
?
1 .6. 75

. ()
.
( , . , ..).
.
.
. .
.
.
,
.
, ,
. ,
.
,
; ,
, ,
; . ,
.
.
.
,
, . -
.
,
. ,
,
, ,
. ,
(
, ,
). !
. ,
- .
, ,
, .
, ,
. , -
.
, .
. ,
. -: ,
. ,
76 1 .

, - .
( ) .
-!

. ,
.
. ,
,
" ". ,
.
-
. ,
.
.
, int,
.
, string,
. ,
, , - "",
,
( ). ,

(), .
, complex
atrix . ,
Shape (), Circle (), Color () Dialog_box
( ) . , ,
. istream (
"i" "" (input)). ,
.
. ,
. .
, .
,
. ,

. ,
( ) .

,
. 22.
1 . 6. 77


,
.
. ,
.
1 . ?
2. ?
3. ?
4. ,
? .
5. ?
.
6.
? .
7. ?
8. ! ,
?
9. ?
1 0. ? .
1 1 .
? .
1 2. ?
.
1 3 .
? .
1 4. -
?
1 5. ?
1 6 . .
1 7.
? .
1 8. ?
1 9. ?
78 1 .


++. ,
, , .
CAD/CAM




1 . .
(. ,
). ,
.
2. ,
- . ,
.
3. , . 2,
,
. . ,
. : , ,
, .
4. , ,
. .
5. ( ),
. ,
(,
-), ,
(,
).
6. , ,
, .
, !
7 . ,
, . ,
. .
1 .6. 79

8. , (
1 00, 500 ). ,
, , .
.
9. , (
, ),
( 1 00, 500 ).
1 . -
, ?
( 1 00 ).
1 1 . .
. .
.
1 2. .
, ,
.
( 1 00 ).
1 3 . )
, ) ) ?
, ,
20/.
1 4. ?
? ?


. -
,
. ,
: ,
.
,
, . ,
, .
,
.
, . , ,
. ,
.

, .
80 1 .

, (
. ). .
. .
, , .
.
. ..
- .
H e l l o, Worl d !

-
- (l )

++. - .
.


.
, -
- .
.
,
.
,
.
84 2 Hello. Worldl

2 . 1 .
2.2.
2.3.
2.4.
2.5.

2.1 .
-. ( - )
- , -
. , " ",
, - ,
.
. ,
,
, " " "
".
:
, ,
. , " , ,
" - , ,
.
, ,
.
. , . ,
.
, .
( ), ..
, , ,
, . ,
, , , .
.
. "
, , ".
? ? ""? ?
? ? ?
? ?
? .
.
( ),
2.2. 85

,
. .
++ - ,
.
,
1 . ,
,
. .

2.2.
.
Hello , World ! .
11 r "Hel l o , World ! "
iinclude " td_lib_facilities . h"
int ain() // r ++ ma i n
{
cout << " Hello , World ! \n"; // "Hel l o , Wo rl d ! "
return ;

,
;
. ,
. :
cout << " Hello , World! \n" ; // "Hel l o , Worl d ! "

.
Hello , World ! ,
; . Hello , World !
. -
, ,
.
++ ( " ) ;
.. "Hello , Word ! \ n " - . \n - "
", . cout
. , " cout"
<<. . cout
"see-out", "character output stream" ("
").
. ,
, , ,
,
.
86 2 Hello. Worldl


11 "He l l o , World ! "

. , / / (..
(/). ),
.
, .
, .
.

, .
, ,
, ,
, , . ,
. 7.6.4
. .
. ,
, .
. -
,
.
.
: , ,
- .
, - ,
.
; .
- ,
, .
11 "Hel l o , World ! "

.
. , .
. ,
, ( ,
).
, .

#include " std li facilities . h"

#include.
("") ,
s td lib facilities . h.
2.2. 87

, ++ (
++).

. ++. ,
,
. std_lib facili ties . h
_

,
- ++.
cout <<. ,
#include, . h
(heade), (heade file).
, cout.
.
. ?
main ,
. main :
int main ( ) // ++ ma i n
{
cout << "Hello , World! \n"; // "He l l o , World ! "
return ;

.
++ main. ,

, .
.
- int (.. integer,
), ,
( - ). int
++ ( ).
- ( .
.3. 1 ) .
; main.
, (. 8.2
8.6); ( ) .
1. { }
(). .
, ++
:
int main () { }
88 2 Hello. World!

. .
main "Hello, Worldl" :
cout < < " Hello, World! \n"; // "Hel l o , Worl d ! "
return ;

Hel lo , World ! ,
() . main ( )
, .
( , Unix/Linux)
. (),
main ( ) , , .
++,
#include ( :
. 4.4 . 1 7), (statement).

2 . .
++ - . ,

, . . .
, r.
, ,
, , ,
, .
++ (, hello_ .

world . cpp) . h (, s td_lib_facilities . h) ,


. obj ( Windows) . ( Unix) . ,

: ,
. ,
" " "
",
.

++ ++

, .
, ,
. ,
, .
. - ,
2.3. 89

#include, . . ,
.
,
. .
11
int main ( )
{
cout << "Hello , World! \n";
return ;

,
cout, .
. #include.
#include " std facilities . h"
int main ( )
{
cout << "Hello , World! \n";
return ;

, ,
std_lib_faci li ties . h. .
#include "std lib facilities . h "
int main ( )
{
cout << "Hello , World! \n;
return ;

( " ).
.
#include " std lib facilities . h"
inteqer main ( )
{
cout << "Hello, World ! \n";
return ;

int integer.
++ . .
#include " std lib facilities.h"
int main ( )
{
cout < "Hello , World! \n";
return ;
90 2 Hello. World!

<< ( ) < (
""). .
llinclude " std lib facilities.h"
int ain()
{
cout << ' Hello , World! \ n ' ;
return ;

, ,
. .
flinclude " std lib facilities . h"
int ain()
{
cout << " Hello, World ! \n"
return ;

,
, . , ++
( ; ).

. ,
, .
: ,
} .

,
? ,
.
. ,
, , ,
, ,
. ,
.
.
. ,
(,
) ,
. , , :

, ,
: , , ,
++.
2.4. 91

(
!) .
. , .
, ,
++.
,
, , , -
, . - .
,
. : -
: . , -
.

2.4.
,
. . "Hello,
World!" . ,
++. ( ..
) ,
,
. , ,
( ) .

++
hello_world.cpp

++


++:

ostream.obj
hello_world.obj

hello_world.exe
92 2 Hello, World!

,
. .
Windows, Windows,
Linux.
- (
). ,
, #include.
-

, ,
: ( . .
, 4.5.2).
. ,
; , ,
. .
,
. ,
,
. .
, .
5.

2.5.
.
, ,
. ,

. ,
, .
,
, ,
. IDE
(interactive (integrated) development environment - (
) ),
,
.
.
,
, . ,
,
, . -

(
) .
2.5. 93

, ,
, ++.
, ,
++,
.
.


. (
, ). .

.
.
,
. "Hello, Worldl" ,
.
-
. ,
-
, ""
.

.
. ,
, (
). .
, .
, ,
,
. . ,
,
. , .
, . -

-
.
, ,
; .
,
.
94 2 Hello, World!

,
,
. ,
, .
, .
1 . , ,
. ++
hello world.
2. hello_world . cpp ,
,
hello world.
finclude " std li facilities . h"
int main ( ) // ++ ma i n
{
cout << " Hello , World! \n" ; // "He l l o , Worl d ! "
keep_window_open ( ) ; 11
return ;

keep_window_open ( )
Windows ,
, .
Windows, ++.
,
keep_window_open ( ) std_lib_faci lities . h.
s td_lib_facilities . h?
, . ,
www . s trous trup . com/ Programming.
, ? ( !)
linclude
linclude<iostrea>
linclude<strinq>
linclude<vector>
finclude<alqoritlun>
linclude<cath>
usinq naespace s td ;
inline void keep_window_open ( ) { char ch ; cin>>ch ; }

. 5
8. 7.
3 . "Hello, World!".
, .

2.5. 95

.
!
,
, , ,
.
4. ,
. ,
!
, ,
2.3, .

(, keep window open ( ) ,
_ _


) ,
.


-
, , .
;
, ,
.
, ,
? , .
,
? , ,
?
1 . , "Hello, Worldl"?
2. .
3. ,
++.
4. , return "Hello,Worldl"?
5. , ?
6. , #include?
7. . h ++?
8. ?
9. ?
10.
?
11. , ?
96 2 Hello. World!

,
. ,
, .
:
,
.

11

++. ,
, , .
tinclude

11
<<
++
cout
IDE
main ()
,
.

;
, .
.
1 . , :
Hello , proqrai nq !
Here we qo !

2. , ,
,
, 2 . 1 .
, .
- ? .
, . :
" - .
, (.
),
. ,
. .
2.5. 97

3. ,
( , : ,
).
. ,
.
4.
(
, - ).
, ,
,
, .

. , ,

. , ,
!
,
.
,
. ,
, ,
,
.
(
, ,
). , (
, ) .
. (
? ?
?)
.
5. ,
"". ,
( ), ,
. ,
.
- . ,
www . stroustrup . com/glossary. html . ,
.
, .
.
98 2 Hello. World!

,
.
, .

"Hello, Worldl" ? -
.
.
:
,
, ,
.
,
. .
,
.
, ,
.
,


.
i -
.
, ,

char, int , doule
string.
100 ,

3.1. 3.6. 1 . : -
3.2.
3.3. 3.7.
3.4. 3.8.
3.5. 3.9.
3.5 . 1 . : 3.9. 1 .
3.9.2.
3.6.

. 1 .
"Hello, Worldl" .
. , ..
. . , ,
- , ,
, .
, ,
; , -
, , . -
"" . - ,
, ,
. . ,
string, -
int. "",
, .
lnt :
age: j 42

, int age,
42. ,
,
.
11
#include " s td lib facilities . h"
int main ( )

cout << "'l' ( ' enter ' ) : \n " ;


strinq first_name ; // fi r s t_name s t ring
-

cin >> first_nae ; // fi rs t


cout << " , " << first name << " ! \n " ;
.1. 101

#include main ( ) 2.
#include (
1 2). , .
.
main ( )

cout << "'l' ( ' enter ' ) : \n " ;

,
.
main ( ) ,
.
.
- . string
first_name.
. .
strinq first_naJll8 ; // fi rs t_name - s t ring


first_name.
string :
first_name : '
----

.
, 11w .
()
:
cin >> first_na.s ; // fi rs t_ name

cin ( "-"
character input),
. >> (" ")
, . ,
. Nicolas, <Enter>
. "Nicolas " first_name.
strlng :
first_name : 1 Nlcholas 1
,
.
( <Enter>) . -
. "" ,
,
102 ,

<Enter>.
. .
first_name,
.
cout << " , " << first_nae << " ! \n" ;

,
Nicolas ( first_name) ( ! )
( ' \n ' ) .
, Nicolas !

,
.
cout < < " , " ;
cout << first_nae ;
cout << " ! \n " ;

. ,
(
), .
, " "
, first_name.
.
, , .

cout << "first n " << " - " << first_nae ;

" first_name" ,
first_name
first_name, - Nicolas. ,

first n - Nicolas

.2.
, ,
,
. "",
, .
.
(, int s tring), ,
(, int
1 2 3 , s tring -
.2. 103

"Hello , World ! \n"). (


, int
*, string <=).
, , . ,
, ( ) .
( )
. :
strinq name "Annemarie" ;
=

int numer_of_steps 39 ; =

:
int : string :
number_of_steps : 1 39 name : 1 Annemarle

.
strinq name2 39; = // : 3 9 -
int numer_of s teps = "Annemarie" ; 11 : "Annema ri e " -
_
//


. ,
.
++ ( .
.8). ,
.
int numer_of_steps = 3 9 ; 1 1 i n t -
doue flyinq_time = 3 . 5 ; 1 1 do ue -
1 1 .
char decimal_yoint ' 1 1 ch a r -
strinq name "Annemarie" ; 1 1 s t ring -
bool tap_on true ; = 1 1 bool -

doue :
"
" ("doue precision floating point. ) "


.
,
.
39 11 i n t :
3.5 // dou e :
1 1
// cha r :
"Annemarie" // s t ri n g : ,
11
true // bool : ,
104 ,

, (, 1234 , 2 976)
, (.
' 1 ' . ' @ ' ' ' ) ,
(. 1 . 234, . 12 . 98)
, ,
(, 11 1234 11 , 11Howdy ! 11 11Annemarie 11 ), .
. 2.

..
>> (" ") ,
..
, . .
11
int main ( )
{
cout << " , \n" ;
s trinq first_nae ; //
int aqe ; //
cin >> first_nae ; / /
cin >> aqe ; //
cout << " , " << first_name << " ( aqe " << aqe << " ) \n " ;

, Carlos 22. >>


Carlos first_name, 22 -
age :
, Carlos ( 2 2 )

first_name Carlos 22?


,
(whitespace), .. ,
.
>> . .
;
>> .
22 Carlos.
, ,
. 22 first_name, .
, 22 - .
. Carlos ,
. 22,
11 ( 11 -
, -96739 . ?
.. 105

age, .
- " ",
. 1 0. 6
, .
age .
.
// ( 2 - )
int main ( )

cout << "Ji , \n" ;


strinq first_name " ? ? ? " ; 11 ( " ? ? ? "
11 " ")
int aqe = - 1 ; // (-1
11 " ")
cin >> first name >> aqe ; // ,
11
cout << " , " << first name << " ( " << aqe << " ) \n" ;

22 Carlos :
, 22 ( - 1 )

,
,
. , <<.
>>. . age
int first_name
" , 11 1 1 ( 11 11 \n 11

s tring >>
( ), :
. >> .
.
. . , ,
.
int main ( )

cout << "Ji , \n" ;


strinq firs t ;
strinq second ;
cin >> first >> second ; //
cout << " , << first << ' ' << second << ' \n ' ;
"

>> .
. .
.
106 3 .

&
" ". .
: ,
. 1 2 ( * ) .
doule, ,
, .

.4.
, ,
, , .
.
int count ;
cin >> count ; // >> coun t
strinq name ;
cin >> name ; // >>
int 2 count+2 ;
= // +
strinq s2 name + " Jr . " ;
= // +
int count-2 ;
= // -
strinq s = n - " Jr . " ; 11 : "_ "

.
, .
,
, , ,
. ,
,
, , :
int aqe = -100 ;

, (
?), ,
.
.

bool char int doule s tring

=
+ +
+

* *
/ /
%
3 .4. 107

bool char int doule s t ring

1 ++ ++
1
n +=n +=n
+=
n - =n -=n
*= *=
/= /=
%=
s s>>x s>>x s>>x s>>x s>>x
s s<<x s<<x s<<x s<<x s<<x
--

!= != != != !=
> > > > >
>= >= >= >= >=
< < < < <
<= <= <= <= <=

.
(
; . 3.9. 1).
. I, .
.
. .
11 ,
int main ( )

cout << " , nJ:11 : " ;


doue n ;
cin >> n ;
cout << " n = << n
"

<< 11 \nn+l " << n+l


=

<< 11 \n n == " << *n


<< 11 \n n == " << n+n
<< 11 \nn " << n*n
=

<< 11 \n n = << n/2


"

<< 11 \n I n == " << sqrt (n)


<< ' \n ' ; //

,
. .
.
; ,
108 .

.
. n
sqrt ( ) .
.
4 . 5 8.5.


. ,
int, douhle.
, sqrt ( ) ,
n doue
sqrt ( ) . , .
, /
, %
-

, 5/2 2 ( 2 . 5 3), 5%2 1 .


* , / % .
int /* + % == .

, ,
23, .
,
:
11
int ain ( )
(
cout << " , \n" ;
string first ;
string second ;
cin >> first >> second; 11
string n = first + ' ' + second ; 11
cout << " , " << n << \n ' ;

+ : .
sl s2 string, sl+s2 ,
s l s2. .
sl "Hello " , s2 "World" ,
-

s l+s2 " HelloWorld" .


.
11
int ain ( )

cout << " , \n " ;


string first ;
string second ;
3.5. 109

cin >> first >> second ; //


if (first = second) cout << " :l'l'\n" ;
if (first < second)
cout << first << " n111 " << second
<< " " << ' \n ' ;
if ( first > second)
cout << first << " " << second
<< " " << 1 \n ' ;


if, 4.4. 1 . 1 .

.5.

, =.
. .
int = ; // 3

: 3

= 4; 11 4
// ( " ")

: 1 4

int = ; //
11 (. . 4)

: 4

: 4

= +5 ; 11 + 5 (. . 9)

: 4

: 9

= +7 ; 11 + 7 (. . 11)

: 11

: 9
110 .

. -.
. "" ,
, +7 .
. .. .
, =+7 .
1 . ; 4.
2 . . 1 1 .
3 . 1 1 .

strinq = " alpha" ; // "a lpha "

: 1 alpha

= "beta " ; 11 "be t a "


// ( "be ta ")

: 1 beta

strinq ; //
// (. . "be ta ")

: beta

: beta

= +" q. " ; / / + "gamma "


11 ( . . "be t a gamma ")

: beta

: betagamma

= a+ " delta " ; 11 a + "del ta "


11 ( . . "be t a del ta ")

: betadelta

: betagamma


" " ".
, :
( ).
( ).
3.5. 111

, ++
=.
int = 8 ; // 8
= 9; 11 9

strinq t = "howdy ! " ; // t


// "howdy ! "
s = "G day" ; // s
// "G ' da y "


. ,
(. int string). - .
, .
, ( )
.
. -
(, ). .
, ,
, , (..
) ("
"), . ,
, ,
.

.5. 1 . :
,
. , ,
,
. ,
. ,
.
.
int ma.in ( )
{
strinq previous " "; // previ o u s ;
// " "
strinq current ; 11
while ( cin>>current) //
if (previous current) //
==

//
cout << " : " << current << ' \n ' ;
previous = current ;
11 2 ',

,
. .
. .
string current ; 11

,
(.. )
while ( cin>>current)

, while, .
4.4.2. 1 . while
, , (cin>>current),
, cin>>current .
,
. , string
>> , .
( .
). Windows
<Ctrl+Z>. - <Enter>. Unix
Linux <Ctrl+D>.
,
curren t (
previous). ,
.
if (previous == current) //
cout << "su : " << current << ' \n ' ;

.
current previous:
previous = current ;

, .
, ,
?
previous :
string previous = " " ; // previ o us ;
11 " "

11 11 (, -
). >>
, .
, while
if (previous == current)

, ( ).
3.6. 11

, "
", .. .
, -
, .

6
,
.
The cat cat j wnped.

.

6

. She she laughed
because what he did did not look very very good good.
? ?
? w ? ,
She she ?
-

.6.
(..
) ++,
. ,

++counter

counter = counter + 1

. , 7, 9
2.
++, :
+= 7 ; 11 = +7
9 ; 11 = -9
*= 2 ; 11 = *2

oper oper=
= oper (. .5).
+=, -=, *=. /= %=.
114 .

. , *= /=
"".

3.6.1 . :
, .
,
. ,
.
int main ( )
{
int nWDer of words = ;
string previous = "" ; //
string current;
while ( cin>>current) {
++nWDer_of_words ; //
if (previous == current)
cout << " " << current
<< <<
" 11
nWDer of words
<< <<
11
.
" ' \n ' ;
previous = current ;

. ,
, :
++nWDer_of_words ;
, 1 , - 2 ..
:
nWDer of words += 1 ;
:
nWDer of words = nWDer_of_words+l ;
++numer_of_words
.
,
3.5. 1 . , 3.5. 1
.
: - ,
, -
. " ",
.
.
3 . 7 . 11 5

3 . 7 .
,
. ++?
++
, ,

numer of elements
Fourier transform
z2
Polyqon

:
2 //
tie$ to$arket // $ ,
-

Start menu // - ,

,
, . ++
.
.
, ,
, _foo.
;
. ,
.
; ,
,
-

. ,
.
#include " s td lib facilities . h "
int &in ( )
{
SRING s " , ! " ;
"'

<< <<
cOut S ' \n ' ;

, , ,
one One, ;
-

, .

&
", !"

. ? -
116 3 ,

?
? .
, , ( )
.

++ .
.. 1 .
, , ..
.
int if = 7 ; / / : "i f" -

,
(, string), .
,
.
int s trinq = 7 ; //

, .
, ; ,
, , -
. .
, xl, 2 . s
7 . ,
.
, ,
:
.tf
TLA
w
NV

, . ,
i, , ..
(.
4 . 5 8.4), i - (. 4.4.2.3).
; ,
. , .
:
partial_swn
element count
stale_yartition

:
.. 1 117

the nuer of eleents


reaininq free_slots_in_symol_tae
_

, element_count,
elementCount ElementCount.
, ,
ALL_CAPITAL_LETTERS,
(. 27.8 . 1 7.2),
.
, Square Graph. ++
,
int string, Int String. ,

.
, ,
, :
Name names nameS
foo fOO fl
fl fI fi

(). ( ), (
), 1 (), 1 ( L), I ( i).

..
++
.
. ,
.
.
u ,
.
,
-

.
,
-

.
- .
- ,
.
- , .
11 8 3 .

,
.
int , 7, 42 -399.
string ,
" Interoperaility" , " tokens : ! @ f$ % " &* " "Old acDonald had farm" .
:
int 7; : 1 7

int 9; : 1 9

char = '' ; :
[!]
doue = 1.2; :
1 1 .2

strinq sl " Hell o , World ! " ; s1 : 1 13 Hello, Worldl

strinq s 2 "1. 2" ; s2 : 1 3 1 .2


s tring ,
int, string .
. doue ,
string - . , 1 . 2,
s2 - : ' 1 ' , ' . ' ' 2 ' .
.
int : .
int
.
4 (32 ).
l , char doue.
l char, , (8 ),
doue - 8 . ,
. ,
char , int,
string doue. int
char , .
, , ,
.
: :
. , ,
, ,
.
. 12 . 5? .
12 . 5 . , 12 . 5 12 . 5 . ,
12 . 5 , .
,
120, int,
' ' , char.
. 9 . 119

string, ,
.
( 1
).

1 00000000 00000000 00000000 01 1 1 1 000


, (),
int (120) char ( ' ' ,
8 ). - ,
. 1 . ()
.2. 1 . 1 .

. 9 .
. -
-
(type-safe),
, . , .
.
,
.
int main ( )
{
doue ; // ,
//
doule ; //
doule z 2 . + ; // + z

11


.
!
- - (,
).
- .
.
, .
. , ++
,
,
.
, .
1 20 3 .

. ,

. , ,
, ,
++.
,
,
.
.
. ,
.

. 9 1
. .

3.4 ,
char doue int.
++ .
char int,
int - doue. .

char ' ' ;


int il ;
int i2 '' ;

i l i2 120, .. 8- ASCil-oy
' '
.

. char
int , :
. .
int, char .
char 2 = il ;
cout << << ' ' << il << ' ' << 2 << ' \n ' ;

:
120

-
( doue)
- .
l char
bool int
bool doue
char int
char doue
int doue
. 9 . 1 21

in t
doue,
.
doue dl = 2 . 3 ;
doue d2 = d1+2 ; // 2 2 . 0
if (dl < ) // .
cout << "dl " ;
-

int
doue ( )
. .

3. 9 .2.

. , ++
() .
,
, .
.
int ain ( )
{
int = 20000 ;
char = ; // i n t
// ch a r
int = ;
if ( ! = ) // ! = " "
cout << " ! : " << << " ! =" << << ' \n ; 1

else
cout << " ! char\n" ;

"",
, ("")
. ,
char
int. , int, ,
char, ( )
int, char.
.
int ain ( )
{
doule d = ;
while ( cin>>d) { // ,
11
int i = d ; // do u e i n t
1 22 .

char = i ; // i n t cha r
int i2 = ; // ch a r
cout < < 11 d=11 < < d // d
<< " i= 11 << i // i n t
<< 11
i2== 11 << i2 11 ch a r
<< 11 char ( 11 << << 11 ) \n" ; //

while,
, 4.4.2. 1 .

&
. .
(, 2 );
(, 127; , 1000); ;
56, 89, 128; (, 56 . 9 56 . 2).
doue int int char
, , (
) .

, ""
. , ,
-.
, .
doue int
doue char
doue l
int char
int bool
char bool
,
, ,
. ? -
, .
.
doule 2 . 7 ;
// -
int ; // 2

,
douhle, , doue
int ( ).
: .
. 9 . 1 23

int char -
int, char .
char
. char
1 . int - 4 .
char :
int :
11
, ,
1 . char :
"". .
int = 1000 ;
char = ; // -24

int char.
char .
char
[-128, 127J,
[0,127J, .
char,
[0,255J .
?
: ++
. .
++ .
.
,
,
. , "
. ". .

,
.
-
.
++ 1 1 .
. . ( )

:
doue { 2 . 7 } ; 11 !:\.
int { } ; 11 : doue -> i n t
int ( 1000 } ; 11
char ( } ; 11 : i n t -> cha r
1 24 3 .

.

, :
char { 1000 } ; / / : (
// - cha r)
char 2 { 4 8 } ; //

, ,
? { }
, ,
,
.
5 .6.4 7 . 5 .
(universal and
unifonn initialization). .



, , .
, .
1 . ,
.
3. 1 ,
", first_name", first_name - ,
.
: " "
" first_name , " (
!).
2. . " ?
. " . ,
.
- .
3.
friend_name.
: " friend_name?" .
4. char friend_sex
. m,
- , f .
-

friend_sex . if
.
3 . 9 . 1 25

- , " friend_
name, , ".
- , " friend_
name, , ".
5.
age, int.
" ,
age ". age
1 1 0, simple_error ( " 'l'
! " ) , simple_error ( )
std lib faci l ities . h.
6. .
1 2 , "
age+ 1 ".
1 7 , "
".
70 , " ,
".
,
.
7. " ",
.


1 . ?
2. ?
3. ,
numer, ,

?
4. \n ?
5. ?
6. ?
7. ?
11 .
cout " , '

cout first_name ;
cout " ! \n " ;

8. ?
1 26 .

9 . ?
1 0. ?
1 1 . ?
1 2 . char, int doue?
1 3 . , -
, int string?

1 4. = =?
1 5. ?
1 6. ?
1 7 . ++?
1 8 .
++? , , .
hi_l ittley ig hi 1 i fine 2_For_l_pecial
latet thing the_$ 12_ethod thi i ok
iniineine nuer correct?
1 9. .
, .
20. .
2 1 . ?
22. doue int
?
23. , ,
.

cin

1 . .
.
2. ++ ,
.
. :
1 .609 .
3. 9 . 127

3. . .
(
, int doule = ; ). .
4. .
. int
val l val2 . .
, , ,
.
5. ,
doule.

. ?
? ?
6. .
, ,
. , 10 4 6.
4, 6, 10. .
. .
4 5 4, 4, 4, 5.
7. . 6 . ,
Steinbeck, Hemingway, Fitzgerald,
Fitzgerald, Hemingway. Steinbeck.
8. .
. . , .
,
"" "". , "
4 JJ " . : .
3.4.
9. , "", ""
. 2 .. ,
.
, 1 , 2, 3 4.
- , "r ! " ,
" r ! "
1 0 . ,
.
:
+ 100 3 . 14
* 4 5
1 28 3 .

operation ,
if, ,
, if (operation= " + " ) .
doue. +, -, * /, plus, minus. mul
div, .
1 1 . .
1 -, 5-, 1 0- , 25-, 50- .

, " ?"
.
23 w .
17 w .
14 w .
7 w .
3 w .
573 .
:
, . ,
"1 4 " "1 " ( " 1
") . ,
, .. 5 73 . 573 .


. -
.

(. 6 9 11-IV).

.
, ,
( ).
() ,
(
)
(). -
,
.

, vector,
.
1 30 4

4. 1 . 4.5 .
4.2. 4.5 . 1 .
4.3. 4.5.2.
4.3.1 . 4.6.
4.3.2. 4.6. 1 .
4.3.3. 4.6.2.
4.4. 4.6..
4.4. 1 . 4.6.4.
4.4.2. 4.7.

4. 1 .
- ; .
- - .
, , ,
( t "" .
- -

. .) ,
.


(
)

, ,
, ,
. " "
: ,
, ,
.. .

, (data stru
ctures) (states) . , ,
,
.
, ,
. ,
-
.
(, )
(, ).
4.1 . 1 31

,
,
, .
.
:
, , , .
.
, .
. . . .

- " " "
".
:
.
. .



lj jj !!

"1/0" "-".
.
, ,
(, )
.
, ,
r (,
). ,
(, ),
, (,
).
"" "", ,
,
, ,
, .
, ,
, , .
-

wn.
,
.
(), 4 9, (),
1 32 4

7 . () square
(. 4.5). , 1 950-
() ClllA .
. . .
(
). .

4 . 2.
- .

;
;
.
, : ,
, .

,
. ,
, ,
.. - -
. , ,

,
. ? ?
? ,
? ? ,
?
. (
)
.
,
,
-
; . ,
. ,
, ;
.
,
.
,
4.2. 1 33

: .
. ,
,
,
.
-
, , -

. .
. ,
(
) . .
(
)
sort ++. ,
, - , ,
sort () , -
: sort ( ) sort (
2 1 .8 .5.4), std library . h.
.
,
,
( 3.2), vector (
4.6, 1 7- 1 9), map ( 2 1 ) . .
" " .
. ,
, : ,
.
.
? ,
, , ,
. ,
. ,
, . ,
,
, .
, , ,
1 , 1
, 1 00 ,
,
1 00 . , , 1
, " "
1 34 4

. .

.
. ,
, .
, ,

. ,
,
.
.
; .
. ,
, ,
++,
.
. , -
-.
.
.
,
: .
.
? (. , )
, .
.
. .
.
-
. - ,
"",
.
(
).

. . ,
. . ,
( ).
. .
.
. .
4 .3. 1 35

4. .

.
.
. ' ' , . 14
"Norah " .
. -
. . .
// :
int lenqth 20 ; = // ( -
11 )
int width 40 ;=

int area = lenqth*width ; //

20 4 0
length width, .
; , length
width. " length"
", length".
.
lenqth = 99; // l ength 9 9

length,
, " length",
: " 99 length".
length,
( "lvalue length" "
length") (
"rvalue length", " length"
" length").
. .
int :
length : 1 99

, length - int,
99. ( lvalue) length ().
( rvalue) - ,
.
, + ,
, , .
.
int perimeter = ( lenqth+width) *2 ; //
1 36 4

:
int perimeter = lenqth*2+width*2 ;

:
int perimeter = lenqth+width*2 ; 11 wi dth *2 l ength

,
. perimeter,
.
, .
, - .
,
, length+width *2
length+ (width*2) . a*b+c/d (*) +
+ ( c/d) , * (+) /d.
.5.
: W ,
".
, a*b+c/d.
, (*) + (c/d) ,
.
?
, , , ,
.
, .
.
, , .

a*b+c/d* ( e-f/q) /h+7 //

,,. 1 .
, , .
,
", -
2 . 54. ,
(, pi, . 14 1 59).
. ++
, .. ,
.
.
4 .3. 1 37

constexpr doule pi = 3 . 14159 ;


pi = 7 ; // :
int v = 2 *pi/r ; // : , pi

.
. . , ,
3 . 14159 "",
299792458? , .
"" 1 2 , .
, . 1 4 , -
"" 22/7, ,
, .
pi, .
constexpr doule pi = 3 . 141592653 5 9 ;

,
( , 1).
-
. (
) .
, case ( .
4.4. 1 .3), ++
. .. ,
. .
constexpr int = 1 7 ; //
int val = 1 9 ; 11
max+2 //
11 ( )
va1+2 11 :
// .

, 2 99792458
-

, ,
. . , ,
.
"" !
constexpr ,
. :
constexpr int = 100 ;
void use ( int n)
{
constexpr int cl = +7 ; // : cl 1 0 7
constexpr int 2 n+7 ; // : 2
11 . . .
1 38 4

. ""
, ,
. ++
(cons t) :

constexpr i n t 100 ;
void use ( int n )
{
cons texpr int cl = max+7 ; // : cl 1 0 7
const int 2 = n+7 ; // ,
11 2
11 . . .
2 7 ; // : 2 con s t

" "
.
++98 cons texpr,
const.
"", (
),
,
.

4. .2.
.

.
, .
.


f () f
++ lval

- - lval

! bool
-
*
/
%
+
4. . 1 39


-
out<< out out - ostream

inb in in - istream

< bool

<= bool
> bool
>= bool
== =
! = bool
&& bool

al lb bool
lval=a =
lval*=a lval=lval *; / , %,
+-

, .
lval ( ",
"). .5.
& & (). 1 1 () ! ()
5 . 5 . 1 , 7 . 7 , 7.8.2 1 0.4.
, << ( <) <,
< l , ..
true, false. , << ,
true<c. fal se<c.
, << "
?", ( )
. , <<
.
, , - ,
.
:
++
a+=l
a=a+l
? ? ,
, ++,
. ,
(
).
, .
1 40 4

, .
a=a+l , ,
. ,
a=b+l, =+2 =-1;
++,
. , ,

, . ,
,
a=a+l ++, ,
.
*= scale, = a* scale.

4. . .
"" . . 2 . 5/2
doue int.
? : ?
; , 5/2 2.
,
; , 5 . 0/2 . 2 . 5. ,
" 2 . 5/2:
?" : " , ;
".
1 . 25, 1 , 1 . 25 . (
) : doule,

doule; ,
int. :
5/2 2 ( 2 . 5)
2 . 5/2 2 . 5/doule ( 2 ) , .. 1 . 25
' ' + 1 int{ ' ' } + 1
type (value) type { value } " value
type. type
value". ,
("") int doule,
char - int. .

, :
doule d = 2 . 5 ;
int i = 2 ;
doule d2 = d/i ; // d2 1 . 25
4. 4. 1 41

int i2 = d/i ; 11 i2 == 1
int i { d/i } ; 11 : do ue -> i n t
11 ( 3 . 9 . 2)
d2 = d/i ; 11 d2 1 . 25
i2 d/i ; 11 i2 == 1

:
, .

: f= 9/ 5 + 2. :
doule dc ;
cin >> dc ;
doule df = 9/5*d+2 ; // !

, ,
: 9/5 1 , 1 . 8,
. , 9,
5 ( ) doule.
doule dc ;
cin >> dc ;
doule df = 9 . 0/S*dc+32 ; //

4 . 4 .
,
4.3. ,
? -
? ,
?
? ++, ,
,
(statement).
:
.
, , :
= ;
++ ;

, .
, = , = - ,
-

= ; ;
. ?
. .
= ++ ; // :
1 42 4

, :
=++ ; ; = ; ++ ; .
. , "
!" ?
, . ,
, : " , !"
,
. .
int = 7 ;
cout << << ' \n ' ;

.
. - (
). , ,
. .
1+2 ; // ,
* ; // ,


,
. , , ,
, -
.
: .
:
if ( == 5) ;
( = ; }

, , .
, -, . ,
, ++ -
. , .. ,
. , ,
.
,
.
, ?
, 5.
, ().
,
. 5,
(
) (
, , 5). ,
4.4 . 1 43

if :
.
, ,
.
.
, ,
.

4.4. 1 .
, ,
. ++ if swi tch.

4.4. 1 . 1 . if
++
if, .
.
int main ( )

int = ;
int = ;
cout << " , wr \n " ;
cin >> >> ;
i f ( <) //
11 1 -
11 (, ) :
cout << "lllAX ( " << << " , " <<
<< " ) " << <<" \n" ;
else
// 2 -
11 (, ) :
cout << " ( " << << " , " <<
<< " ) " << << "\n" ;

if .
. ;
. .
.

,
. , , ,
, , ,
, : "
, , ,
" . ++ - :
1 44 4

if ( traffic_liqht==qreen) qo ( ) ;
if ( traffic_liqht==red) wait ( ) ;

, ,
. (
, #include).
11
11 ' i ' ' '
in t ain ( )

constexpr doule c_per_inch 2 . 54 ; //


doule lenqth = 1 ; 11 ( )
char unit = ;
cout << " , "
<< " ( i) : \n " ;
cin >> lenqth >> uni t ;
if (unit -- ' i ' )
cout << lenqth << "in == 11
<< c_per_inch*lenqth << " \n" ;
else
cout << lenqth << " == 1 1
<< lenqth/c_per_inch << " in\n" ;

,
: . lin = 2 . 54cm; 2 . 54,
2 . 54cm = = l in. -
.
,
. .
. uni t= ' i '
' i ' .
' ' .
. 15f () ",
. "? (uni t ' i ' ) ,
-

else ( ).
. . , ,
' f ' .
,
- - - -
. .
.
.
//
11 ' i ' ' ' ,
11
int ain ( )
4 .4 . 145

constexpr doule cm_yer_inch = 2 . 54 ; //


doule lenqth 1; = // ( )
char unit ' ' ;
= // -
cout << 11 , 11
<< 11 ( i) : \n 11 ;
cin >> lenqth >> unit ;
i f (unit 'i')
cout << lenqth << 11 in == 11
<< cm_yer_inch *lenqth << 11 \n 11 ;
else if (unit '' )
==

cout << lenqth << 11 cm == 11


<< lenqth/cm_yer_inch << 11 in\n 11 ;
else
cout << 11 , JD, 1 11
<< unit << 11 1 \n 11 ;

uni t= ' i ' , uni t== ' ' .


,
" , . " . ,
. .

"else-if" , ++ .
if. if
:
if ( ) else

, if ,
, else .
-

if else if:
if ( ) else
if ( ) else

:
if (unit 'i')
==

. . . 11 1 -
else if (unit '')
==

// 2 -
else
11 3 -


.
, , .
,
. ,
, .
1 46 4


,
, .
, .

4.4. 1 .2. swi tch


uni t ' i ' ' '
: ,
.
, ++
: swi tch.
int ma.in ( )

constexpr doue cm_yer_inch 2 . 54 ; //


doue lenqth = 1 ; // ( )
char unit = ' ' ;
cout << " . , "
<< " ( i) : \n" ;
cin >> lenqth >> unit ;
switch (unit) {
case ' i ' :
cout << lenqth << " in = "
<< cm_yer_inch*lenqth << " cm\n" ;
break ;
case ' ' :
cout << lenqth << " cm = 11
<< lenqth/cm_yer inch << " in\n " ;
_
break ;
default :
cout << " , , 1 ' "
<< unit << " ' \n" ;
break ;

swi tch ,
if,
. ,
swi tch, .
case. case,
case. case -
break.
case, ,
defaul t. , , -
. , ,
4.4 . 1 47

4.4.1 .3. swi tch



, swi tch.
1 . . .
, char ( 9 . 5).
, string .
2. case
( 4.3. 1 ) . . case
.
3. case .
4. case.
5. case break.
, ,
.
.
int ain ( ) //
{ // . .
cout << " JD pwy? \n" ;
strinq s ;
cin >> s ;
switch ( s ) 11 : ,
case " " : !/
11 . .
break ;
case "" :
11 . .
break ;


if map (
2 1 ).
swi tch
.
if.
, case
. .
1 48 4

int ain ( ) // ca se
11 :
int = ' ' ; / /
constexpr char n = ' n ' ;
constexpr char m = ' ? ' ;
cout << " ? \n" ;
char ;
cin >> ;
switch ()
case n :
11 . .
break ;
case : 11 : ca se
11 . .
break ;
case m :
11 . .
break ;
case ' n ' : 11 : ca se
11 ( ' ' )
11 .
break ;
default :
11 . .
break ;

switch
.
. .
int ain ( ) //

cout << " , \n" ;


char ;
cin >> ;

switch ( )
case ' ' : case ' 2 ' : case ' 4 ' : case ' ' : case ' ' :
cout << " \n" ;
break ;
case ' 1 1 : case ' ' : case ' 5 ' : case ' 7 ' : case ' 9 ' :
cout << " \n" ;
break ;
default :
cout << " \n" ;
break ;
4 .4 . 1 49

, swi tch,
case break.
.
int ain ( ) // ( brea k )
{
constexpr doule c_per_inch = 2 54 ; //
.

doule lenqth 1 ; //
char unit ' ' ;
cout << " , "
<< " ( i) : \n" ;
cin >> lenqth >> uni t ;

switch (unit)
case ' i ' :
cout << lenqth << "in == "
<< c_per_inch*lenqth << " \n" ;
case ' ' :
cout << lenqth << " == "
<< lenqth/c_per_inch << " in\n " ;

. ,
case ' i ' , "" case
' ' , 2i
:
2in == 5 .
2 == 0 . 787402in



. swi tch.
. ,
? ?

4.4.2.
- .

. (
,
) .
1 50 4

4.4.2.1 . while
,
(EDSAC).
(David Wheele)
(Cambridge Univesity, England) 6 1 949 .
.

1 1
2 4
9
4 16

98 9604
99 9801

,
( ' \ t ' ) .
++ :
// 0 - 9 9
int ain ( )
(
int i = ; 11
while ( i<lOO ) (
cout << i << 1 \t ' << square ( i ) << \n ' ;
++i ; // i (i i + l )

square ( i ) i . ,
4.5, , .
,
++, .
.
, 1 00, .
.
,
( ' \ t ' ) .
.

, .
().
,
( .
) . int
i .
4.4 . 1 51

( - ).
(
1 00 ).
, ( ).
while.
while , .
while ( i<lOO) // , i
{
cout << i << ' \t ' << square ( i ) << ' \n ' ;
++i ; // i (i i + l )

11 ( ).
-

i .
i<lO O .
,
. , . . i 1 0 0 ,
while , .
,
.
while
, while.
, .
. ,
( - "
i "),
. !
,
.
.
int i = ; //

.
, .
.
,
, .


' ' char ( ' a ' +l ) , ' ' char ( ' a ' +2 ) ..
,
.
152 4

97
98

z 122

4.4.2.2.
, ,
.
while ( i<lOO) {
cout << i << ' \t ' << square ( i ) << 1 \n ' ;
++i ; 11 i (i i + l )

,
(( }). . -
. { }
,
. .
if ( <=) 11
}
else { 11
int t = ;
= ;
= t;

4.4.2.. for

++. .
.
for while. .
,
. " :
11 0 - 9 9
int main ( )
{
for ( int i = ; i<lOO ; ++i )
cout << i << ' \t ' << square ( i ) << ' \n ' ;

, i ,
, .
i 100". for
while.
4 .4. 153

for ( int i = ; i<lOO ; ++i)


cout << i << 1 \ t ' << square ( i ) << 1 \n ' ;

int = ; // for
while ( i<lOO) 11 for
// for
cout << i << ' \ t ' << square ( i ) << ' \n ' ;
++i ; 11 for

while,
for. for
. for -
, .
while . .
for.

. .
int main ( )
{
for ( int i = ; i<lOO ; ++i) { // i { 0 , 1 0 0 )
cout << i << 1 \t ' << square ( i ) << ' \n ' ;
++i ; // ? !

. . ,
1 00 . . ++i
i ,
50 .
, . .
for while.
, 2, :
11
11 { 0 , 1 0 0)
int main ( )
{
for ( int i ; i<lOO ; i+=2)
cout << i << ' \t ' << square ( i ) << ' \n ' ;

. ,
. .
1 54 4


, "" ,

. for. .
.

"for ",
, vector; . 4.6.

4.5.

square ( i ) . . , ,
i. -
. (
).
,
,
sqrt ( ) , 3.4.
. square.
int square ( int ) //

return * ;

, (
), square,
int ( ) int (
); ,
:
int main ( )
{
cout << square ( 2 ) << ' \n ' ; 11 4
cout << square ( l O ) << 1 \n ' ; // 1 0 0

, ,
, .
.
square ( 2 ) ; //
11 :
int vl = square ( ) ; //
4 .5. 155

int v2 square ; //
int v = square ( l , 2 ) ; 11
int v4 = square ( " two" ) ; 11 - i n t


. . . -
. , , ,
" two" . 2.
++ .
,
++. .
. . -
- .
. .
.
(. 4.4.2.2),
.

return ; //

square :
.
++ . . .
,
.
:
wn ( _ ) _
( ) wn (
). - . -
(. ). ,
, .
- ( ).
. .

void ( ""). .
void write_sorry ( ) // ;
{ / /
cout << " \n" ;

.
, 8.
1 56 4

4.5.1 .
,
,
.
.
( -
).
.
.
,
. .
,
. , ,
(.. )
. , .
,
. , , ,
* 7 * 7 , (+7) * (+7) , square () , square ( 7 )
square (+7 ) , , square .
( ++
sqrt): sqrt ()
sqrt ( 7 ) , sqrt ( x+7 ) , (
) .
, . : ,
.
, sqrt () .
8.5 ,
, .
main ( ) .
:
void print_square ( int v)
{
cout << v << ' \t ' << v*v << ' \n ' ;

int main ( )

for ( int i = ; i<lOO ; ++i ) print_square ( i ) ;


print_square ( ) ? , ,
, square ( ) , , ,
4.5. 157

print square ( )
,
square ( ) , ,
;
square ( ) ,
print_square ( ) , , .
print_square ( ) :
;
.
,
. square ( )
.
,
square ( i ) , i * i .
.
,
1 949 .
"".
i * i
.
. .
( (subroutine))
.
!


square ( ) ;

. *
( . ,
). "".
square ( ) .

.5.2.
, . .
? .
int square ( int )

.
int = square ( 4 4 ) ;
1 58 4

.

. ,
sqrt ( ) ? ,
. , square ( ) ?
, .
, -
. , ++
, ,
. r.
int square ( int) ; 11 square
doule sqrt (doule) ; 11 sqr t

,

.
int square ( int ) 11 square

return ;

. ,
. -
#include.
. " " 8.3 8.7.

.
(
4 . 2).

4.6.
- ,
, . .
,
. . . ,
.
, - ..
,
.
( ,
20 2 1 ) . , .
: vector.
4.6. 159

- ,
. ,
vector v.
size( )

v: 6
v[O] v[1 ] v[2] v[] v[4] v[5]
v 5 7 9 4 6 8

, ; -
1 .. ,
, v [ O ] 5,
v [ l ] 7 ..
. :
++ -
. , ,
, " ", . .
.
, , :
vector<int> v = ( 5 , 7 , 9 , 4 , 6 , 8 } ; // 6

, ,
.
vector (<>). int.
.
vector<strinq> philosopher //
= { "ant" , " Plato" , " Hume " , "ierkeqaard" } ;

, ,
.
philosopher [ 2 ] 9 9 ; // :
v [2 ] = "Hume " ; 11 :

vector ,
. ( n ) , n -
,
. :
vector<int> vi ( ) ; // ve c t o r 6 in t ,
vector<strinq> vs ( 4 ) ; // ve c t o r 4 ,

(11 11) .
,
:
vd [20000] = 4.7; //
1 60 4

4.6. 1 .
vector "" ,
:
vector<int> v = ( 5 , 7 , 9 , 4 , 6 , 8 } ;
for ( int i=O ; i < v . size ( ) ; ++i )
cout << v [ i ] << 1 \n ' ;

v . size ( ) v .
v . size ( ) vector
.
v [ O , v . size ( ) ).
.
v v [ O ] , - v [v . size ( ) -1 ] . v . size ( ) =,
v , .. .
++
++ ( 1 7.3, 20.3) .


, , , :
vector<int> v = ( 5 , 7 , 9, 4 , 6 , 8 } ;
for ( int : v) // v
cout << << ' \n ' ;

for ,
(range)
. for ( int v) " int v " ,

[O,v . size ( ) ) .
.



for ( 4.4.2.3).

4.6.2.

.
push_back ( ) ,
4.6. 1 61

, .
.
vector<doue> v; // ,
11 v

v: 11
v . push_back ( 2 . 7 ) ; /! v 2 . 7
// v v [ 0 ] ==2 . 7

v: [!)3---@
v . push_back ( S . ) ; // v 5 . 6
11 v v [ l ] == 5 . 6

v: 1 2 1 ---+--1 2.1 l I
s.e

v . push_back ( 7 . 9 ) ; 11 v 7 . 9
11 v v [2 } == 7 . 9

push back ( ) .
_

. r- push back ( ) -
_

vector, .
. -:
_. u_u_ ( u_ )
, -
vector - size ( ) , v . size ( ) ,
.

, push k ( ) 3.
_

, ,
vector .
() ,
.
, vector ++
.

4.6..
.
, . -
. -" ,
, , ,
, ,
. .
, .
1 62 4

- , -
.
, .
//
int main ( )
{
vector<doule> temps ; 11
for ( doule temp ; cin>>temp ; 11 temp
temps . push_back ( temp) ; 11 t emp vector
11 . . . - .

, ?
:
vector<doule> temps ; /!

.
du.
.
for (doule temp ; cin>>temp ; 11 t emp
temps . push_back ( temp) ; 11 temp vector

temp doue
. cin>>temp doule,
( ).
. ,
for
cin>>temp. cin>>temp ,
, ,
for doule,
. .
1.2 3.4 5. 7.8 9.0 1

temps : 1 . 2. 3 . 4 . 5 . 6, 7 . 8, 9 .
( , temps [ ] =1 . 2).
' 1 ' , .. . doule.
1 0.6
.
temp
, for, while:
doule temp ;
while ( cin>>temp) //
temps . push_back ( temp) ; //
11 t emp
.6. r 1 63

, for , ,
, ,
.
, .
.
11
int ain ( )
{
vector<doule> teps ; 11
for (doule t ; cin>>temp ; 11 t emp
teps . push back ( temp) ; 11 t emp
_

// :
doule sum = ;
for ( int : temps ) sum += ;
cout << " : "
<< sum/ temps . size ( ) << 1 \n ' ;

11 :
sort ( teps ) ; //
cout << " : "
<< temps [ teps . size ( ) /2 ] << ' \n ' ;

,
(.. temps . size ( ) ) .

// :
doule sum = ;
for ( int : temp s ) sum += ;
cout << " : "
<< sum/temps . size ( ) << ' \n ' ;

, +=.
(,
, - )
.
sort ( ) .
11 :
sort ( temps) ; 11
cout << " : "
<< temps [ teps . size ( ) /2 ] << ' \n ' ;

( 20).
:
, .. temps . size ( ) /2.
(
), ,
1 64 4

.
. 2 .

4.6.4.
.
,
- , , - , ,
- ,
.
, ,
( ;
. 2 1 ). .
11 :
int ain ( )
{
vector<strinq> words ;
for ( strinq temp ; cin>>temp ; 11 ,
11
words . push_back ( temp ) ; 11
cout << " : " << words . size ( ) << ' \n ' ;

sort (words ) ; //
for ( int i = ; i<words . size ( ) ; ++i )
if ( i==O 1 1 words [i-l ] ! =words [ i ] ) // ?
cout << words [ i ] << "\n" ;

,
. , ,

n plan canal n

canal
n
n
plan

? , ?
for ( strinq temp ; cin>>temp ; 11
words . push_back ( temp) ; 11

( 4 . 6. 2) .
- , .
,
4.6. 1 65

() . , ,
"". 3.5. l ,
Windows <Ctrl+Z>.
Unix - <Ctrl+D>.

. "
" ( r
) " ".

if ( i==O 1 1 words [i-l ] ! =words [ i ] ) // ?

. :

canal
man
pana.ma
plan

,
. ? ,
(words [ i -1 ] ! =words [ i ] ), ,
, , . ,
(i==O),
1 1 ().
if ( i==O 1 1 words [i-l ] ! =words [ i ] ) // ?

, .
! = ( ), == (), < (), <= (
), > () >= ( ),
. <. >
, " " "Apple"
"Chimpanzee" .

&
. "" ;
, cin
cout, BLEEP.
,
string disl iked = "Broccol i " ;
, .
1 66 4

4. 7 .

, :
( for while), ( if),
( ++ +=), (=, ! =
1 1 ) . (. main ( ) , sort ( ) size ( ) ) .
, .
vector ( ). cout ( ) sort ( ) ().
, ,
.
,
,
. : -
. ,
,
,
.


.
. ,
- , .
1 . . while,
int.
' 1 '

2. . "
: " ,
- " : "
.
3. . " " ,
.
4. , doule,
int.
5. . "
" , 1 . 0/100.
6. .
doule .
,
.
4. 7. 1 67

.
, " " .
,
" " .
7. doue
; , , l Ocm, 2 . Sin, Sft
3 . m. : cm, m, in,
ft. : lm =lOOcm,
lin =2 . 54cm, lft == 12in.
. 12 m (
) 12m ( ).
8. , yard,
meter, km gallons, .
9. (
) . ,
,
, .
, . .
( ).
1 . ( )
, .
1 1 . ,
.


1 . ?
2.
? .
3.
?
4. ?
5. ,
?
6. lvalue? , lvalue.
, lvalue?
7. ?
8. ?
9. ?
1 68 4

1 0 . "" ? .
1 1 . . .
.
1 2. ,
?
1 3 . string?
1 4. swi tch if?
1 5. swi tch?
1 6. , for
?
1 7. , for. - while?
1 8 . ( char)?
1 9. char foo ( int ) .
20. , ?
.
2 1 . int,
s tring?
22. string, -
int?
23. ?
24. for, ?
25. vector<char>alphaet ( 2 6 ) ; ?
26. - push back ( ) .
27. - begin ( ) , end ( ) size ( ) .
28. vector?
29. ?

11
begin ( )
else
end ( )
lvalue
push back ( )
_

rvalue -
size ( )
sort ( ) i f for
4. 7 . 1 69

vector swi tch for


while

1 . . .
2. , ",
,
- ". 4.6.2 ,
. :
.
3. doule .
,
, .
( ).

.
.
4. , r .
1 1 00, ,
, (, "
50").
. :
< <=. if-else.
5 . , r
.
- , ,
.
: doue .
35 . 6, 24 . 1 ' + ' ,
" 35 . 6 24 . 1 5 9 . 7 " . 6
.
6. , " zero " , "one " ,
" . , "nine " . ,
: ,
7 seven.
, ,
: , seven
7 .
1 70 4

7. -, . 5, .
. ,
.
8. ,
.
. -
. - ,
64 . .
.
! . .
. 1 ,
1 1 . ,
. . ,
int. .

.
,
.
9. ,
. 8. .
. int,
doue. .
(
int). .

( doue)?
1 0. ". . '' .
.
Google ( -
) .
swi tch. , (..
,
) .
.
.
. .

.
.
1 1 . . 1 100.
. .
(.. ,
4. 7 . 1 71

). ,
(, primes, primes [ 0 ] =2 ,
primes [ 1 ] =3, primes [2 ] =5 ..). 1
1 00.
. .
.
primes. 2.
1 2. . ,
max, 1 max.
1 3 . . . l 1 00.
"
". ,
. . .
1 4. . . , ,
max,
1 max.
1 5. , n
n .
1 6. .

. ,
, . .
. .
1 7 . ,
, . s tring.
1 8 . .
2 + + = 0 .
, .
, ,
. ,
doue.
, , xl 2 .
1 9. , ,
, Joe 17 Barbara 22.
names , - scores
( . names [ 7 ] == " Joe" .
scores [ 7 ] =17 ) . . NoName .
. .
. , .
( . ) .
1 72 4

20. . 1 9 ,
"
" .
2 1 . . 1 9 ,
,
, " " .


, . - ! ,
,
. . ,
, :
, ( ),
, ,
. .
(
). .
, .. ,
. ,
, .

51 1 &- .#U-

;;;'/'

.. fu. "'k.
- ( IJ', f'I-'1)

,
,
. ,

, - .
? !
,
,
. ,
" , "

.
1 74 5

. 1 . 5.6.
5.2. 5.6.1 .
5.3. 5.6.2. ,
5.3 . 1 .
5.3.2. , 5.6.3.
5.3.3. 5.6.4.
5.4. 5.7.
5.8.
5 .5. 5.9.
5.9. 1 .
5.5 . 1 .
5.10. -
5.5.2. 5.10.1 .
5.11 .
5.5.3.

5. 1 .
, ,
, , .
.

.
.
.
. ,
.
, :
+ ;
+ , .
. ,

.
11. ,
.
:
+ , (
/ );
+ , (,
++);
5.1. 1 75

, .
. ,
.
, -
. , ,
.
, " ". ,

.
? .
,
, , .
, ,
,
,
. -
: ?
, ,
.
1 .
.
2.
.
3.
.
4.
.
5. .
. 3-5 ,
. 1 2
,
- , .
1 00%, .
.
, . ,
,
90% . ,
,
. ,
, . .
1 76 5


.
.
.


.
, .

; .
, .. , ,
,
. , ,
. ,
,
.

5.2.
.
. ,
,
" " ,
(. .
,
) .
.
, . - .
.
. .
, ,
, , ,
- 1 . 2: sqrt ( -1 . 2) .
sqrt ( )
doue doue.
.
5.5.3.
. . ..
( r, ,
, ..).
. ,
5.3. 1 77

, .
,
"!"? 5.6.3 1 0.6.
.
(""),
.
, vector
.
?
. 26.3.5.
. .
, ; .
6.6 6.9.
.
.
,
.
. ,
,
. .

5.3.
.
. .

. ,
.
. , ,
" ".
.
ru .
.
- -
.
.
:
int area ( int lenqth , int width) ; 11
1 78 5

5.3. 1 .
, area ( ) :
int sl area ( 7 ; 11 : )
int s2 area ( 7 ) 11 :
Int s area ( 7 ) ; 11 : In t -
1
int s4 area ( ' 7 ) ; // :

: .
++,
. ,
. , . ,

, , .
( ,
, !)
, ,
, .
, ,
,
.
, ,
,
, , . .
s,
- :
" int:
i . "
, :
: ' ; ' 's'"
" 's' "
'Int"'
,
. ,
. ,
- . ,
:
s , -
Int, s."
.
5.3. 1 79

5.3.2. .
,
, ; ,
, ( ,
) ,
, ,
..
int = arena ( 7 ) ; // :
int xl area ( 7 ) ; //
:
//

int 2 area ( " seven" , 2 ) ; /!
:
11

.
1 . arena ( 7 ) : area
arena, ,
arena. ( ""? ,
.) arena ( ) ,
,
. arena
7 ,
: ,
( ;
. 5. 7).
2. area ( 7 ) ,
. ++
,
.
,
( 1 4. 1 ) .
3. area ( 11 seven11 , 2 ) , ,
11 seven11 ,
7? .
, . ++
(. 3.9),
strinq int. ,
1 80 5

. ,
area ( "Hovel lane" , 2 ) , area ( " 7 , 2 " ) area ( " sieen" , " zwei " ) ,
?

.
, .



.
.

5. . .
, ,
; . ,
. . ,
,
,
, . .
int 4 = area ( l0 , -7 ) ; 11 , ,
11 11 ?
int 5 = area ( l0 . 7 , 9 . 3 ) ; 11 , a rea (1 0, 9)
char area ( lO O , 9999) ; 11 ,
=

4
. area ( l 0 , -7 ) :
area ( ) , :
, .
5
, doue, 1 0 . 7 9 . 3,
int, 10 9 (. 3. 9.2). ()
,
doue int,
area ( 1 0 . 7 , 9 . 3 ) .

, area ( 1 0 . 7 , 9 . 3 ) . int,
area ( 100 , 9999) , , 999900,
char. , ,
" -36.
,
.
5.4. 1 81

,
.
: " " ,
. , ,
, .
.

5 . 4.

, -1. (translation
units). -
,
. (
8.3). ,
.
, .
( )
8.3. ,
.
int area ( int lenqth , int width) ; 11

int a.in ( )
{
int = area (2 , 3 ) ;

area ( )
, ,
, area ( ) .
area ( )
( ),
.
int area ( int , int ) { /* " . */ } // " " a rea ()

,
"" .
doule area (doule , doule }
{ / * . . . */ } // " " a rea ()
int area ( int , int , char unit)
{ / * . . . */ } // " " a rea ()

,
.
182 5


. :
, .
.
,
, .
:
, ,
.
8 .2 8.3.

5.5.

, . -
.
, .
, . .
int area ( int length , int width) //

return length*width ;

int fraJDed_area ( int , int ) //

return area ( x-2 , y-2 ) ;

int ain ( )
(
int -1 ;
int 2;
int z = 4 ;
11 . . .
int areal = area (x ,y) ;
int area2 = framed_area ( l , z ) ;
int r framed_area (y , z ) ;
doue ratio = doule ( area l ) /area ; // do u e ,
1 1


.
. z, .
,
areal area2. .
5.5. 1 83

? ,
: , area ( ) ,
? ?
,
ratio .
. , - ?
, : r .
doue (areal ) /r .
, ;

.
,
. .
,
. .
"- !" ,
- .
. .
,
area ( ) . .
1 . .
area ( ) .
2.
area ( ) .

5.5.1 .
("
!"). , .
area ( ) ,
. ,
.
area ( , ) main ( )
:
if (<=) rrr ( "n " ) ;
if (<=) rrr ( "n " ) ;
int areal = area (x , y) ;

, . .
error ( ) . -
. std_lib_facil i ties . h
error ( ) .
.
1 84 5

, error ( ) .

. runtime_error
( 5 . 6.2, 7.3, 7.8. .2. 1 ) .
.
.
,
.
if (<= 1 1 <= ) error ( 11 n'l' area ( ) 11 ) ;
11 1 1 ""
int areal = area ( x , y) ;

area ( )
, framed_area ( ) .
:
if ( z<=2)
rrr ( 11 n'l' area ( ) "
11 framed_area ( ) " ) ;
int area2 = framed_area ( l , z ) ;
if (<=2 1 1 z<=2)
rrr ( 11 nn'l' area ( ) "
11 framed_area ( ) " ) ;
int r = framed_area (y , z ) ;

, .
, , framed area ( )
area ( ) . , framed_area ( ) 2
. !
- framed area ( ) , 2
1?
framed_area ( )
. "",
. "
" ( 4. 3 . 1 ). ,
, framed area ( ) , .
constexpr int frame_width = 2 ;
int framed_area ( int , int ) //

return area ( x-frae_width , y-frame_width) ;

. framed_
area ( ) .
if ( 1-frame_width<=O 1 1 z-frame_width<=O )
rrr ( 11n1 area ( ) 11
11 framed_area ( ) 11 ) ;
5.5. 1 85

int area2 = framed_area ( l , z ) ;


if (y-frame_width<=O 1 1 z-frame_width<=O)
rrr ( 11 1 r J area ( ) 11
1 1 framed_area ( ) 11 ) ;
int r = framed_area (y , z ) ;

! , ? ?
? ( ,
).
, framed_area ( ) m .
!
.
int area2 framed_area ( l , z ) ;
int r = framed_area (y, z ) ;

. ,
. ,
framed_area ( ) .

5.5.2.
framed_area ( )
, -
error ( ) .

int framed area ( int , int ) //


_

constexpr int frame width = 2 ;


if (x- frame_width<=O 1 1 y-frame_width<=O )
rrr ( 11 1 r area ( ) 11
11 framed_area ( ) " ) ;
return area ( x-frame_width , y-frame_width) ;

,
framed_area ( ) . ,
500 , .
, - ,
.
:
" " "
".
,
.
. , .
, .
.
1 86 5

area ( ) .
int area ( int lenqth , int width) //

if ( lenqth<=O 1 1 width <=)


error ( " Heono:llC'l'enw area ( ) " ) ;
return lenqth*width ;

area ( ) ,
framed_area ( ) .
- .
,
, ?
- , -
, .
.
,
. , ,
. ,
- ,
. ,
, , ,
.
, .
.
, ,
.
, .
, ,
, ,
. .
.
.
.
, area ( )
(.. ,
, ).
,

, ,
.
5.5. 1 87

, ? ,
.
, ,
5. 1 0.

5.5..
: ,
? "
". .
11 " " " " ;
11 ' ' (. . " " , " ")
char ask_user ( s trinq question)
{
cout << question << " ? ( n) \n" ;
strinq answer = " " ;
cin >> answer ;
if ( answer = "" 1 1 answer=="ye s " ) return ' ' ;
if ( answer ="n" 1 1 answer=" no " ) return ' n ' ;
return ' ' ; // ' '

// ;
11 - 1 ,
int area ( int lenqth , int width)

if ( lenqth<=O 1 1 width <= ) return -1 ;


return lenqth*width ;


.
-. .
. .
,
.
.
. .
,

.

, . . .
(, >>
1 88 5

cin) , int,
,
.
,
, .
.
int f ( int , int , int z )

int areal = area ( x , y) ;


if ( areal<=O ) rrr ( " " ) ;
int area2 = framed_area ( l , z ) ;
int r = framed_area (y , z ) ;
doue ratio = doule (areal ) /area ;
11 . . .

? ,
" ":
.

&
.
areal, area2, r ratio.
. ,
? ;
.

:
(exceptlons) .

5. 6.
, ++
: .
(
) (
), ,
. , ,
,
. ,
.
5.6. 1 89

:
, ,
return, . -
throw, , .
, ,
catch, ..
. ,
throw.
try ( ),
.
catch try.
, .
( 1 9),
.

5.6. 1 .
area ( ) .
class Bad area { } ; // ,
// a rea ()

// ;
11 Ba d a rea
int area ( int lenqth , int width)

if ( lenqth<=O 1 1 width<=O) throw Bad_area { } ;


return lenqth*width ;

, , , ,
, ,
area () throw, .
catch. Bad_area -
,
area ( ) , tch
, area ( ) . ,
( ), 9.
Bad_area { } " Bad_area
", throw Bad_area { } "
Bad_area ".

int main ( )
try {
int -1 ;
int 2;
1 90 5

int z = 4;
11 . . .
int areal area ( x ,y) ;
int area2 = framed_area ( l , z ) ;
int r = framed_area (y , z ) ;
doue ratio = areal/area ;
catch (Bad_area) {
cout << " area ( ) \n" ;

-, ,
area ( ) - main ( ) .
framed_area ( ) . -,
: main ( ) ,
throw Bad area { } , area ( )
, ( )
Bad_area, .
.
.
. , -

.

5.6.2. .
.
, .
. ++
(container).
vector,
4.6. vector ,
- size ( ) . ,
,
[O,v . size ( ) )? [low,high)
, low high-1, ..
, .

low : high :

1 "
1 . 1---_J
, :
" ?" , ,
v [O,v . size ( ) ).
!
5.6. 1 91

, .
:
vector<int> v ; //
for (int i ; cin>>i ; )
v . push back ( i ) ;
_ 11
for ( int i = ; i<=v . size ( ) ; ++i ) //
cout << "v [ " << i << " ] == " << v [ i ] << endl ;

? , .
. ,
, . , ,
. size ( ) ,

, i
, v [ i ] .
, . for:
i<=v . size ( ) ,
i<v . size ( ) . , -
, .
v [ 5 J , .
,
: (off-by-one error) ,
, (range error),
,
(bounds error), .
for
? ,
.
, . for
.
.
vector<int> v ( S ) ;
int = v [ S ] ;

,
.
,
? vector
, ( ; .
4.6 1 9.4). ,
out_of_range. ,
, , -
, ,
.
192 5

i nt ain ( )
t ry {
vector<int> v ; // i n t
for { int ; cin>>x ;
v . push_back (x) ; //
for ( int i = ; i<=v . size ( ) ; ++i) //
cout << "v [ " << i <<" ] == " << v [ i ] << ' \n ' ;
catch (out_of_ranqe) {
cerr << " ! \n" ;
return 1 ;
catch ( . . . ) { //
cerr << "JJDD : - \n " ;
return 2 ;

,
, ,
5 . 5.2. ,
.
(vector : : operator [ ] )
, 1 ?
,
. vector .
.

5.6..
,
, 1 0.6. ,

, ,

. , .
. .
.
doue d = ;
cin >> d ;

, ,
cin.

1 , vector<> : : operator [ ]

,
; (. ,
) - vector<> : : at ( ) .
std_lib_facilities . h ,
operator [ ] .
19 . -
. .
5.6. 1 93

if ( cin) {
11 ,

else {
11 ,
11 -

.
- , ,
du.
,
,
, .
. .
doule some_function ( )
{
doule d = ;
cin >> d ;
if ( ! cin)
error ( " 'l' doule "
" ' some_function ( ) ' ) ;
"

11 -

! cin (" cin", .. cin ) ,


cin .
, error ( ) ,
.
error ( ) ,
? ,
, . ,
error ( )
. ,
.
,
. - (
- 7 .3) .
,
out_of_range, vector. ,
runtime_error,
. ,
. error ( )
:
1 94 5

void error ( s trinq s )


(
throw runtime_error ( s ) ;

run time_error.
.
run time_error main ( ) .
int main ( )
try (
//
return ; 11

catch ( runtime_error& ) (
cerr << " runtime error : " << e . what ( ) << ' \n ' ;
keep_window_open ( ) ;
return 1 ; // 1

e . what ( ) runtime_
error. &
catch ( runtime_error& ) (

, . .

. 8 . 5.4-8 . 5 . 6 .
.
,
cerr. cou t.
, .
cerr cout , cerr
,

, . cerr
.
.
out_of_range runtime_error,
runtime_error
out_of_range,
vector
. out_of_range, runtime_error "
",
exception.
int main ( )
try (
//
5.6. 1 95

return ; //

catch (exception& ) {
cerr << "error : " << . what ( ) << ' \n ' ;
keep_window_open ( ) ;
return 1 ; // 1

catch ( . . . ) {
cerr << " clCJDl)ee\n " ;
keep_window_open ( ) ;
return 2 ; // 2

Ji ,
tch ( ).
. . .

Ji (out_of_range runtime_error)
exception, ,
exception () .
Ji
1 3- 1 6.
, ,
main ( ) , "", .
(, Unix) . (
Windows) . ,
, , main ( ) ,

- .
error ( )
, .
.
,
error ( ) .
void error ( strinq sl , s trinq s 2 )
{
throw runtiJDe_error ( sl+s2 ) ;

,

. , error ( )
: error ( )
tch,
Ji runtime_error ( - main ( ) ) .
Ji error ( ) 7 .3
7.7. Ji ,
( " Ji").
1 96 5

&
,
,
error ( ) .

5.6.4.
3.9.2 :
, .
.
int = 2 . 9 ;
char = 106 6 ;

2, 2 . 9 ,
int, int .
, ASCII,
42 ( * ) .
1066, char
.
3 . 9.2 ,
. ( ; . 1 9.3)
, runtime_
exception,
. .
int xl = narrow_cast<int> ( 2 . 9 ) ; //
int 2 = narrow_cast<int> ( 2 . 0 ) ; //
char cl narrow_cast<char> ( 10 6 6 ) ; //
char 2 = narrow_cast<char> ( 8 5 ) ; //

< > ,
. . .

vector<int>. ,
, . ,
, .11 (template arguments).
, ""
, narrow_cast,
std_lib_faci lities . h
error ( ) . (cast)
, -
"" ( 2).
, ,

2 cast - " '',

" ". -
. .
5. 7 . r 1 97

, . ,
.

5. 7 .
,
, . ,
, .
. ,
; , ;
- if - .
,
, .
- , , .
, - .
. .
.
- .
.
int .ain ( )
{
vector<doue> temps ; //

for ( doule temp ; cin>>temp ; 11


temps . push_back ( temp ) ; 11

doule swn = ;
doule hiqh_temp ;
doule low temp "' ;
_

for ( int : temp s )

if (x > hiqh_temp) hiqh_temp = ; //


if ( < low_temp) low_temp ; //
swn + ; //

cout << "&. : "


<< hiqh_temp << 11\n " ;
cout << ". 8 : "
<< low_temp << "\n" ;
cout << " : "
<< swn/temps . size ( ) << 11 \n" ;
1 98 5

.
. (Lubbock, 'Jexas). 1 6 2005 (
).
-16 . 5 , -23 . 2 , -24 . 0 , -25 . 7 , -2 6 . 1 , -18 . 6 , - 9 . 7 , -2 . 4 ,
7 . 5 , 12 . 6 , 2 3 . 8 , 2 5 . 3 , 2 8 . 0 , 34 . 8 , 3 6 . 7 , 4 1 . 5 ,
40 . 3 ' 4 2 . 6 ' 3 9 . 7 ' 35 . 4 ' 12 . 6 , 6 . 5 , -3 . 7 , -14 . 3

:
: 4 2 . 6
: -26 . 1
: 9 . 3

.
. .
.
. 23 2005 .
7 6 . 5 , 7 3 . 5 , 7 1 . 0 , 7 3 . 6 , 70 . 1 , 7 3 . 5 , 7 7 . 6 , 85 . 3 ,
8 8 . 5 , 9 1 . 7 , 95 . 9 , 99 . 2 , 98 . 2 , 1 00 . 6 , 106 . 3 , 112 . 4 ,
110 . 2 ' 103 . 6 ' 94 . 9 ' 91 . 7 ' 8 8 . 4 ' 85 . 2 ' 85 . 4 ' 8 7 . 7

:
: 112 . 4
1D1&.11 : .
: 8 9 . 2

- . (0.0F 1 8) -

- ! ?
low_temp m . ,
,
.


. .
. " (..
), .
?

. . .
?
high_temp :
,
, .
!
5. 7 . 1 99

;
""
. , ""
. .
int main ( )
{
vector<doue> temps ; 11

doue sum ;
doue high_temp -1000 ; //
//
doue low_temp 1000 ; //
//
int no_of_temps = ;

for ( doue temp ; cin>>temp ; ) { 11


++no_of_temps ; 11
SUJD += temp ; 11
i f { x > high_temp) high_temp = ; 11
if (x < low_temp) low_temp ; 11

cout << " . : "


<< high_temp " \n" ;
cout " : "
low_temp << "\n" ;
cout " : "
<< sum/no_of_temps << " \n" ;

? ?
""? 1000 -1000?
"" ( 5 . 5 . 1 ). 1000
-1000 - ,
, . . ? ,
- 1 000F (-573)? ,
1 000F (538)?


.
. min_temp
(" ") max_temp (" ").
.
200 5

5.8.
, , ,
,
, 2 . , -34 . 56.
, . ?
. (
, , ) 21 . 65685.
-

? ,
.
,
, , .
.
. , ,
, 9. ,
21 . 65685 !
10 . 392 3 . !
,
. I ,
, ,
.
.
1 . ?
( ) .
2. ?
, " ?"
" ?" . ,
, - , .
, ,
.
- ,
.
, "
",
.
, (guesstimation3) ,
.

3 : guesstintio guess ("

") estimation - (""). - . .


5.9. 201


, 2 .
?
. .
.

( ). (
- )
.

,
, . ,
,
. ,
9 ? ? ""
""?
? . ,
2000 -
. ( )
200 / (200 9 2000),

9 . : ,
,
.


,

. ,
, .

5. 9 .
(?) . ,
()
.
, .
- , ,
- .
, ,
. (debugging),
(bugs). I,
-
202 5

, -
. .
.
(Grace Murray Hopper), COBOL (
22.2.2.2). 50 .
.
(debugglng).
.
1 . .
2. .
3. . , -
.
:

. . - . ,
- .

.
. .
, - .
, . .
(
).
.
while ( r ) { //
r
, r

?
. . - .
,
.
:
r, ?


. . ,
, .
, , ,

5. 9. 203

. .
, . .
, - .

.

5. 9 . 1 .
,
. . -
.
. .
: " error ( )
exception main ( ) . "

. .
.

.
!
,
. ,
.
.
.
.
.
.
.
.
.

.
.
. !.
.
,
.
. .
.
204 5

,
.
.
:
.
.
.
if, .. , ,
, .
I , ,
.
. , ,
, .
,
.
. , -
. (
, ), ,
, , .
. -
. ,
( ),
( ). ,
, " ".

.
.
?
cout << " , << name << ' \n ' ; // !

?
cout << " , " << name << ' \n ; // !

?
int f ( int )

if (> ) / * - */
else / * - */ )
// !

?
if (<= // !
= f (y) ;
5.9. 205

"":
, ,
.
?
(,
#include " std_lib_facilities . h11 )?
?
?
int count ; /* . . . */ ++Count ; // !
char ch ; /* . . . */ Cin>>c ; // - !

?
= sqrt (y) +2 // !
z = + ;

.
, 5.2.
,
, , ,
, .
, .
,
. Windows
, , ,
( - - ).
- keep_window_open ( )
s td lib facili ties . h main ( ) .
- ,
, .
,
, , ,
. ,
. ? , ,
.
, ,
, "" , .
.
for ( int i = ; i<=max ; ++j ) { // ! ()
for ( int i=O ; O<max ; ++i) ; 11 v
cout << "v [ " << i << " ] ==" << v [ i ] << \n ' ;
11 . . .
206 5

,
( ,
).
, , ,
, ,
, ,
(
).
.
,

( cerr), :
int my_fct ( int , doule d)
{
int res = ;
cerr << "my_fct ( " << << " , " << d << 11 ) \n " ;
11 . . . . . .
cerr << "my_fct ( ) " << res << ' \n ' ;
return res ;

(.. ,
; . 9.4.3)
, :
int my_coplicated_function ( int , int , int )
11 < <
{
if ( ! ( < && < && <) ) // ! " " , & & - ""
rrr ( " '1'I f " ) ;
11

,
, ;
, , , .
assert.
,
.
-.
,
. ,
. ,
.
, ,
.
5.1 . - 207

5 . 1 . -
.
. -
,
:
. , :
int my_complicated_function ( int , int , int )
11 < <
{
if ( ! ( < && < && <) ) // ! " ", & & - ""
rrr ( " mcf" ) ;
11

,
. (
. ). .
, ,
(pre-condition) : ,
. , ,
. .
1 . (, ,
).
2. ( - ).
-
. .
int = my_complicated_function ( l , 2 , "horsefeathers") ;

.
( ). ,
. .
,
(
, ).
, ,
. ,
. , ,
,
? .

. ,
. .
208 5

.
.
.
, ,
.
.
. ,
.
.
,
, .
.
,
, .
:
, , .
, .
. , ,
, ,
, , , .
( ),
: ,
.
, . ,
. .

. , ;

,
.
int my_coplicated_function ( int , int , int )
// < <
{
if ( ! ( < && < && <) ) // ! " ", & & - ""
error ( " HeepJ.Ie 'l'l mcf" ) ;
11


int my_coplicated_function ( int , int , int )
{
11 . . .
5.1 . - 209

5.1 D. 1 .

.
- ? ,
returnl , ,
; .. - ,
- (
, ?).
5 . 6. 1 , .
11 ;
11 Ba d_ a rea
int area ( int lenqth , int width)

if ( lenqth<=O 1 1 width <= ) throw Bad_area ( ) ;


return lenqth*width ;

,
( ),
, (
).
.
int area ( int lenqth , int width)
11 .
11 : l ength wi dth
11 : ,
11
{
if ( lenqth<O 1 1 width <= )
error ( "area ( ) pre-condi tion" ) ;
int = lenqth*width ;
if ( <= ) error ( "area ( ) post-condition " ) ;
return ;

,
, ,
.

&
. ,
- .

- .
( 9.4.3),
( 4.2 5 .2), ( 26).
210 5

5 . 1 1 .
, ? ,
, ,
. ,
? . " " - :
, , .
.
,
. (testing)
7.3, 1 0 26. -
, -

.
(test case).
.
.
. .
. . -
. ,
. - . .

1 . ! @#$%
!
2. . !
. ?
, .
. .
,
. - !

.
. 7.3.
5.1 1 . 211

-../
.
.
#include " s td lib facilities . h"

int main ( )
try {
<< '1.' '1.'.n sa..r >>
keep_window_open ( ) ;
return ;

catch (exception& ) {
cerr << " : " << e . what ( ) << ' \n ' ;
keep_window_open ( ) ;
return 1 ;

catch ( ) { ...

cerr << " ! \n " ;


keep_window open ( ) ;
_
return 2 ;

, - . -
. . ,

"Successl" , ,
() ;
,
. ,
.
- "
" . ,
; , -
.
1 . Cout << " Success ! \n" ;
2 . cout << "Success ! \n ;
3 . cout << " Success" << ! \n"
4 . cout << success << ' \n ' ;
5 . string res = 7; vector<int> v ( lO ) ; v [S] res ; cout <<
" Success ! \n" ;
6 . vector<int> v ( l O ) ; v ( S ) = 7 ; if (v ( S ) ! =7 ) cout << " Success ! \n" ;
7 . if ( cond) cout << " Success ! \n" ; else cout << " Fail ! \n" ;
212 5

8 . bool = fal s e ; if () cout << " Success ! \n" ; else cout <<
" Fail ! \n" ;
9 . string s = " " ; = " fool " <s ; if () cout << " Success ! \n" ;
10 . s tring s = " " ; if ( s==" fool " ) cout << " Success ! \n" ;
1 1 . s tring s = " " ; if ( s==" fool " ) cout < " Success ! \n " ;
12 . string s = " " ; if ( s+ " fool " ) cout < " Success ! \n" ;
1 3 . vector<char> v ( 5) ; for ( int i=O ; O<v . size ( ) ; ++i) ; cout <<
" Success ! \n" ;
1 4 . vector<char> v ( 5 ) ; for ( int i=O ; i<=v . size ( ) ; ++i) ; cout <<
" Succes s ! \n " ;
1 5 . s tring s = " Success ! \n" ; for ( int i=O ; i< ; ++i) cout << s [ i ] ;
1 6 . if ( true) then cout << " Success ! \n " ; else cout << " Fail ! \n " ;
17 . int = 2000 ; char = ; if ( ==2000) cout << " Success ! \n" ;
1 8 . s tring s = "Success ! \n" ; for ( int i=O ; i<lO ; ++i) cout << s [ i ] ;
1 9 . vector v ( 5 ) ; for ( int i=O ; i<=v . size ( ) ; ++i) ; cout <<
" Succes s ! \n " ;
20 . int i=O ; int j = 9; while ( i< l O ) ++j ; if ( j<i ) cout <<
" Success ! \n " ;
21 . int = 2 ; doue d = 5/ (-2 ) ; if (d==2*x+0 . 5) cout <<
" Success ! \n " ;
22 . string<char> s " Success ! \n" ; for ( int i=O ; i<=lO ; ++i) cout
s [i] ;

23 . int i=O ; while ( i< l O ) ++j ; if ( j<i ) cout << "Success ! \n" ;
24 . int 4; doue d = 5/ (-2 ) ; if (d=2*x+0 . 5 ) cout <<
" Succes s ! \n " ;
25 . cin << " Success ! \n " ;


1 . .
2. ?
3. ?
4. ,

.
5. ?
6. ? .
5.1 1 . 21 3

7. ? .
8. ?
.
9. ? .
1 0 . ,
.
1 1 . ?
?
1 2 .
.
13. ,
(" ")?
14. ?
1 5 . .
1 6 . v [v. size ( ) ] v
? ?
1 7. ;
( area () );
, .
1 8 . ?
1 9. ?
20. .
2 1 . ?
22. ?

assertion
catch
throw



11
1 . ,
.
21 4 5

2.
.
. .
.
doule ctok ( doule ) //
( //
11
int k = + 2 7 3 . 15 ;
return int

int main ( )
(
doule = ; 11
cin >> d ; //
doule k = ctok ( " c" ) ; 11
Cout << k << ' /n ' ; 11

3. , .. -273. l 5C,
.
.
main ( ) ,
-273. 1 5.
4. . 3 ,
ctok ( ) .
5. .
.
6. .
(
4.3.3). ( 5.8) ,
.
7. 2 + + = .

= -------
2
: 2 - 4 ,
. ,
. ,
.
. ,
, .
5.1 1 . 215

? .
?
8. .
, N .
N. vector
N . :
:
3
. ( 1 ) :
12 23 13 24 15
3 ( 12 23 1 3 ) 48 .

. ,
, ,
.
9. . 8 .
,
int.
1 . . 8 . douhle
int. , ,
N- 1 ,
.
1 1 . ,
, .. ,
1 1 2 3 5 8 1 3 2 1 34.
. ,
int.
1 2 . " " .

9 (. . , , 2345, 4455),
. , 1 234,
1 359: " 1 1
", ( 1 )
() (3)
(). ,
, . .
.
1 3 . . ,
(
) .

randint ( lO )
216 5

std_lib_facilities . h. ,

.
,
randint ( l O ) srand ( n) , n - ,
. n .w
(seed),
.
1 4. ( , ) .
:
uesday 23 Friday 56 Tuesday -3 Thursday 99
vector<int>.
.
.
, Funday, ,
, n monday.
.

,
? .
- " !" .
. "
. .
, , ,
, , -
, . ! ,
,
, , ,
. . .
.
.

- .&#'


,
.
,

, , , .
.
-
, .
,

.
218 6

6.1 . 6.5. r
6.2. r
6.2. 1 . 6.5.1 .
r
6.2.2. 6.5.2.
6.3. 6.5.3.
6.3. 1 . 6.5.4.
6.3.2. 6.6.
6.3.3. 6.7.
6.3.4. 6.8.
6.3.5. ! 6.8.1 .
6.4. Token stream
6.4. 1 . : 6.8.2.
6.8.3.
6.4.2. r 6.9.

6.1 .
:
. ,
. -
. , ,
,
, .
, ,
,
. ,
.
?
.
.
.
, ,
.

.
,
.
.
.
6.2. 21 9

, .
, ,
.
: W
. ".
. . ,
:
, ,
.
.
2+3 . 1* 4


14 . 4

, ,

,
.

6.2.
? ,
. ,
. ,
.
, . .
,
.
: ( )
.
- .
, ,
.
, ,
, .
.
,
.
.

.
,
2 20 6

, . .

, .

,
.
, .
, , ,

, , ,
, , - ,
( ).

6.2. 1 .
, .
, .
. , ,
() .
.
.
,
.
. ,
,
.
, (,
) .
. , ,
, .

6.2.2.
,
.

? ,
, - , .
, -
, , - ,
.
( );
6.2. 221

, ,
, . "
?" "
?" ,
.
?
.

. .
.
.
. , ,
.
. , ,
() . ,
" 2 . 0",
.
,
? ,
. (
) , ,
, .
( ) ( )
, .
.
, .
, u .
, ,
? .

: ++.

.

, ..
, .
: ,
.
:
- ,
, . , ,
,
222 6

. ,

? . ,

,
.
,
(
).
, .
( vector), ( string) ,
(cin cout) .
.
, ,
(Token Token_strea.m) . 8 1 3- 1 5
.
.
, ,
, , , ..
.

- , .

. .. , .
. ,
, .


(. 9,
- 1 4 - 20).
,
. ,
. ,
( , ?!),
. ()
( )
, . ,
, .
:
,
;
6.. 223

,
r .
,
. ,
.

(prototype).
( ), .
. .
;
.
,
. ,
,
.
,
.

6 ..
? :
, cin cout,
(GUI) 1 6,
r .
r. , :
l : 2+2
: 4
: 2+2 *3
:
: 2+3-25/5
:

, 2+2 2+2*3,
; .
" : "
.

" , ",
.
, , >.
.
.
.

(use cases) .
224 6

.
:
read line
calculate //
write result

, , - :
(pseudo code).
. ,
. . calculate" ?
. ?
.

6.3.1 .
,
.
. - . .
. - . .
- ,
. :
ftinclude " s td lib facilities . h "
int a.in ( )
{
cout << "'l' ( + -) : " ;
int lval = ;
int rval ;
char ;
int res ;
cin>>lval>>op>>rval ; 11 - 1 + 3
if ( == ' + ' )
res = lval + rval ; //
else if (== ' - ' )
res = lval - rval ; //

cout << " Peyrrarr : " << res << ' \n ' ;
keep_window_open ( ) ;
return ;

. ,
2+2, ( 4)
. , ,
lval . . . - rval.
(-) ! . ?
- ! ,
, . .
6. 3 . 225

, .
-.
1 . .
2. (, 2 *3).
3. (,
1+2+3).
, ,
( "" )

s w i tch, if.
, 1+2+3+4 ,
; , 1, +2
2 1 ( , 3), +3
3 , 3, ..

:
#include " s td lib facilities . h "
int ain ( )
{
cout << " ( + , , * /) \n" ;
-

cout << " ( 1+2*3) : " ;


int lval = ;
int rval ;
cin >> lval ; 11

if ( ! cin)

error ( "e r " ) ;

for ( char ; cin >> ; 11


11
11 . . .
if ( ! = ' ' )

cin >> rval ;

if ( ! cin)

error ( "e r " ) ;

switch ( )

'+' :
2 26 6

lval += rval ; // : l va l l va l + rva l


break ;

case ' - ' :


lval -= rval ; // : l va l l va l - rva l
break ;

case ' * ' :


lval *= rval ; // : l va l l va l * rval
break ;

case ' / ' :

lval / = rval ; // : l va l l va l / rva l


break ;

default : // ;
cout << " : " << lval << 1 \n ' ;
keep_window_open ( ) ;
return ;

rrr ( " " ) ;

, 1+2*3,
, 9, 7, .
1-2 * 3 -3, -5, .
: 1+2*3 ( 1+2) * 3 , 1+ ( 2 *3) ,
. 1-2 * 3 ( 1-2) *3, 1- ( 2 * 3 ) ,
. ! , ,
, , .
.
.

6..2.
, (- ) ,
* ( /). , (-
) .
, ,
.
1 . .
.
1
+
2
6.3. 227

.
2. * ( /) +, -, (
) ?
3. , *?
4. ,
( 1+2*3)?
, 1-3,
4 .
, . - ,
, , ,
, ?
: ,
(tokens).
45+11 . 5/7

45
+
11 . 5
/
7
(token) - ,
, ,
. ++
.
.
++,
.
, ++.
3 . 14 , . 2742 42.
. + , -, * /. % .
( , ) .

: 12 , 12 . 3-3,

. , ,
, .
?
,
( ).
2 28 6

, ,
; , 42
- 4 2, ,
42 (.. 4 * 10+2).
- . (, ).
- : ,
? ( - )
.
(, ) ?
Token, . ? ,
: ,
,
. , int
. ,
. s tring
.
++
, char, int, douhle, string, vector ostream,
Token. - -
, - ,
. ,
, - atrix (. 24), Date (. 9)
( "Bignwn") .
, ,
: , ,
?
, ++
,
, (.. ,
).

6.3. .
? .
Token? Token
, + -, , 42
. 14 .
, ,
.
Token : Token :

kind : plus kind : number


value : value : 3.14
6.3. 229


++. . :
clas Token { //
pulic :
char kind;
doule value ;
};

Token - ( , int char) ,


.
( ) : kind value. class
", "; .
. , kind, char,
' + ' ' * '
+. .
Token t ; 11 t - Token
t . kind = ' + ' ; 11 t +
Token t2 ; 11 t2 - Token
t2 . kind = ' 8 ' ; 11
t2 . value = 3 . 14 ; 11 -

_. _
. t . kind " kind t",
t2 . value - " value t2" . Token
, int.
Token tt = t; //
if ( tt . kind ! = t . kind) rrr ( " ! " ) ;
t = t2 ; //
cout << t . value ; // 3 . 1 4

Token, ( 1 . 5+4 ) * 1 1
.

1'(' 1 1+ 1
'8'
1 .5 4 11

, (, +)
, value.
. ' 8 ' .
, .
' 8 ' ,
.
Token ++. .
, - (
), -. Token
230 6

- .
.
class Token {
puic :
char kind ; //
doue value ; // :
);

("") Token,
:
Token tl { ' + ' ) ; // tl ( t l . k i nd = ' + ' )
Token t2 { 1 8 1 , 11 . 5 ) ; // t 2 ,
/ / ( t 2 . kind = ' 8 ' t 2 . va l ue = 1 1 . 5 )


9.4.2 9.7.

6..4.
. , ! ,
. .
Token ?
Token.
Token qet_token ( ) ; 11 cin

vector<Token> tok ; //

int .ain ( )
{
while ( cin)
Token t qet_token ( ) ;
tok . push_back ( t) ;
)
11

, .
, 11 * 1 2 :
'8'

1 1

11 12


. , 11 12
, .
. 1 + 2 * 3
Token.
6.. 231

'8'
1+ 1 '8'

2
1 1 '8'

.
for ( int i = ; i<tok . size ( ) ; ++i ) {
if ( tok [i ] . kind== ' * ' ) { // !
doue d = tok [i-1] . value*tok [i+l ] . value ;
11 ?

. ? d?
? , + *
.
!
1+2*3. 1 *2+3.
1+2*3+4 . " ": 1+ ( 2 * 3 ) + 4 .
, ? ,
. .
,
.
, (
) .
, -
.
, ( . ) ,
.
. ""
.
, (
) ( ) .


,
?
. ,
. ,
. , , 12 . 5+2 .
, , ,
. ,
, , ,
! , ,
+ * 2+3* 4 .
232 6 r

" ". ,
1+2*3/ 4%5+ ( 6-7 * ( 8 ) ) ? . 2+*3
2 &3? . ,
.

6..5. !

,
. - ,
, .
.
.
while (not_fini shed)
read line
calculate 11
write result

, , ,
, ,
.
, ? ,
.
.
.
. ,
,
.
1 . 45+5/7,
- 45, +, 5, / 7? ( !)
2. ? ,
! ( ""
: "" - .)
3. 45+5/7 .
? , 4
5 4 5 (.. 4 * 10+5). (
. - .)
4. . 45+5/7 45+ ( 5/7) ,
( 4 5+5) /7?
6.4. 233

5. 5/7? . 71, .
, ,
.
? !
6. ? ,
v=7
m=9
v*

, . .
, .
7.8 , , w",
. -
.
, ,
- . w
" . ,
.
. .
, . w" ,
:
" ".
? ?
" ", .
1 , 4 .
, , 4 5
+ , - . .
, ? ,
. .
?
. ,
- , -
. .. .
!
. I ,
- , ,
.

6.4.
,
: ,
( ). ,
234 6

45+11 . 5/ 7


45
+
11 . 5
/
7

- ,
, .

. , ,
45+ 11 . 5/7 4 5+ ( 11 . 5/7) , ( 45+11 . 5) /7,
, , ?
- ,
. ,
. .
11 :
:

"+" 11
"-" 11
:
_
" * " _ 11
" / " _ 11
11 % 11 _ 11 ( )
_ :

" ( " " ) " 11
:
Ji___:

. : " -
". : "
_ ".
:
, .
6.3.2, ,
++, :
___ ( ++,
. 14 , . 2742 42):
+ , -, * , / , % ():
( , ) ().
6.4. 235

..
, .
. ,
: . .
. .
. .
( ) ,
( ) .
1-2*3, 1+2-3 3*2+4/2. .
"" .
. ?
-. ?
?
.
.
.
? .
" ". ,
.
w (parsing),
. . w
(parser syntax analyzer).
, , .
: 2 ?
1 . .
_ _
. . _
( . . 2 -
( . 'l'
___'l', .. , .
. _.
2. ( 2) / .
* %, (
/ . * %).
3. (_ 2) +,
-. (
+ -).
, 2 -
. . .
236 6 r

:

" + "
"-"
:

_
"*" _ t
_
; _
"%" _
_:

t
"(" ")"
:
___
t
___

t
2

, ,
. , , 2 - ,
2 - 'l'___'l', ,
_, ,
.
: 2+3 - ?
,
2 .
1 . ,
_ _
, _
( . , 2
( , 'l'___'l', ,
_.
2. _ ( 2) /,
*, %, (
/. * %).
. (_ 2) +,
,
, + . , ,
2 - , , .
-

+ , -,
( ,
+ -). , 2+3 + Term
.
6.4. 237

(
___ ).

2+3


:

"+"
f

"-"

t
:
_ t
_ _

'""""
"*" _

"/" _
"%" _
_:

t

1 1
"(" ")"
:
___

2 +

.
. , , 2 + 3 -
, 2 - , , 3 - ,
, + , .
, ,
,
, + *,
45+11 . 5*7.
.


, 2 2 + 3 . , 45,
11 . 5 7 ___,
, I_,
, _.
1 . 45 - , + ,
, +.
2. 11 . 5 - , * ,
_, *
.
3. 7 - _, 11 . 5*7 -
*_.
, 45+ 1 1 . 5*7 -
+ . , ,
238 6

11 . 5 * 7. 45+11 . 5 * 7 .
4 5+ ( 1 1 . 5 * 7 ) .
( ,
___ ).

" 45+1 1 . 5*7

tt
" + "
"-"
:
_
"*" _ _
t
_ _

'"'""'
"/" _

t t
"%" _
_:

"(" ")"

1 1 1
:
__ _

*
45 + 1 1 .5 7

, .
. , *_
1 1 . 5 7 . 4 5.
.
. .
2+2 45+1 1 . 5* 7.
. .
"" 4 5+ 1 1 . 5* 7
, .
, ,
, .
- , .

6.4.1 . :
,
. ,
,
. .
6.. 239

:
_ // , + + r u l es
J // , Bi rds fly bu t
11 fi sh swim

J :
" and"
"or"
"but"

_ :
"irds "
"fish"
"+ +"

:
"rules "
" fly"
" swim"

(,
, ).
- , ..

, "++ fly and blrds rules",
.

.
.
, , - "
" !

.
. ,
, 6.4 .
, , !

6.4.2.

? "". ,
. ,
. -
: .
240 6

:
_

1 1
:
"and"
"or"
"but"

-""
_ :

"' 1
"irds"
"fish"
"++" _ _

1
"
- "
: "
"rules"
"fly"

''swim"
"irds" " fly" "but" "fish" "swim"

1. .
2. ().
3. ().
4. ().
5. .

. , (
m) u
(tenninals), u (non-tenninals)
-

(productlons) .
. .
.
:
" { " ..n: " } "
..n: :

" " .
,


:
" "
""
6.5. 241

. - , ,
.
, .
-
-

. (?):
{}
{}
{, }
{,, , , }

, , (?):
{}

{, , ,,
{, , , ,
{ }
{,, , , , }

, ,
""; .

7.4 7.8. 1 .

6 .5.

. :
.
Token. . ,
. (parser).
.

6.5. 1 .
. : -
.
qet_token ( ) //
11 cin
expression ( ) // + -
11 t e rm () ge t_ token ()
ter111 ( ) // * , / %
// prima ry () ge t_ t oken ()
primary ( ) //
11 expre s s i on () ge t_ token ()
242 6

:
, ;
. -
, ,
.
?

, , get_
token ( ) , . .
primary ( ) () ,
:
qet_token ( ) // (
expression ( ) 11

?
? ,
2+3 expression ( ) 5,
. .
, : "
45+5/7 , ?"
,
.
!
,
expression ( ) -
. , 80/ .
get token ( ) :
_

, ,
. , + ( - . , get_
token ( ) Token. ,
.
1 1 , }')'I
Token qet token ( ) //
_
doue expression ( ) // + -
doue ter. ( ) // * , / %
doue priary ( ) / /

6.5.2.
expression ( ) .
:
:

6.5. 243

' + '
' - '


. .
, ,
. ,
, ,
-. -
, .

6.5.2. 1 . :
' + ' ,
expression ( ) + ( -),
,

term ( ) .
doue expression ( )
{
doue left = expression ( ) ; //
Token t = get_token ( ) ; //
switch ( t . kind) { //
case ' + ' :
return left + term ( ) ; // ,
11
' - ' :
return left - term ( ) ; // ,
//
default :
return lef t ; //

.
. : ,
, + -.
.
,
. , , + -?
,
. , +.
expression ( )
: expression ( )
expression ( ) , .
.

expression ( ) , .
,
, . pelC!Jpcu
, ,
244 6

. :
( 8.5 .8).

6.5.2.2. :
, ? ,
:
, + -.
.
doue expres ion ( )
{
doule left = Terxn ( ) ; 11
Token t = qet_token ( ) ; 11
witch ( t . kind) { 11
' + ' :
return left + expression ( ) ; 11
11 ,
case ' - ' :
return left - expression ( ) ; 11
11 ,
default :
return lef t ; 11

- - .

.
. ,
1+2 ( 1),
+ , - ( ,
2). , 3.
1+2+3 6. ,
, ,
, 1-2-3. expression ( )
1 , 2-3 (
2, 3). , 1
2-3. ,
1- ( 2-3 ) , 2 .
, 1-2-3 ( 1-2 ) -3 , , -4.
, ,
. . ,
. , 1+2+3
(6), 1+ ( 2+3) ( 1+2) +3.

? ,
6.5. r r 245

.
.
.
. ! ,
, ,
.
- .
expression ( ) . ,
+ -, w.
.
:

' + ' //
' - ' //

,
1-2-3 w 1-2. -
3, 1-2-3
1. - w 2-3. ,
, 1-2-3 ( 1- 2 ) -3, 1- ( 2 - 3 ) .
. , ,
,
, .
, ,
.
, 1-2-3 1- ( 2 - 3 ) ,
( 1-2 ) -3 .
,
, .

6.5.2.3. : ()
? (
6. 5.2): ,
+ -. ,
, , + -, ,
"" "" . .
doue expression ( )
{
doue left = term ( ) ; //
Token t = qet_token ( ) ; 11
while ( t . kind== ' + ' 1 1
t . kind== ' - ' ) { 11 + -
if ( t . kind == ' + ' )
left += term ( ) ; //
246 6

else
left -= ter ( ) ; 11
t = get_token ( ) ;

return left ; 11 : + - ;

:
+ - . , + - ,
get token ( ) .
_

. + -.
doule expression ( )
{
doule left = ter ( ) ; 11
Token t = get_token ( ) ; 11
while ( true) {
switch ( t . kind) {
case ' + ' :
left += ter ( ) ; 11
t = get_token ( ) ;
break ;
case ' - ' :
left -= ter ( ) ; 11
t = get_token ( ) ;
break ;
default :
return left ; 11 : + - ;
1 1

, - -
( 6 . 5 . 2. 1 ) .
expression ( ) expression ( ) .
,
, + - .

6.5..
.
:
_
' * ' _
' / ' _
' l/i ' _

,
. :
6.5. 247

doule term ( )
{
doule left = primary ( ) ;
Token t = qet_token ( ) ;
while ( true)
{
switch ( t . kind)
{
case ' * ' :
left *= primary ( ) ;
t = qet_token ( ) ;
break ;
case ' / ' :
left / = primary ( ) ;
t = qet_ token ( ) ;
break ;
case ' % ' :
left %= primary ( ) ;
t = qet_token ( ) ;
break ;
default :
return left;

, :
(%) .
.
5 6.3.5 - "
?". -

. ! ?
, % .
, .
% .
(. 7 . 5). % ,
:
.
, term ( ) . ,
2/0? .
,
,
.
.
.
248 6

doue term ( )
{
doue left = priary ( ) ;
Token t = get_token ( ) ;
while ( true)
{
switch ( t . kind)
{
case ' * ' :
left *= priary ( ) ;
t = get_token ( ) ;
break ;
case ' / ' :

doule d = priary ( ) ;
if (d = 0 )

rrr ( " " ) ;


}
left / = d ;
t = get_token ( ) ;
break ;

default :
return lef t ;

/ ?
.
swi tch. .

6.5..
.
_ :

' ( ' ' ) '

. , ,
.
doule priary ( )
{
Token t = get_token ( ) ;
switch ( t . kind)
{
case ' ( ' : 11 ' ( ' expre s s i on ' ) '

doule d = expres sion ( ) ;


6.6. 249

t = qet_token ( ) ;
if ( t . kind ! = ' ) ' )

error ( " ' ) ' ") ;

return d;

case 1 8 1 : // ' 8 '


return t . value ; //

default :
rrr ( " " ) ;

expres
sion ( ) term ( ) : ,
Token .

6 . 6.
.
get_token ( ) main ( ) . main ( ) :
expression ( ) .
int main ( )
try
{
while ( cin)

cout << expression ( ) << ' \n ' ;

keep_window_open ( ) ;

catch ( exception& )
{
cerr << e . what ( ) << ' \n ' ;
keep_window_open ( ) ;
return 1 ;

catch ( . . . )
{
cerr << 11 1J\n11 ;
keep_window_open ( ) ;
return 2 ;
250 6

5.6..
get_token ( ) 6.8
.

&
(
get_token ( ) ) calculatorOO . .
.

,
, .
"?" " ,
?", " ?" 2
. !
, , . -
. 3 . !
4 . 2 1
:
2

3
4
2

5+6. 5, :
2

3
4
2
5+6
5
, ,
.
. ?
. ? , ""
. .
.
. - - ,
!

. ,
, , .
= :
6 . 6. 251

while ( cin) cout << "= " << expression ( ) << 1 \11 1 ; 11 1

, .
:
2

4
2
5+6
= 5

! , .
.
.
2, 3
?
4 2 ,
4?
5+6 5,
11?

. ,
. ,
? : 4 2, 5+6
11, 5. . ,
1 2 3 4+5 6+7 8+9 1 0 11 12 .

1 2 4+5 6+7 8+9 10 11 12


1
4
6
8
= 10

? 2 , 3. 4 , 9 (. .
4+5)? 6 1 3 (..
6+7)? :
! , ""
? . expression ( ) .

doue expression ( )
{
doule left = term ( ) ; 11
Token t = get_token ( ) ; 11
252 6

while ( true) {
switch ( t . kind)
case ' + ' :
left += term ( ) ; 11
t = get_token ( ) ;
break ;
' - ' :
left -= term ( ) ; 11
t = get_token ( ) ;
break ;
default :
return lef t ; 11 : + - ;
11

Token, get_token ( ) ,
' + ' , ' - ' , .

. . ,
, , - .
, term ( ) .
, ""
.
expres sion () , ""
. (t).
? ,
(.
): ,
.
!
, , .
. . ,
,
.
, -

"Token_strea.m" - ts. , Token_strea.m


- get ( ) , .
- putack ( t) , t .
Token_strea.m 6.8,
, . Token_strea.m,
expression ( ) .
Token_strea.m.
doule expression ( )
{
6 . 6. 253

doule left ter ( ) ;


= 11
Token t ts . qet ( ) ;
= 11
while ( true) {
switch ( t . kind) {
case ' + ' :
left += ter ( ) ; 11
t = ts . qet ( ) ;
break ;
case ' - ' :
left ter ( ) ;
-= 11
t = ts . qet ( ) ;
break ;
defaul t :
ts . putack ( t) ; 11 t
return left ; 11 : + - ;
11

, term ( ) .
doule ter ( )
{
doule left = primary ( ) ;
Token t ts . qet ( ) ;
= 11
while ( true)
{
switch ( t . kind)
{
case ' * ' :
left *= primary ( ) ;
t = ts . qet ( ) ;
break ;
case ' / ' :
{
doule d = primary ( ) ;
if (d 0)=

{
rrr ( " " ) ;
}
left /= d ;
t = t s . qet ( ) ;
break ;

default :
ts . putack ( t) ; 11 t
return left ;
254 6

, primary ( ) .
get token ( ) ts . get ( ) ; primary ( )
.

6 . 7 .
, .
( Token_s tream)
calculatorOl . . . 2
. ?
. ,
"". - ? 3
. 2 . 2+2
. 3.
:
2

3
=2
2+2
=3

. . . putack ( ) expression ( ) term ( )


.

. .
2 3 4 2+3 2 * 3
2
3
4
5

! ! (6) .
.
, "" . ,
,
.
; ,
. ,
, <Enter>
. ,
.
? - "
". ,
,
. , " ",
6.8. 255

,
. q (
"quit" ()). main ( )
while ( cin) cout << "=" << expression ( ) << ' \n ' ; // 1

, :
doue val = ;
while ( cin) {
Token t = ts . get ( ) ;
if ( t . kind == ' q ' ) break ; // ' q '
if ( t . kind == 1 ; ' ) 11 ' ; '
cout << "=" << val << \n ' ;
else
ts . putack ( t ) ;
val = expression ( ) ;

. ,

2;
= 2
2+3 ;
= 5
3+4*5 ;
= 23
q

.
, .
. ,
, .

6.8.
.
Token_Stream. , -
- ,
. Token_Stream
. ,
.

,
( 1 . 5+4 ) * 1 1 (. 6.3.3). ,
cin
. , ,
, -
256 6

, .
. , 1 . 5+4 .
+ , 1 . 5 ?
1 . 55555? , ,
get ( )
putack ( ) . ++
, Token_stream.
, pu i c :
Token, 6.3.3.
. Token_stream
. ++ ,
, :
( pulic : ) (
private : ). , ,
,
, .
class Token strea {
_
pulic :
11
private :
11
11 ( Token s t ream)
_
};

,
, () ,
. () ,
, -
.
() .
. -
,
, - , ,

.
Token_stream.
? , get ( ) putack ( ) -
- . Token_stream
Token . ,
Token_
stream, cin. ,
Token_stream :
class Token s trea
puic :
6.8. 25 7

Token_stre&111 ( ) ; // Token_ s t rea m ,


11 cin
Token qet ( ) ; 11 Token
void putack ( Token t) ; 11 Token
private :
11
1;

, Token_
stream. ,
cin , -
.
, 7 .
" putack ( ) ,
put ( ) ?
get ( ) putack ( ) : ,
. , putack ()
istream: - .
.
Token_stream .
Token_strea. ts ; // Token_ s t ream ts
Token t = ts . qet ( ) ; 11 Token t s
11 . . .
ts . putack ( t) ; 11 Token t s

, , .

6.8.1 . Token s tream


_

Token_s tream.
Token_s tream? .
Token_stream,
? ,
Token_stream. ,
.
(
). ,
Token .
class Token strea.
pulic :
Token qet ( } ; 11 (ge t () 6 . 8 . 2)
void putack ( Token t) ; //
private :
bool full ( false } ; 11 ?
Token buffer ; 11
11 pu tback ()
1;
258 6

() -. putack ( )
, .
Token_stream:
void Token_stream : : putack ( Token t)
(
buffer = t ; // t
full =true ; //

void ( "") ,
putack ( ) .
- ,
, .

_ : : _-
- putack Token
stream.
- ?
- : ( ) ,
. -
, , ,
. ,
.
.
, -
, .
,
( get ( ) )
, .
void Token_stream: : putack ( Token t)
{
if ( full) error ( "pu tack ( ) : " ) ;
buffer = t ; // t
full = true ; //

full "
Token".
, Token_stream , ..
full fal se ,
get ( ) .
full Token_stream.
6.8. 259

6.8.2.
get ( ) . Token_strea.m : :
buffer Token, get ( )
cin Token.
Token Token stream : : qet ( )
_
(
if ( full) { // Token ?
full = fals e ; //
return buffer ;

char ch ;
cin >> ch ; 11 , >>
11
switch ( ch)
case ' ; 1 : //
case ' q ' : 11
case ' ( ' : case ' ) ' : case ' + ' :
case ' - ' : case ' * ' : case ' / ' :
return Token { ch ) ; //
case ' ' .
case ' ' : case ' 1 ' : case ' 2 ' : case ' 3 ' : case ' 4 ' :
case ' 5 ' : case ' 6 ' : case ' 7 ' : case ' 8 ' : case ' 9 ' :
cin . putack ( ch) ; //
doule val ;
cin >> val ; / /
return Token ( ' 8 ' , val } ; // 8 " "
' '

default :
rrr ( " " ) ;

get ( ) . ,
Token. ,
.
if (full ) { // Token ?
full =false ; //
return buffer ;

full fal se (.. ),


.
. ,
.
error ( ) . .
default :
rrr ( " " ) ;
260 6

error ( ) 5.6.
std lib facili ties . h.
, , . .
kind Token.

.
:
' (' :' ) ' : case ' + ' :

case ' -' : ' * ' : case ' / ' :

return Token { ch ) ; //

6.8..
. .
, 123? , 100+20+3.
12 . 34? ,
12 . 345?
, , , . ++
doue. ,
, - - cin get ( ) .
1 1 .

case ' ' : case ' 1 ' : case ' 2 ' : ' 3 ' : case ' 4 ' :
case 5 ' : 6 ' : case ' 7 ' : case ' ' : case ' 9 ' :
' '

cin . putack ( ch) ; //


doue val ;
cin >> val ; //
return Token { ' B ' , val } ; // 8 " "
' '

, Token
.
, ? .
++ (. ). ,
' . ' (
). , .
, cin , -
( ),
.
:
, 123, 1 ,
cin 23, 100 23. .
. , (
), cin , Token_stream,
6.9. 261

, . ,
,
cin .
, ,
,
.
: .
: " ". ,
( ):
. ?

6. 9 .
, .
, ,
.. . .
ltinclude " s td lib facilities . h"

class Token /* */ } ;
class Token stream /* */ } ;

void Token_stream: : putack ( Token t) /* */


Token Token_stream: : qet ( ) /* */

Token_stream ts ; // ge t () pu tback ()
doue expression ( ) ; //
// primary ()

doue primary ( ) /* */ 11
doue term ( ) /* */ 11 * /
doue expression ( ) /* */ 11 + -

int main ( ) /* */ 11
11 l!

.
. , ts
, ts . get ( ) ,
error ( ) .
. :
expression ( ) term ( )
,

primary ( ) , , . expression ( ) .
(
error ( ) , ) .
262 6

cin

, :
,
. ,
, .
"" expression ( ) .
? ,
. , ,
.
, ? ,
, .
6.6 . (
6. 7) , (
) .
.
, ,
.


, , ,
, .

,
.
1 . calculator02buggy . . .
. . -
6 . 9. 263

, calculator02
buggy . . , .
2 . q .
3. ; =.
4. main ( ) .
" . -'l' !
w- J . "

5. , ,
,
.


1 . " - "?
2. -
. , .
3. ?
4. ,
?
5. " "?
6. -
.
7. ?
8. ?
9. . , ,
I_.
1 . :
, , .
( 17+4 ) / (5-1) .
1 1 . numer ( ) ?
1 2. ?
1 3 . ? ?
1 4 . ? ?
1 5 . ?
1 6. expression ( ) swi tch
?
1 7 . ""?
1 8 . puthack ( ) ?
264 6

1 9. term ( )
( ,) %?
20. - Token?
2 1 . () ?
22. Token_stream,
get ( ) ?
23. swi tch get ( ) Token_stream -
; 1 q ?
24. , ?
25. , ? ?
26. ?
27. , ?

class
private
puic -


-

1 . ,
.
2 . { } ( ) ,
{ ( 4+5) * } / ( 3+4 ) .
3 . :
! . , 7 !
7 * 6* 5 * 4 * 3 * 2 * 1 . !
* /, .. 7 * 8 ! 7* ( 8 ! ) ,
( 7 * 8 ) ! . ,
.
!
1 . : doule,
.
! int
.
6. 9. 265

4. Name_value, .
. 1 9 4 ,
vector<Name value>.
5. " " 6.4. 1 the ,
"h irds fly but the fish
swim".
6. ,
" " 6.4. 1 .
, , . ,
. , irds fly
but the fish swim . , but irds fly but
the fish swim ( ) irds fly but the fish swim . (
) - .
"" "". :
, strinq
>>.
7. .

, ,
: ! (), - (), & (), 1 () " (
).
(. 25.5). ! -
. "
. 1 ( , *
. +), x l y" z
x l (y" z ) , ( x l y) " z . & ,
" . x"y&z " (y&z ) .
8. . 1 2 5 ( " "),
, .
9. .
. , 123
1 . 2 3 .
"123 1 , 2 3 ".
-

int. ,
, . . :
5 ' 5 ' , ' ' ,
. ' 5 ' - ' 0 ' ==5.
l . -
. , . .
,
266 6

. ( 60 , 3)
,
!
( , ) =
(- )! '
! . . 4 ! - 4 *3 * 2 * 1 .
. ,
. ,
,
. ,
- ,
.
:

( , ) = (

, ) .
.
(
), .
.
. .
.
.
. .
.

-
.
. ,
, .
,
. . .
. .


,
.
.
. .
, . .
,
,
,


.
268 7

7. 1 . 7.6.3.
7.2. 7.6.4.
7.3. 7.7.
7 .4. 7 .8.
7.5. : % 7.8 . 1 .
7.8.2.
7 .6.
7.8.3.
7.6. 1 .
7.8.4.
7.6.2.

7 1
. .

, ,
, .
,
, "" -
. " ",
! , -,
, .

, 6.
,
, .
,
.

7 2
. .

6 ,
:
:

,
: . ,
. .
, , - .
.
,
. ,
,
. ,
7 .2. 269

: Peyn'l'a'I' : ? ?
"" .
.

2+3 ; 5*7 ; 2+9 ;

:
" 5
- 35
" 11

: 'l''I' : . :
: 2+3 ; 5 * 7 ; 2+9 ;
: 5
: : 35
: : 11
:

, - , - - .
,
,
. , : Pe
yn'l'a'I' : . -
.
.
. , -
, ,
. , .
=.
.
>.
> 2+3 ;
5
> 5*7 ;
35
>

,
main ( ) .
doue val = ;
while ( cin) {
cout << "> " ; 11
Token t = ts . qet ( ) ;
if ( t . kind == ' q ' ) break ;
if ( t . kind == ; ' )
cout << "= " << val << ' \n ' ; 11
270 7 r

else
ts . putack ( t) ;
val = expression ( ) ;

,
.
> 2+3 ; 5*7 ; 2+9 ;
5
> 35
> 11
>

,
.
:
> 2+3 ; 5*7 ; 2+9 ;
5
35
11
>

, , , , .
main ( ) .
> . =
? , !
> get ( ) , ,
get ( )
Token, , .
, ,
Token s tream.
. .
Token stream. _

.
, ,
.

7 ..
, , "
" ,- "" , ..
,
. " ",
- ,
, - .
7 .. 271

" . !".
,
. , !
: " ! ,
. !"
, r.
1+2+3+4+5+6+7+8
1-2-3-4
! +2

( 1+3 ;
( 1+) ;
1*2/3%4+5- 6 ;
() ;
1+;
+1
1++ ;
1/0
1/0 ;
1++ 2 ;
-2 ;
-2 ; ; ; ;
1234567890123456 ;
'' ;
q
l+q
1+2 ; q



,
. ,
.. ?
, .
? .

(testlng) .
. .
-
, .
26. :
" ,
?" , ..
, , . ,
,
272 7

.
, ,
"" , :
r had little l
srtvrqtiewcet7rewaewre-wqcntrretewru754389652743nvcqnwq;
! @f $ %" &* ( ) : ;


-
, . " -
", " ".
.
, -
" ".
r .
,

+1 ;
()
! +2

( )
, ,
. .
.
, ,
, ,
.

. ""
.
, main ( )
(. 5.6.3).
catch ( runtie_error& ) {
cerr << e . what ( ) << ' \n ' ;
11 keep_ window_ open () :
cout << " , \n" ;
char ch ;
for ( char ch ; cin >> ch ; ) // -
if ( ch== 1 1 ) return 1 ;
return 1 ;

, keep_window_open ( ) .
, , -
7 .3. w 273

.
.
. keep_window_
open ( ) , ,
. .
:
catch ( runtime error& )
cerr < < e . ;hat ( ) << 1 \n ' ;
keep_window_open ( 11 1 1 ) ;
return 1 ;


+1

()


,
w w ,

, ''""
.
: , ,
( !),
, - . -
.
( , Unix) ,
cin .
cout - .
. ( . 1 0) .
.
1+2 ; q

1+2 q

(3) . ,

1+2 q

,
1+2 ; q
274 7

" ".
? , main ( ) , , ;
q. "" "" ,
( 6. 7) ,
.
:
doue val = ;
while ( cin) {
cout << "> " ;
Token t = ts . qet ( ) ;
if ( t . kind == ' q ' ) break ;
if ( t . kind == ' ; ' )
cout << "= " << val << ' \n ' ;
else
ts . putack ( t) ;
val = expression ( ) ;

, expression ( ) ,
q. , .
term ( ) , primary ( ) ,
q. _,
. ,
q.
,
, main ( )
:
in t ain ( )
try
{
while ( cin)
cout << " > " ;
Token t = ts . qet ( ) ;
1 . 1
while ( t . kind == ' ; ' ) t=ts . qet ( ) ; // " " '

if ( t . kind = = ' q ' ) {


keep_window_open ( ) ;
return ;

ts . putack ( t) ;
cout << "= " << expres sion ( ) << ' \n ' ;

keep_window_open ( ) ;
return ;

catch (exception& ) {
cerr << e . what ( ) << 0 \n ' ;
keep_window_open ( 1 1 11 ) ;
return 1 ;
7 . 4. 275

catch ( . . . ) {
cerr << 11 1U \n" ;
keep_window_open ( 11 1 1 ) ;
return 2 ;

.
.

7 . 4 .
, ,
. ,
-1/2

.
,
(0-1 ) /2

.

. ,
. ,
. ,
.
-
,
.
- . ,
, ,
: ,
.
.
,
. ,
_. :
&J11 :

" ( " 8&J11 " ) "

, :
:

" ( " ) " "
276 7

"-"
"+"
. ++.
, ,
. , _,
:
doule primary ( )
{
Token t = ts . qet ( ) ;
switch ( t . kind) {
case ' ( ' : // ' ( ' ' ) '
{
doule d = expres sion ( ) ;
t = ts . qet ( ) ;
if ( t . kind ! = ' ) ' ) rrr ( " ' ) ' " ) ;
return d ;

case 8 :
' ' // ' 8 '
return t . value ; //
case ' - ' :
return - primary ( ) ;
case ' + ' :
return primary ( ) ;
default :
rrr ( " " ) ;

. .

7 5 : %
. .

, ,
- % .
, .
. .
1 . % Token.
2. doule int,
% .
, % ,
:
> 2%3 ;
= 2
> 3%2 ;
1
7 .5. : % 277

> 5% 3 ;
"" 2

. ?
> . 7% 3 . 3 ;

?
.
. , %
x%y=x-y*int (x/y) . 6 . 7% 3 . 3==6 . 7-3 . 3*int ( 6 . 7/ 3 . 3 ) , .. 0 . 1.
fmod ( )
<cmath> (24.8). term ( ) .
:
case ' % ' :

doue d = primary ( ) ;
if (d == ) error ( " % : " ) ;
left = fmod ( left , d) ;
t = ts . get ( ) ;
break ;

<cmath>
, sqrt ( ) ( ) . abs () (
) . log () ( ) pow ( , ) ( ).
%
. . ,
. . .
.
%
(3 . 9 . 2 5.6.4),
narrow cast:
case ' % ' :

int il narrow_cast<int> ( left) ;


=

int i2 narrow_cast<int> (primary ( ) ) ;


if ( i2 0 ) error ( " % : " ) ;


=

left =il%i2 ;
t ts . get ( ) ;
=

break ;

. .
278 7

7 6
. .

.
, ,
. , -
, ,
.. ,
. , ,
.
,
.

7 6 1
. . .

. . ' 8 '
Token, .
,
; ,
.
, .
case ' 0 ' : 11 ' 0 '
return t . value ; 11
case ' - ' :
return -priary ( ) ;

, ,
' ' , ' 8 ' , ,
. . -
' 8 ' . Token,
, ;
1 8 1 "
", 4.3. 1 .
. .
const char nWDer = 1 8 1 ; // t . k i nd==number , t -

const . .
; , nu.mer= ' '
. nu.mer
' 8 ' .
primary ( ) . , :
case nWlle r :
return t . value ; //
case ' - ' :
return -priary ( ) ;
7 .6. 279

.
, .
, , -
. .
Token_stream : : get ( ) , ,
:
case 1.
1

case 1 0 1 : case ' 1 ' : case ' 2 ' : case 1 3 1 : case ' 4 ' :
case ' 5 ' : case 1 6 1 : case ' 7 ' : case 1 8 1 : case ' 9 ' :

cin . putack ( ch) ; //


doule val ;
cin >> val ; //
return Token (numer , val ) ;

,
. , ' ( ' ' + ' .
, . ' ; ' "" (
" ") ' q ' ""
. ' ' ' ' ?

, :
const char quit = ' q ' ; 11 t . k i nd==qui t , t -
//
const char print = ' ; ' ; // t . kind==pri n t , t -
//

main ( ) :
while ( cin)
cout << "> " ;
Token t = ts . get ( ) ;
while ( t . kind == print) t=ts . get ( ) ;
if ( t . kind == quit) {
keep_window_open ( ) ;
return ;

ts . putack ( t ) ;
cout << "= " << expression ( ) << ' \n ' ;

"" "",
. , ,
main ( ) , , . ,
, "
" ' ' ( "exit").
main () .
280 7

"> " "= "


"" ? ,
main ( ) ,
? , ?
,
.
const strinq prompt "> " ;
const strinq result " = " ; 11 ,
11


, .
:
while ( cin) {
cout << prompt ;
Token t = ts . qet ( ) ;
while ( t . kind ==print) t=ts . qet ( ) ;
if ( t . kind == quit) {
keep_window_open ( ) ;
return ;

ts . putack ( t) ;
cout << result << expression ( ) << 1 \n ' ;

7 6 2
. . .

,
.
:
expression ( ) , term ( ) primary ( )
, get ( )
. main ( ) ,
.
1 . main () :
, "" .
2. main ( ) .

(. 4.5. 1 ) . main ( )
. .

calculate ( ) .
7 .6. 281

void calculate ( ) //
{
while ( cin)
cout << prorpt ;
Token t = ts . qet ( ) ;
while ( t . kind == print) t=ts . qet ( ) ; 11
if ( t . kind == quit) return ;
ts . putack ( t) ;
cout << result << expression ( ) << ' \n ' ;

int main ( )
try {
calculate ( ) ;
keep_window_open ( ) ; 11
return ;

catch ( runtime_error& ) {
cerr << e . what ( ) << endl ;
keep_window_open ( 11 11 ) ;
return 1 ;

catch ( . . . ) {
cerr << " \n" ;
keep_window_open ( 11 11 ) ;
return 2 ;

, ,
.

7 .6..
:
switch ( ch) {
case q : case ' ; ' : case ' % ' : case ' ( ' : case ' ) ' :
' '

case ' + ' : case ' - ' : case ' ' : case ' / ' :
return Token { ch } ; //

, ' q ' , ' ; ' ' % ' ,


. . ,
. , /
case
. Token_stream : : get ( )
:
Token Token_stream : : qet ( )
11 c i n Token
282 7

if ( full) { // Token
full =fal se ;
return buffer ;

char ch ;
cin >> ch ; 11 , >>
switch ( ch) 11
case quit :
case print :
case 1 ( ' :
' ) 1 :
case ' + ' :
case , _ , :
case 1 * 1 :
case ' / ' :
case 1 % 1 :
return Token { ch } ; //
case 1 1 . /!
//
11 :
case ' : case 1 ' : case
' ' ' 2 ' : case ' : case 4 ' :
' '

case ' 5 ' : case 6 ' : case


' ' 7 : ' 8 ' : ' 9 ' :
'

cin . putack ( ch) ; //


doue val ;
cin >> val ; //
return Token { nwaer , val } ;

default :
rrr ( " neJCcea " ) ;

. case
, .
, get ( )
.
; , .
. .
, ,
' q ' qui t.

qui t . .

.
.
. ,
- , ,
. : .
7 .6. 283

7 6 4
. . .


. - .
. ,
,
,
.
1 . ( . ) .
2. ( ).
. ( ).
: ,
.
. , ,
, .
.
= + ; //
11

,
, ,
.
,
.
: ,
,
(. 5.9. 1 ) .
. - :
, ,
( ) ,
.
.
/*

2 0 0 7 .
2 0 0 6 .
2 0 0 4 .
(bs @ cs . tamu . edu) 2 0 0 4 .

.
284 7 r

cin ; - cou t .

:
q

:

+
-

:
_
* _
/ _
% _

_ :

)
- _
+ _

:
___

cin Token s t ream t s .


*/

,
/ * * / .
,
.
, .
:
, (,
).
calculate ( ) ,
.
7.8. 1 .
7 . 7 . 285

7 . 7 .
,
? ,
?
? , ,
, . ,
. ,
,
,
.

main ( ) .
, calculate ( )
,
.
void calculate ( )
{
while ( cin)
try {
cout << propt;
Token t = ts . get ( ) ;
while ( t . kind == print) t=ts . get ( ) ; //
if ( t . kind == quit) return ; //
ts . putack ( t) ;
cout << result << expression ( ) << ' \n ' ;

catch (exception& )
cerr < < e . what ( ) < < ' \n ' ; //
clean_up_mess ( ) ;

while try,
.
-.
" "?

,
.
Token_stream. , , -
,
. .
1++2 * 3 ; 4+5 ;
286 7

, 2 * 3 ; 4+5
Token stream cin , +
. .
1 . Token_s tream.
2. Token_stream ,
.
( 4+5 ; ),
- 2 * 3 ; , 4+5 ; .
, - .
. .
.
, ,
. . .
get ( ) . ,
clean up mess ( ) :
_ _

void clean_up_ess ( ) //
{
while ( true) { // ,
//
Token t = ts . get ( ) ;
if ( t . kind == print) return ;

. . ?
:
l@ z ; 1+3 ;
@ catch while.
clean_up_mess ( ) .
, . get ( ) z .
( z
), catch ( . . . )
main ( ) .
1+3.
try catch,
.
, .
, .
Token stream ,
_

.
get ( ) , . ,
7. 7. 287

. , .
- Token_s tream.
class Token stream
puic :
Token qet ( ) ; 11
void putack ( Token t ) ; 11
void iqnore ( char ) ; 11
11
private :
bool full { false } ; 11 ?
Token buffer ; 11 ,
11 pu tback (}
};

ignore ( ) Token_stream,
.
ignore ( ) - ,
Token_stream ,
.
, ,
, ( ,
). , :
void Token stream : : iqnore ( char )
_
11

11 :
if ( full && c==buffer . kind)
full = fal se ;
return ;

full = false ;

11 :
char ch = ;
while ( cinch)
if ( ch==c) return ;

.
, . ;
cin, .
clean_up_mess ( ) :
void clean_up_ess ( )
{
ts . iqnore (print) ;
288 7

- .
.
,
. " " (..
) .
.
- .

7 . 8 .
,
.
; ?
.
.
,
, pi , .
-
. .
,
. ,

.

7 .8. 1 .
,
( . ) ,
.
Variae:
clas Variale
puic :
tring name ;
doule value ;
};

name
Variae. value - .
name.
Variae ,
, name ?
,
: Variale.
7 .8. 289

vector<Variale> var_tale ;

var_t
Variae, ,
. get_value ( ) ,
name value.
doule qet_value ( strinq s )
1 1 . s

for ( const Variale& v : var_tale)


if (v . nae == s) return v . value ;
error ( 1 1qe t : n 11 s) ;

: Variae
var_tae (
) , name s .
name s , value
.
set_val ue ( ) .
value Variae.
void set_value ( s trinq s , doule d)
11 s Va ri a e d

for (Variae& v : var_ta.le)


if (v . nae ==s) (
v . value = d ;
return ;

error ( 1 1 se t : n n 11 s) ;

,
Variae var_t.
Variale var_tale?
, ,
? , ++.
doule var = 7 . 2 ;

,
doule, .
.
var = 7 . 2 ;

. ,
.
290 7

varl = 7 . 2 ; // va rl
varl = 3 . 2 ; // var2

! var2 = . 2 ; , "" (
). , . . .
, ++.
( ) .
doule, -
, - -
let.
let var = 7 . 2 ;

r :
:

l

w

:
'
:

'n :
" let" 11 = 11

- ( ) .
( calculate ( ) ),
.

'I'. , :
doule s tatement ( )
{
Token t = ts . get ( ) ;
switch ( t . kind) {
case let :
return declaration ( ) ;
default :
ts . putack ( t ) ;
return expression ( ) ;

expression ( ) calculate ( )
statement ( ) .
void calculate ( )
{
while (cin)
7 . 8. 291

try {
cout << prompt ;
Token t = ts . qet ( ) ;
while ( t . kind == print)
t=ts . qet ( ) ; //
if ( t . kind== quit) return ; //
ts . putack ( t) ;
cout << result << stateent ( ) << \n ' ;

catch (exception& ) {
cerr << e . what ( ) << ' \n ' ; //
clean_up_ess ( ) ;

declaration ( ) .
? , let ,
- = . .
? var_t
vector<Variae> Variale name
.
get_value ( ) set_
value ( ) . , ,
. .
let vl 7;
let vl = 8 ;


. . ,

let vl 7;
let v2 ;

Variale var val


.
1. var_tale Variale
var.
2. (var,val) var_tale.
,
is_declared ( ) define_name ( ) ,
.
bool is_declared ( strinq var)
11 va r va r_ ta l e ?

for ( const Variae& v : var_tae)


if ( v . name var) return true ;
==
292 7

return false ;

doule define_name ( strinq var , doule val)


11 (va r , va l ) va r_ ta e

if ( i s_declared (var) ) error (var , " '111111 ) ;


var_tae . push_back (Variale (var , val ) ) ;
return val ;

Variae vector<Variale>
: - push_back ( ) .
var_tale . push_back (Variale (var , val ) ) ;

Variae (var , val )


Variae, push_back ( )
var_tae. , ,
let , declara tion ( ) .
doule declaration ( )
11 , "l e t "
11 : =
11 ,
11

Token t = ts . qet ( ) ;
if ( t . kind ! = name)
error ( " ' " ) ;
strinq var_name = t . name ;

Token t2 = ts . qet ( ) ;
if ( t2 . kind ! = ' = ' )
error ( " ' " var_name) ;

doule d = expression ( ) ;
define_name (var_name , d) ;
retur d ;

, ,
. ,
,
let v = d/ ( t2-tl) ;

v .
, calculate ( ) ,
statement ( ) .
.
.
7 .8. 293


(symbol taes) .
map ( 2 1 .6. 1 ) .

7 .8.2.
, , , .
. - . -
. -
. ' = ' , .
case Token_stream : : qet ( ) (
7.6.3). let ? , ,
,
qet ( ) . ? .
const char n. = ' ' ; 11
const char let = ' L ' ; 11 l e t
const string declkey = " let" ; 1 1 l e t

Token Token_stream: : get ( )

if ( full ) {
fullfal se ;
return buffer ;

char ch ;
cin >> ch ;
switch ( ch)
11
default :
if ( i salpha ( ch ) ) {
cin . putack ( ch) ;
string s ;
cin >> s ;
if ( == declkey)
return Token ( let) ; 1 1 l e t
return Token { na.s , s } ;

rrr ( " " ) ;

. isalpha ( ch) .
" ch ?";
isalpha ( )
std_lib_facilities . h.
1 1 .6.
:
294 7

( - ).
putack ( )
>>.
. ; Token
. Token { name , s } .
. Token
. doule, s tring
.
kind; . Token { ' * ' } .
kind ; . Token { numer , 4 . 321 } .
kind ; , Token { name , "pi " } .
, ,
, :
class Token {
pulic :
char kind ;
doue value ;
s trinq name ;

11 kind ch
Token ( char ch) : kind { ch ) { }

11 k i nd va l u e
Token ( char ch , doue val) : kind { ch } , value { val } { }

11 kind
Token ( char ch , strinq n) : kind { ch } , name { n } { }
};


. 9 (
9.4.2. 9. 7).
let ' L ' ,
. .
doule, var, i - .
declkey. s .
.
, . :
let = 3 . 4 ;
let = 2 ;
+ * 2;

. ,
. :
7 . . 295

let = 3 . 4 ;
let = 2 ;
+*2 ;

? , .
, . "
" ( 7 .8. 1 ) .
? ? . ? ,
. ? +? ?"
.
s tring >>.
, . , , +*2 ;
-
. , , .
?
, ,
get ( ) . :
, . ,
:

al
Z12
asdsddsfdfdasfdsa434RTHD12345dfdsa8fsd88 8fadsf

:
la
as s
#
as*
car


++.
defaul t get ( ) .
default :
if ( isalpha ( ch) )
strinq s ;
s += ch ;
while ( cin . qet ( ch) &&
( isalpha (ch) 1 1 isdiqit ( ch) ) ) s+=ch ;
cin . putack ( ch) ;
if ( s == declkey)
return Token { let } ; 11
return Token { nae , s } ;

rrr ( " " } ;


296 7

s string
s,
. s+=ch ()
ch s.
while ( cin . qe t ( ch) && ( isalpha ( ch) 1 1 isdiqit ( ch) ) s+=ch ;

ch ( - get ( )
cin) , . ,
ch s . -
get ( ) >>. ,
.

7 ...
. ,
,
, pi .
? main ( ) calcula te ( )
calculate ( ) .
main ( ) , - .
int main ( )
try (
11
define_nae ( "pi " , 3 . 141592 6535 ) ;
define_nae ( " " , 2 . 7182818284) ;

calculate ( ) ;

keep_window_open ( ) ; 11
return ;

catch ( exception& ) (
cerr << e . what ( ) << 1 \n ' ;
keep_window_open ( " -- 11 ) ;
return 1 ;

catch ( . . . ) (
cerr << 111JDD \n" ;
keep_window_open ( " -- " ) ;
return 2 ;

7 8 4 ?
. . .

- . ,
,
7 .8. 297

. , .
. "" (. . 2),
-
(. . 3).

. . ,
.
1 . :
.
2. .
.
45%,
.
, ,
, .
, 50/
.
. .
, . -
, .. , ,
,
.


1 . calculatorOBbuggy . .

2. .
3. (
. ) . :
.
4. :
. ?
? , ,
" " .
5. ,
.
6. k 1000.
7. sqrt ( ) .
sqrt ( 2+6 . 7 ) . , sqrt () -
298 7

; . sqrt ( 9) 3.
sqrt ( ) ,
std_lib_facilities . h.
.
8.

.
9. pow ( , i ) .
" i "; , pow ( 2 . 5 , 3)
2 . 5*2 . 5*2 . 5 . i .
.
% .
1 0. " " let #.
1 1 . " " q exi t.
qui t,
let 7.8.2.


1 . ,
? .
2. "1+2 ; q". .
?
. nwner?
4. main ( ) .
main ( ) ?
5 . ?
.
6. ?
7. narrow_cast?
8. ?
9. ?
1 0. % ()
?
1 1 . i s_declared ( ) ?
1 2. let
.
?
7 .8. 299

1 3. , ,
, .
14. ?
1 5. ?
1 6. ?
1 7. ,
?
1 8. ?
.
1 9. ?
20. , ?
2 1 . ?

1 .
.
2. =,

let. ,
.
3. ,
. : Variae ,
,
set_value ( ) .
(
pi ),
, const pi = 3 . 14 ; .
4. gt_vlu ( ) , set_value ( ) , is_declared ( ) define_name ( )
var t. Symol_t
_

var_t vector<Variae> - get ( ) ,


set ( ) , is_declared ( ) define ( ) .
Symol_t.
5. Token_stream : : get ( ) , ,
, Token (print) .
300 7


' \n ' .
i s space ( ch) , true,
ch .
6. .
( )
.
7 . q h quit help .
8. 7.6.4 (
,
): ,
4+4 ; 5-6 ; , ,
7.8. . ,
, .
9. (
). .
1 . ,
; ,
. : narrow_cast ( 7.5).
1 1 . ,
4 5.
, . ,
-
.

, ,
. ,
, .
.
( ).
. i
;
.
:

,, -.,.,."....-.
k.

u

-
++ . ,


++ . ,
,
,

.
302 :

8. 1 . 8.5.5.
8.2.
8.2 . 1 . 8.5.6.
8.2.2.

8.2.. 8.5.7.

8.3.
8.5.8.
8.4. 8.5.9. nstr-
8.5. 8.6.
8.5.1 . 8.6.1 .
8.6.2.
8.5.2.
8.7.
8.5..
8.7.1 . usinq

usinq
8.5.4.

8.1 .
,
, . .
, ,
,
. :
,
, .
.
.
.
, ,
" .
,
.
. .
.
- .
, .

. , ,
- . ,
. ++ ,
.2. 303

; ,
.
,
, ,
. ,
.
,
. - -
.
. .
, , ,
, . ,
++ , ,
( 27), Java #, .
, , ,
, f, g, .
, ,
.
(
), ,
. ,
.
,
,
.
, ,
++ (
, ) . ISO ++
1 300 ; The ++ Programming
Laguage 1 300 ,
(
++ ).
,
,
, .

8.2.
(declaration) - ,
( 8.4),
(,
)
304 :

() (.
).
.
int = 7 ; 11 i n t
const doue c d = 8 . 7 ; //
//
doue sqrt (doule ) ; 11 dou e ,
1 1 doue
vector<Token> v ; 11 - Token

++.
.
int main ( )
{
cout << f ( i ) << ' \n ' ;

,
: cout. f i
. , cout, ,
s td_lib_facilities . h.
.
ftinclude " std li. facilities . h" // co u t

int main ( )
{
cout << f ( i ) << ' \n ' ;

.
.
.
,
"- ". ,
, ;
, .
,
: , cout
<<;
ftinclude. ;
, ,
. cout.
, "" .
- f i .
:
8.2. 305

#include " s td lib facilities . h" // co u t

int f ( int) ; 11 f

int ain ( )
{
int i = 7 ; 11 i
cout << f ( i ) << ' \n ' ;

,
, (. 2.4),
f ( ) ; , ,
f () .
, ,
(definition). .
int = 7 ;
vector<doue> v ;
doue sqrt (doue d ) { / * . . . */ }

,
.
, ;
,
, - .
doue sqrt (doule) ; //
extern int ; // "extern "
11 ,

,
, ,
, ,
.
, .
, .
, .
.
doue sqrt (doue d) { / * . . . */} //
doue sqrt (doue d) { / * . . . */} // :

int ; 1 1
int ; // :

, ,
, , ;
, .
306 t :

,
, .
int = 7 ; //
extern int ; 11
extern int ; //

doule sqrt ( doule) ; /!


doule sqrt ( doule d) { / * . . . */} //
doule sqrt ( doule ) ; 11 sqrt
doule sqrt ( doule ) ; // sqrt

int sqrt ( doule ) ; // :


// sqrt

?
sqrt,
doule (int
doule).
extern,
, , .
. ,
,
, (.
8.4 8.6.2).
: :

doule sqrt(doue d) doue sqrt(doue d)


{
11
doue sqrt(doue d) 11
11 d

extern int ;

int x = 7;
extern lnt ;

++ ,
?
, ,
(), , ,
, () .
, (
). (
),
( ). ,
, -
.2. 307

,
, - .

.
;
.
( ),
.
8.3.
6: expression ( )
term ( ) , , . primary ( ) ,
expres sion ( ) .
++ ,
, .
doue expression ( ) ; 11 ,

doule priary ( )
{
11 . ..
expression ( ) ;
11 . . .

doule term ( )
{
11 ...
primsry ( ) ;
11 . . .

doule expression ( )
{
11 ...
term ( ) ;
11 . . .

;
,
. ,
. expression ( )
primary ( ) . .
.
?
, (
), , ?
308 :

, "" ,
. ++ ,
(
; . 9.4.4). ,
( ): , ,
,
,
. " "
, .
,
. ( ),
, , "-".
"-" , .
,
, ( )
.

8.2. 1 .
++.
.
.
.
(. 8 . 5) .
(. 8. 7).
( ; . 9).
(. 1 9).

8.2.2.
, (
) . .
int ; //
dou.le d = 7 ; 11

vector<int> vi ( 10 ) ; // ()
vector<int> vi2 { 1 , 2 , 3 , 4 } ; // { }

ISO ++.
, ,
const .
8.2. 309

cont int =7 ; // =
cont int 2 { 9 } ; // { }
cont int ; // : '

, ,
:
. , -
; , ,
. .
void f ( int z)
{
int ; //
// . . .
7; //

11 . . .

, ,
. ,
? .
void f ( int z )
{
int ; //
// . . .

if ( z>x) {
11
}
11
= 7; //

11 . . .

, z>x
. z>x

.
z>x -
, -
.
,
, ,
. . " " (,
) -
. ,
, -
- . .
. ,
310 8 :

, ;
. , ,
.

{ } .
"". ,
, =.
( ) ,
(. 1 7.4.4).

8.2..
, ,
string, vector .
vector<strinq> v ;
strinq s ;
while ( cin>>s) v . push_back ( s ) ;

.
.
, ,
. ,
v (.. ), s (
" "). , ,
(default constructor).
, ++
. (. 8.4)
,
. ,
,
, (
). , !

. .
?
,
;
. , ,
. , cout
sqrt ( ) - .
. I ,
- , ++ . ,
8.. 311

(header) :
-

, (header file).

#include. ,
(. 6 7).
. token . h,
, Token Token _

stream:
token.h:

// :
class Token { /* . . . / } ;

class Token_atream { /* . . / } ;
.

token .cpp:

#include "token.h'"
/ "''"'''"'
//:
#include "token .h'"
vold Token_atream:: putback(Token t)
// :
{
buffer = t;
Token_stream ts;
full = true;

Token t = ta.get();

ts. putback(t);

Token Token stream


token . h. token . . ++
h ,
.

. .
++ .

.
, #include "file . h"
file . h , .
. f . h.
11 f . h
int f ( int) ;

user .
11 user .
#include " f . h "

int g ( int i )
31 2 :

return f ( i ) ;

user .
. :
int f ( int) ;
int q ( int i )
{
return f ( i ) ;

#include
,
(preprocessing) ( . 1 7).

. .
, ,
.
. , , Token_
stream : : putack ( ) .
Token Token_s tream : : putack ( Token t)
{
buffer . push_back ( t) ;
return t ;

. ,
, ( #include)
Token_stream : : putack ( ) .
, , putack ( )
Token, buffer
Token, vector<Token>,
push_back ( ) . ,
,
.
:
Token t = ts . qett ( ) ; 11 : ge t t
11 . . .
ts . pu tack ( ) ; 11 :

; token . h
, .
s td_lib_facilities . h
, cou t, vector sqrt ( ) ,
, error ( ) .
8. 4. 31 3

. 1 2. 8

.
.
, ,
(, -
, ).

8.4.
(scope) - .

(.. ),
,
. .
void f ( )
{
q() ; 11 : g () ( )

void q ( )
{
f() ; 11 : f ()

void h ( )
{
int = ; 11 : ( )
11
int = ; 11 :
q() ; 11 : g ()

, ,
. . f ( )
g ( ) , "
. h
. , , ,
, - , f ( )
g ( ) .
,
.
(global scope):
, .
31 4 :

(namespace scope):
.
( 8. 7).
(class scope): .
( 9.2).
(local scope):
{ . . . } .
: . for.
-
. .
. .
void f ( int ) 11 f ;
11 f

int z = +7 ; 11 z

int q ( int ) 11 g ;
11 g

int f = +2 ; 11 f
return 2 *f ;

.
R :

'D

f ( )
g ( ) .
.

: f ( )
f ( ) .
g ( ) g ( ) .
(clash) .
f g ( ) (
) f ( ) .
.
.
8.4. . 31 5

int ma.x ( int , int ) // ,


//
-

return (>=) ? : ;

int as ( int ) 11
// ()

return (<) ? - : ;

max ( ) s ( ) ,
. ? :
{ (arithmetic if) (conditional expres
sion). (>=) ? : , >=, -
.
.:
int ax ( int , int // ,
11
-

int 111 ; 11
if ( >=)
111 = ;
else
111 = ;
return 111 ;

.
.

, .
, , ,
, ,
. :
.
.
,
,
( ).
11 r, i v
class y_vector
{
vector<int> v ; // v
11
pulic :
31 6 :

int larqest ( )

int r = ; // r (
// )
for ( int i = ; i < v . size ( ) ; ++i) // i
r = max ( r , as ( v [ i ] ) ) ; // for

11 i
return r ;
)
11 r
};
// v

int ; // -
int ;

int f ( )

int ; // ,
//
= 7; //

int = ; //
11
11
++ ; //

++ ; // f ()
return ;

I , .
" !"
,
: , , z.
. .
, , ,
.
. , ,
, .
? ? ?
? .
, ,
! (.
) - . .
, 6 7,
: ts na.mes.
8.4. 31 7

, ++.
, .
: - ( 9.4.2).
class {
pulic :
void f ( ) ;
void q ( ) // -
{
11
}
11
};

void C : : f ( ) // -
{
11 . . .

.
: - ( - ).
class {
puic:
struct
11
};
11
};
; ,
.
: .
void f ( )
{
class L {
11
};
11

; ,
, .

: (
).
void f ( )
{
void q ( ) //
{
11 . . .
31 8 :

}
11

++ ; -
.
( - ).
void f ( int , int )
{
if {>)
11
}
else {
11
{
11

11
}

,
.
++ - name
space,
( 8. 7).
,
.
:
11
s truct {
void f ( int ) {
s truct {
int f ( ) { return 1 ; } int m; } ;
int m ;
m=x ; 1112 ;
return f (lll2 . f ( ) ) ;
int m ; void g ( int m) {
if (m) f (m+2 ) ; else {
g (m+2 ) ; } }
( ) { } void m3 ( ) {
}

void main ( )
; a . f (2) ; }
};

.
, -
8.5. 31 9

(
). , "
", (
).
.

8. 5 .
.
-, ,
. ++ (, + *), -
,
, (, for if),
.
, .

, , . ,
.

8.5. 1 .
++
.
,
. :
doue fct ( int , doue d) ; 11 fc t ( }
doue fct ( int , doue d)
{ return a*d; } // fc t

(,
), , ,
.
(parameters). ,
, , :
int current_Power ( ) ; //

, ,
void, :
void increase_Power ( int level ) ; //

void " ".


, . ,
.
.
320 :

// s vs ;
// vs [hin t }
// ; -1 ,
//
int y find (vector<tring> v , //
_
tring , int hint) ;
int y_find ( vector<tring> , //
s tring , int) ;

r
,
. -
my_find ( ) , :
, .
r .
.
int y_find (vector<string> vs , tring , int hint)
// s vs , h i n t
{
if (hint < 1 1 vs . size ( ) <= hint) hint = ;
for (int i = hint ; i < vs . size ( ) ; ++i ) // ,
// h i n t
i f (vs [ i ] ==s) return i ;
if ( 0 < hint) { // r ,
11 h i n t
for ( int i = ; i < hint ; ++i )
if (vs [ i ] ==s ) return i ;

return -1 ;

hint ,
n, ,
. ,
my_find ( ) , ,
hint,
. hint ,
" " my_find ( )
hint. (
, ),
my_find ( ) .
r. , .
int y_find ( vector<string> vs , string s , int) // 3 -
//
for (int i = ; i < vs . size ( ) ; ++i )
if (vs [ i ] ==s) return i ;
8.5. 321

return -1 ;

ISO
++.

8.5.2.

return.
f ( ) // f { )
{
V v;
11 . . .
return v ;

= f() ;

- ,
v.
V v;
11 . . .
t (v) ; // t v
, - .
. . .
. " ":
doue _s ( int ) // :
{
if ( < 0 )
return - ;
else if ( > 0 )
return ;
// : = = ,


, "" = 1 ,
, ,
. . ,
return error ( )
.

1 , >= , ,


,
, . - . .
322 :

main ( )
. " " main ( )
return , .
, , return

, :
void print_until_s (vector<string> v, string quit)
{
for ( int s : v) {
if ( v [ i ]
== quit) return ;
cout << v [i ] << ' \n ' ;

, " " vid-.


" " return ; .

8.5..

,
. f ( ) , -
. .
11 (
11 )
int f ( int )

= x+l ; //

return ;

int main ( )
{
int = ;
cout f (xx) << ' \n ' ; 11 : 1
cout << << ' \n ' ; 11 : ; f ()

int = 7 ;
cout << f () << ' \n ' ; 11 : 8
cout << << ' \n ' ; 11 : 7 ; f ( )

, x=x+l
f ( ) ,
.
.
8.5. 323

: :

: 1
1



: :

: 1 7
1

7

.
.

8.5-'.
, ,
, int, doue
Token (. 6.3.2).
(
), (, )
(, )?
. ,
,
,
, . ,
,
:
void print (vector<doule> v) // ; ?
{
cout << " { " ;
for ( int i = ; i < v . si ze ( ) ; ++i ) {
cout << v [ i ] ;
if ( i ! =v . size ( ) -1) cout << " " ;

cout << " } \n" ;

print ( ) :
void f ( int )
{
vector<doule> vdl ( l O ) ; 11
vector<doue> vd2 ( 1000000 ) ; 11
vector<doue> vd (x) ; 11
// . . . vdl , vd2, vd . . .
print (vdl ) ;
print (vd2 ) ;
print (vd) ;
324 :

, print ( )
doue (, 80 ), -
doue (, 8 ),
. " -
?" ,
. ,
. ,
, ,
, -
, .
- print ( )
"" . . ""
(reference) , :
void print ( const vector<doule>& v) //
{ // U1
cout << { 1 1 ;
11

for ( int i = ; i < v . size ( ) ; ++i ) {


cout << v [ i ] ;
11 11 .
if ( i ! =v . size ( ) - 1 ) cout << '

cout << 11 ) \n 11 ;

& , const
print ( ) .
, . .
, , ,
. . "
" , . print ( )
, .
void f ( int )
{
vector<doule> vdl ( l O ) ; //
vector<doule> vd.2 ( 1000000 ) ; //
vector<doule> vd (x) ; 11
11 . . . vdl , vd2 , vd . . .
print (vdl ) ;
print (vd.2 ) ;
print (vd ) ;

.
8.5. 325

v:

vd2

vd2:

:
. ,
,
print ( ) , - ,
.
void print ( const vector<doue>& v) //
{ //
11 . . .
v [ i ] = 7 ; // : v ( )
-

11 . . .

-
. my_find ( ) ( . 8 . 5 . 1 ),
.
.
int .y_find (vector<strinq> vs) ; 11 :
s trinc;r s ) ; 11

,
. my_
find ( ) , .
// : ,
int .y_find ( const vector<strinq>& vs , const strinq& s ) ;

8.5.5.
, ,
? . ,
init ( ) ,
.
void init (vector<doule>& v) //
{
for (int i ; i < v . size ( ) ; ++i ) v [ i ] = i ;

326 8 :

void g ( int )
{
vector<doule> vdl ( lO ) ; 11
vector<doule> vd2 ( 1000000 ) ; //
vector<doule> vd (x) ; //
init (vdl ) ;
ini t (vd2 ) ;
init (vd ) ;

. ini t ( ) .
r. . (..
). (..
). .
. - .
. . int& -

int.
:
int i = 7 ;

int& r = i ; // r i
-

r = 9; // i 9
i = 10 ;
cout << r << ' ' << i << 1 \n ' ; // : 1 0 1 0

. r
i .
.
:
vector<vector<doule> > v; // do ue

. v [f () ] [g () ]
. , v [f () ] [g () ]
, .
. :
doule val = v [ f (x) ] [q (y) ] ; // v [ f (x) } [g (y) }
val .
v [ f () ] [ g () ]

v [ f () ] [g () ] ? .
doule& var = v [ f (x) ] [ g (y) ] ; // v [ f (x) } [g (y) }

. v [ f (x) ] [g (y) ]
var:
var = var/2+sqrt ( var) ;
8.5. 327

- ""
.
. .
11
int f ( int& )

= x+l ;
return ;

int main ( )
{
int = ;
cout << f (xx) << ' \n ' ; 11 : 1
cout << << ' \n ' ; 11 : 1 ; f ()
11

int = 7 ;
cout << f {yy) << ' \n ' ; 11 : 8
cout << << ' \n ' ; 11 : 8 ; f {)
11


.
:

( )

:
7

8.5.3.
, -
:
, . , -
- .
. ,
doue:
void swap (doue& dl , doue& d.2 )
{
doue temp = dl ; 11 dl t emp
dl d.2 ; 11 d2 dl
d2 = temp ; 11 dl
11 d2
328 :

int main ( )
{
doue = 1 ;
doue = 2 ;
cout << " =" << << " == "

<< << ' \n ' ; 11 : x==l ==2


swap (x , y) ;
cout << " =" << << " == "

<< << \n ' ; / / : ==2 y==l

swap ( ) ,
,
swap ( ) .

8.5.6.

,
?
.
void f ( int , int& r, const int& cr)
{
++ ; //
++r ; // , r
++cr ; 11 : cr-

,
: ,

. :
void q ( int , int& r , const int& cr)
{
++ ; 11
++r ; 11 , r
int cr ; // , cr

int main ( )

int = ;
int = ;
int z ;
q (x , y , z ) ; 11 ==; y==l ; z==O
q ( l , 2 , 3 ) ; // : r :
q (l ,y , 3) ; 11 cr ,
8.5. 329

, ,
, . .
2 ( , rvalue , ..
-

), . .
r f ( ) lvalue (.. ,
).
, lvalue
. ,
. g ( l , y , 3)
int cr g ( ) ,
:
g ( l , y , 3 ) ; 11 : i n t _ compi ler_genera ted = 3;
11 g ( l , y, compi ler_genera ted)

. ,
(temporary object) .
,
. .
1 . .
.
2. . ,
. .
3. . , .
.
4. . -
.
, u
.
int, doue
. ,
, ,
.
, ,
. .
int incrl ( int ) ( return a+l ; 11
void incr2 ( int& ) ( ++ ; } 11 ,
11

int = 7 ;
= incrl (x) ; 11
incr2 (x) ; 11
330 :

-
? .

(, )
.
, ( ++
return
).
.
void larqer (vector<int>& vl , vector<int>& v2 )
// :>t vl
// vl v2;
// :>t v2
11

if (vl . size ( ) ! = v2 . si ze ( ) )
error ( " larqer ( ) : " ) ;
for ( int i=O ; i < vl . size ( ) ; ++i )
if (vl [ i ] < v2 [ i ] )
swap (vl [ i ] , v2 [ i ] ) ;

void f ( )
{
vector<int> vx ;
vector<int> vy ;
// vx vy
larqer ( vx , vy) ;
11 . . .

-
larger ( ) .
,
. ,
, .
,
, .
. Fortran - ,

, - .
, ,
Fortran, , .
.
8.5. 331

,
, .
, , -
, , . ,
, ,
, ,
, .
, ,
.

8.5. 7 .

, .
.
void f ( T ) ;
f (y) ;
= ; //
11 ( . 8 . 2 . 2)

f () , = ;
.
.
void f (doue ) ;

void q ( int )
(
f (y) ;
doule = ; //
11 (. 8 . 2 . 2)

,
int doue.
f ( ) . doue,
f ( ) , ,
.
,
( 3 . 9.2). ,
, . -
doule .
int, .
void ff ( int ) ;

void qq (doule )
332 :

ff () ; // , ?
int = ; 11 , ?

doue
int, .
void qqq ( doule )
{
int xl = ; 11
int 2 int (x) ;
int static_cast<int> (x) ; 11 (1 7 . 8)

ff (xl) ;
ff ( x2 ) ;
ff (x) ;

ff (x) ; //

ff ( int (x) ) ;
ff ( s tatic_cast<int> ( x) ) ; 11 (1 7 . 8)

,
.

8.5..
?
expression ( ) , term ( ) primary ( ) , 6 7,
:
,
. !
: ,
. ,
ts Token_s tream
: ts .
. ,
Token_stream&.
.
, expression ( ) , :
(ts) (left t).
doule expression ( Token_stream& ts)
{
doule left = term ( ts ) ;
Token t = ts . qet ( ) ;
11 . . .
8.5. 1 333

term ( ) expression ( ) , i
, (d) ,
( case ' / ' ) .
doule term ( Token_stream& ts)
{
doule left = primary ( ts ) ;
Token t = ts . get ( ) ;
11
case ' / ' :

doule d = primary ( ts ) ;
11 . . .

11

primary ( ) term ( ) ,
, left.
doule primary ( Token_stream& ts)
{
Token t =ts . get ( ) ;
switch ( t . kind) {
case ' ( ' :

doule d = expression ( ts ) ;
//
1
11

,
:
, . ,
,
, ,
, .

,
. ,
expression ( ) ,
.
334 :

expression(): ts
left
'

" " .
,

. 11 11
(functton acttvation record) .
. .

.
expression ( ) term ( ) .
term ( ) .

expression(): ts
left
t



term(): ts
left
t
d

, term ( )
d, .
,
. . (
)
. d
, case ' / ' .
term ( ) primary ( ) .
.
.5. 335

expressio(): ts
left
t

term(): ts
left
t

d

primary( ) : ts
t
d

, primary ()
expression ( ) .

expressio(): ts
left
t

term(): ts
left
t
d

primary(): ts
t
d

expression(): ts
left
t


336 8 :

expression ( )
,
expression ( ) . ,
, left t
. ,
( ) , 11
(recursive). ,
,
( ).
, , ,
(stack of actlvatlon records),
(stack) , . ,
, . ,
expression ( )
primary ( ) , .

expression(): ts
left
. . ..
t

term(): ts
left
t

d

primary(): ts
t
d

primary ( ) term ( ) ,
, .
8.5. 337

expression(): ts
left
t



term(): ts
left
t
d

. . (call
stack), ,
-

" - ".
,
++, , .
, ? , ;
,
" " " ",
, .

8.5. 9 . ns tr-
,
.
,
. ,
, , ,
.
,
constexpr. ,
,
:
constexpr doule l 10 ;
= //
constexpr doule l .;
=

constexpr Point scale ( Point p) { return { xscale*p . x , yscale*p . y ) ; ) ;

, Point
, . ,
scale ( ) Point,
, xscale yscale,
:
338 :

void user ( Point pl)


(
Point 2 ( 10 , 10 } ;

Point scale (pl) ; // : == ( 1 00, 8 ) ;


//
Point 4 = scale (p2 ) ; // 4 == ( 1 00 , 8 )

constexpr Point pS scale (pl ) ; 11 : sca l e (p l )


//
constexpr Point = scale (p2 ) ; 11 == ( 1 00 , 8 )
11 . . .

constexpr ,
- , ,
. , r
(, 2),
, (, pl).
, , constexpr.
, ( ,
) . ++ 1 1 , ,
constexpr, , return
( scale ( ) ): ++ 1 4
. ntr-
, . .
, ,
.
, :
int qob = 9 ;

constexpr void bad ( int& arq) / / :


( //
++arq ; // :
//
qlob = 7 ; // :

, nstr-
" " (
), .

8.6.

. " "
8.6. 339

, , ..
, .
. , .. ,
, ,
. .
strinq proqram_name " silly" ;
vector<strinq> v ; 11 v -
void f ( )
{
strinq s ; // s - f
while ( cin>>s && s ! ="quit" )
{
strinq stripped ; // s t ripped -
strinq not_letters ;
for ( int i=O ; i < s . s i ze ( ) ; ++i) // i
11
if ( isalpha ( s [ i ] ) )
stripped += s [ i ] ;
else
not letters += s [i ] ;
v . push_back ( stripped) ;
11
1
11

, progra.m_na.me v,
main ( ) . ,
, .
(.. progra.m_na.me
v) , (..
v progra.m_na.me).
- f ( ) .

s; . s
. , f ( )
. , while,
stripped not_letters.
stripped not_letters,
stripped. .
( , not_
letters stripped) ,
. ,
qui t, , stripped
not_letters .
340 :

, for, i.
, for, i
, v . push_back ( s tripped) ; .
, ( )
(
), ,
. ,
, .

8.6. 1 .
,
.
. , -
. :
,
. .
v [ i ] = ++i ; 11
v [++i ] = i ; 11
int = ++i + ++i ; 11
1 1
cout << ++i
<< i ' \n ' ; //
f ( ++i , ++i) ; 11

,
: , ,
,

. - :
.
, = ()
. ,
,
. v [ ++i ] = i
.

8.6.2.
( :
8 . 7)
, . .
// fl .
int xl 1;
int yl = 1+2 ; // yl 3
8.6. 341


main ( ) .
,
, . ,
, ,
( 8.4).
,
. .
11 f2 . cpp
extern int yl ;
int 2 1+2 ; // 2 2 5

:
, ,
.
fl .
f2 , 2 5 (
.

). ,
f2 fl ,
. .

2 2 ( ,
,
).
;
, ,
.
.
( ) ?
Date,
. -:
const Date default_date ( 1970 , 1 , 1 ) ; // - 01 . 01 . 1 9 70

, default_date
? ,
.
, .
.
const Date default_date ( ) //
1
return Date ( 1970 , 1 , 1 ) ;

Date ,
defaul t_date ( ) . ,
default_date ( ) ,
342 :

Date .
.
:
const Date& default_date ( )
{
static const Date dd ( 1970 , 1 , l ) ; // dd
!/
return dd ;

()
. .
. .
, , - ,
.
( 8.5.6)
.

8. 7 .
(
8.4). .
( 9).
.
,
.
, .
, .
r
. .
,
(namespace). .
Color, Shape. Line, Function Text ( 1 3) .
namespace Graph_li
struct Color /* */ } ;
s truct Shape /* */ 1 ;
struct Line : Shape /* */ } ;
struct Function : Shape /* */ } ;
struct Text : Shape /* */ } ;
11 ...
in t qui_ain ( ) /* */
. 7 . 343

, - ,
. - Text,
- , .
Graph_lib : : Text, - Text.
,
Graph_lib, Text. Graph_
lib ; . "
" Graphics - .
, Text
. .
Graph_lib, .
, , -
TextLib.
namespace TextLib
class Text /* */ } ;
class Glyph /* */ } ;
class Line /* */ } ;
11 . . .

,
.
Text Line. ,
, .
.
; . Text -
Graph_lib : : Text, TextLib : : Text. ,
-

( )
, : : , . . (fully
qualified name).

8. 7 . 1 . us inq us inq
.
, ++
s td :
#include<strinq> // s t ri n g
#include<iostream> // ios t ream

int main ( )
{
std : : strinq name ;
std : : cout << " . , \n" ;
std : : cin >> name ;
344 :

std: : cout << " 8'1' , " << name << ' \n ' ;

string
cout,
- std : : string std : : cout. :
, s tring
std : : string, cout - s td : : cout ..
using std : : string ; // s t ri n g s td : : s t ri n g
using std : : cout ; // co u t s t d : : co u t
11 . . .

using.
"", . ,
.
""
: "
, std".
, using.
using namespace s td ; //
11 s t d

.
#include<string> 11 s t ri n g
#include<iostrea> 1 1 ios t ream
using namespace s td ; 11 s td

int main ( )

s tring name ;
cout << " , \n " ;
cin >> name ;
cout << " , " << name << ' \n ' ;

in - std : : cin, string - o std : : string


.. std_lib_facilities . h,

std.
using
, ,
,
std. ,
using, , ,
,
.
8. 7 . 345

using ,
using (
) - . .
using std
std_lib_facilities . h. .
:
llinclude " s td lib facilities . h"
int ain ( )
{
strinq name ;
cout << ". , \n" ;
cin >> name ;
cout << " , << name << ' \n ' ;
"

,
std.

1 . : my . h, my . use . . my . h
:
extern int foo ;
void print_foo ( ) ;
void print ( int) ;

my . cpp llinelude my . h
std_l ib_facilities . h print_foo ( ) (
foo eout) print ( int i) (
cout i ) .
use . cpp #inelude
my . h, main ( ) .
foo 7
print_foo ( ) . 99
print ( ) . , use . cpp
#inelude std_lib_faeilities . h,
.
.
Windows. .
use . my . use .
{ ehar ; ein >> ; } . : ein
#inelude <iostream>.
2. : swap_v ( in t , int) . swap_r ( i nt& , int&) swap_
er (const int& , eonst int&) .
346 :

{ int t ; t = , = ; =t ;

- .
, .
int = 7 ;
int 9 ;
swap_? (x , y) ; // v, r cr
swap_? ( 7 , 9 ) ;
const int = 7 ;
const int = 9 ;
swap ? ( cx , cy) ;
_
8W&p_? ( 7 . 7 , 9 . 9 ) ;
doule dx 7.7;
doule dy 9 . 9 ;
swap_? (dx , dy) ;
swap_? ( 7 . 7 , 9 . 9 ) ;

?

, ,
. , 8.6.
. , ,
. z, . main ( ) ,
, .
int main ( )
{
: : var = 7 ;
: : print ( ) ; // va r
using namespace ;
var " 9 ;
print ( ) ; // va r
{
using Z : : var ;
using Z : : print ;
var 11;
print ( ) ; / / va r Z

print ( ) ; // va r
: : print ( ) ; // va r


var print ( ) .
var cout.
8. 7 . 347


1 . , ?
2.
?
3.
?
4. 6 ,
?
5. int ; -
?
6. -
?
7. ?
8. ?
9. ?
1 . ?
1 l . .
.
1 2. ,
?
1 3.
?
1 4. ,
?
1 5. , m
?
1 6. swap ( ) ?
1 7. , vector<doue>,
?
1 8.
. ?
1 9. && 1 1 ?
20. ++ :
, ,
, ?
2 1 . ?
22. ?
348 :

23. ?
24. ?
25. using.
26. using ?
27. s td.

const
constexpr
extern
namespace
return



-

-

usinq usinq

1 . - 7,
( 8. 5.8). ,
Token_stream ( 7.8.2)
istream& . , ,
(, ),
. 11:
i s tream.
2. print ( ) . cout
. : "
vector.
3.
. 2. ,
fibonacci ( , , v , n) ,
int, v vector<int>,
n - , v:
v [ O ] , v [ l ] - . -
,
. 7 . 349

. .
1 2. 1 . 2, 3, 5, 8, 1 3, 2 1 , . "

. fibonacci ( )
. .
4. int ,
.
fibonacci ( ) .
5. ,
vector<int>. , 1 , 3, 5, 7, 9
9, 7, 5, 3, 1 . ,
,
vector, vector
.

. 11: swap.
6. 5 vector
<string>.
7. vector<string> name,

vector<doue> age.
(name [ i ] , age [ i ] ) . ( sort (name . egin ( ) , name . end ( ) ) )
(name [ i ] , age [ i ] ) .
, age,

name. 11: name
age
name. ,
.
8. , vector
<doue>. price weight, .
price [ i] *weigh t [ i ] . weigh t .
size ( ) price . size ( ) .
=

9. maxv ( ) ,
, .
1 . ,
, ,
. .
struct,
.
?
350 :

1 1 . print_until_s ( ) 8.5.2.
.
? . print_until_
ss ( ) , ,
qui t.
1 2 . , vector<string>
vector<int>.
. .
.
.
? ?
1 3 . ,
(, void f ( const int) ; )? ?
?
? ; .
. .

( )
. 1 1 , " "
, . .
. .

. ,
, . ""
.
:

r : la -
- r (?...! .//-.;_)


: ++ .
,

, - .

Date.

.
352 9 :

9. 1 . , 9.5 .
9.2. 9.5. 1 . ""
9.3. 9.6.
9.4. 9.7.
9.4 . 1 . 9.7. 1 .
9.4.2. - - 9.7.2.
9.7.3.
9.4.3. 9.7.4. -
9.4.4. - 9.7.5.
9.4.5.
9.4.6. 9.8. Date

9 1 ,
. .

++ , char, int doule


( .8).
, ,
(, + *)
, .
, ,
, (user
defined types UDT). -
-

, ++,
ISO (, string, vector ostream,
1 0), , ,
Token Token_s tream (. 6 . 5 6.6).
, ,
Shape, Line Text ( 1 3) .
, ,
,

, , :
, .
, , ,
.
, vector [ ] size ( ) ( 4.6. 1 .4.8),
ostream <<. Token_stream
- get ( ) (
-

6.8), Shape add ( Point) set_color ( ) ( 14.2).


-

? ,
.
, -
9. 1 . , 353

-
. .
? ? , .
. ,

, , ,
.
, int;
, s tring;
,
Token Token_stream. .
r. ,
.
. . .
: ""
(
) , .
, -. -, ,
, ;
.
.
- " " .
, "" ""
. " ?"
.
++.
++
: .
, -
, , .
. (class) -
() .
, , ( 1 7.5).
- . . ,
, "" .
, , , ,
. , ,
, , , , , .
++ ( )
,
.
(. 6 7).
354 9 :

9 2
. .

- .
, . ,
,
(members) . ,
. .
class {
puic :
int m ; // -
i n t m.f (int v) { 11 -
int old = m; m.=v ; return old ;

};

.
-, .
-,
.
. , :
var ; / / va r
-
var . m = 7 ; // va r
int = var . m.f ( 9 ) ; /! - m f () va r

, .
, int,
..
-, mf ( ) ,
var . m. (
- m) . -
, - . ,
var . mf ( 9) m mf ( ) var . m.

9
. .

, . -
,
. - , -
. .
puic : . -
priva te : . :
class { 11
pulic :
11 :
9 3
. .
355

// - ( )
11
11
11 ( priva te)
private :
// :
// - (
11 )
11
11
11
1;

. ,

class {
int mf { int) ;
11 . . .
1;

class {
private :
int mf ( int) ;
11 . . .
1;

; //
int = x . mf ( ) ; // : mf
11 (. . )


. -
, , :
class {
int m;
int mf ( int} ;
pulic :
int f ( int i ) m=i ; return mf ( i ) ; }
1;

;
int = x . f ( 2 ) ;


( )
( ).

356 9 :

. . ,
, .
,
. struct
class, :
struct {
int m;
11 . . .
};

class {
pulic :
int m;
11 . . .
};

.
; ,
( 9.4.3).

9 .4.
,
. , - -

. :
(, . 1 9 1 963 )
. (
, , . .
..). . .

9 4 1
. . .

?
: , ".
, .
.
11 Da te ( ?)
struct Date {
int ; //
int m; //
int d ; //
};

Date today ; // Da te ( )
9 .4 . 357

Date, today,
int.
Date:

42
;:
d:
,
Date, -
.
, Date; ?
, ,
today ( Date),
. i ,
. , Date,
. .
11 2 0 2004
today . y 2004 ;
=

today . 111 20 ;

today . d " 2 ;

.
? , ,
! , ,
; ?
Date ;
. -3 ;
=

. 111 13 ;
=

x . d = 32 ;

, , .
? ?
Date ;
. = 2000 ;
. 111 " 2;
y.d 29;

? ?
, ,
.
,
.

. Date
Date. , ; :
358 9 :

11 :
void init_d&y (Date& dd , int , int m, int d)
{
11 , (y , m , d) ,
11 , , dd

void add_d&y (Date& dd , int n)

11 dd

Date.
void f ( )
{
Date tod&y ;
init_d&y ( tod&y , 12 , 2 4 , 2005 ) ; 11 ! ( 1 2 - Ui
11 2 0 0 5 - . )
add_d&y ( tod&y , l ) ;

"" -
.
, ,
. , , -
.
,
- .
.
( , - ),
, "
?"

9 .4.2. -
Date,
.
, . , ,
Date << ( 9 . 8):
void f ( )
{
Date tod&y ;
11 . . .
cout << today << ' \n ' ; // today
11 . . .

init_d&y ( tod&y , 2008 , 3 , 30 ) ;


11 . . .
Date tomorrow;
9 . 4 . 359

tomorrow . y today . y ;
=

tomorrow . m today . m ;
=

tomorrow . d today . d+l ;


= 11 today
cout << tomorrow << ' \n ' ; 11 tomorrow

"" today.
ini t_day ( ) . ,
"-" , add day ( ) -
_

( ), tomorrow
. - . .
. . .
. .
Date
"", d
: today
.
. . .

. . .
.
u-'-!" . . ,
, :
11 Da t e ,
//
//
struct Date {
int , m, d ; // , ,
Date ( int , int m, int d) ; 11
11
void add_day (int n ) ; 11
};

-, , .
(constructor)
() .
, ,
, .

.
Date y_irthday ; 11 :
Date today ( 12 , 2 4 , 2007 } ; 11
Date las t ( 2000 , 12 , 3 1 } ; 11 ( )
Date next = { 2014 , 2 , 14 } ; // ( )
Date christmas =

Date ( 1976 , 12 , 24 } ; 11 ( )
360 9 :

my_irthday ,
. today
,
( { 12 , 24 , 2007 } 2007- -

24- 1 2- ).
last
- ,
Da te, { }
.

, ,
.
,
( Da te { 1 97 6 , 12 , 2 4 } ) ,
=.
, .
""
.
last . add_day ( l) ;
add_day ( 2 ) ; 11 : Da t e ?

, - add day ( )
Date (
). - ,
9.4.4.
++98
, :
Date last ( 2000 , 12 , 31 ) ; // ( )


{ } , ,
() ,
.
, :
int ( 7 } ; // Ok ( )

{ } =:
Date next = { 20 14 , 2 , 14 } ; // ( )

9. 4 . .
: , -
- add_day ( ) ? . -
9 .4 . 361

? , ""
.
Date birthday { l960 , 12 , 31 } ; // 31 1 9 60
++irthday . d ; / / ' !
// bi rthda y . d == 32

Date today { l970 , 2 , 3 } ;


today . m = 14 ; // ' !
11 toda y . m == 14

, Date , - -
- ; .
, . -
Date ,
.
; - -. ,

- - -
.
, Date,
-,
. , :
11 ' ' Da t e ( )
class Date
int , m, d ; // , ,
pulic :
Date ( int , int m, int dl ; 11
void add_day ( int n ) ; // Da te
int month ( ) { return m;
int day ( ) { return d ;
int year ( } { return ;
1;

:
Date irthday { l 970 , 12 , 30 } ; 11
irthday . m = 14 ; // : Da t e : : m
cout << irthday . month ( ) << endl ; //

" Date" -
.
. ;
, , ,
. -
.
. (state),
- .
362 9 :


.
. ,
.
, ,
.
. .
.
, ,
(invariant). Date (W
Da te .
") :
, ,
. .
Date - . .
, ,
. .
. , ,
. struct.

9 -
. . .

Date
.
-. ,
Date,
.
11 Da t e ( )
class Date {
pulic :
Date ( int , int m , int d) ; //
void add_day ( int n) ; /! Da t e
int month ( ) ;
11 . . .
private :
int , m, d; 11 , ,
};

.
. ,
. .
, ,
, -.
, ,
.
9 4
. .
363

,
; .
.
, ,
. u_ :
: .
Date : : Date ( int , int mm., int dd) //
: { ) , m{mm) , d { dd ) / /

void Date : : add_day ( int n)


{
11 . . .

int onth ( ) // : Da t e : :

return m; // - ,

: { } , m { mm } , d{dd}
().
:
Date : : Date ( int , int mm., int dd) //
{
= ;
m = mm ;
d = dd ;


, ,
. ,
.
: { } , m { mm } , d{dd } .
.
int ; // . . .

11 . . .
= 2 ; // . . .


int { 2 } ; //

- .
11 Da t e (
11 )
364 9 :

class Date {
pulic :
Date ( int , int m, int dd)
: { } , m{ m} , d { dd }

11 . . .

void add_day ( int n)


{
11 . . .

int month ( ) { return m; }

11 ...
private :
int , m, d; // , ,
};

,
. add_day ( )
.
.

.
month ( ) .

, Date : : month ( ) ,
.
.
, month ( )
m, , m
() month ( ) .
, . ,
,
, .
-
.
m11 (inline), . .
.

, month ( ) .
-
, .
,
.
9 4
. .
365



.
. ,

-.
: -
,

. , , ,
.
, - .

9. , . 5 .
Date.
class Date {
11 . . .
int month { ) { return m ; }
11 . . .
private :
int , m, d; // , ,
};

void f (Date dl , Date d.2 )


{
cout << dl . month ( ) << ' ' << d2 . month ( ) << ' \n ' ;

Date : : month ( ) ,
dl . m, d2 . m?
-

Date : : month ( ) : !
Da te : : mon th ( ) "", ?
- , Date : : month ( ) ,
, ,
. , m
dl m, - d2 . m.
.

1 7. 1 0.

9 .,.6.
?
?
5.6 , "-
", -
366 9 :

Date.
Date - .
Date .
Date.
11 Da t e ( )
class Date {
pulic :
class Invalid }; //
Date ( int , int m, int d) ; 11
11 . . .
private :
int , m, d; // , ,
bool is_valid ( ) ; 11 , t rue
};

is_
valid ( ) .
, ,
. , ,
:
Date : : Date ( int , int llllD. , int dd)
: { } , m{ llllD. } , d { dd } // -

if ( ! is_valid ( ) ) throw Invalid { } ; //

bool Date : : i s_valid ( ) // true


{
if (m<l 1 1 12<m) return false ;
11

Da te :
void f ( int , int )
try {
Date dxy { 2009 , x , y } ;
cout << dxy << ' \n ' ; 11 << . 9 . 8
dxy . add_day ( 2 ) ;

catch (Date : : Invalid)


rrr ( " " ) ; // error ()
// 5 . 6 . 3

, << add day ( )


Date.
Dt 9.7
, ,
: .
9.5. 367

9 . 5.
enum (enumeration)
-

, (
) :
enum class Month {
j an=l , f , r , apr , , jun , jul , auq , sep, oct , nov , dec
};

''" .
class enum class ,
. . . , j an,
n th : : j an.
,
jan,
. ,
,
. ,
Month ,
. ,
enum class Month {
j an=l , f=2 , ma.r= , apr=4 , ma.y=S , jun= ,
jul=7 , auq=B , sep=9 , oct=lO , nov=l l , dec=12
};

.
, , ,
; ,
, "" .
, . .
,
:
enum class Day {
monday , tuesday , wednesday ,
thursday , friday , saturday , sunday
};

monday=O sunday== .
.
Month :
Month m = Month : : fe ;

Month m2 = f ; 11 : feb
. = 7 ; 11 :
11 i n t Mon th
368 9 :

int n = m; // :
11 Mon th i n t
onth n = onth ( 7 ) ; 11 ()
11 i n t Mon th

onth , ""
int. onth
, int
onth. ,

onth bad = 9999 ; 11 :


11 Mon th

onth ( 9999) ,
. ++

, ; ,
! ,
onth { 9999 } , ,
Month; int
.
, .
,
.
onth int_to_month ( int )
{
if ( < int (Month : : j an) 1 1 int (onth : : dec) < )
rrr ( " " ) ;
return Month (x) ;

int (Month : : jan)


onth : : j an. , :
void f ( int m)
(
onth n = int_to_month (m) ;
11 . . .

? ,

. ,
aop aepa (up, down; yes , no, maybe; on, off; n, ne, e, se, s , sw, w, nw)
(red, u. green, yellow, maroon, crimson,
k).
9 .5. 369

9 . 5.1 . " "


enum class,
(scoped enumeratlon), "
, ,
"
int, :
enum onth { // "cl a s s "
j an=l , f , .r , apr , . , j un , jul , aug , sep , oct , nov , dec
};

onth m = f ; // : feb
Month m2 = Month : : fe ; // :
m = 7; // :
11 i n t Mon th
int n = . ; // : t h
11 i n t
Month IDID. = onth ( 7 ) ; // i n t
11 Mon th ()

, " ,
enwn class . " ,
. ,
. ,
Month iostream ( 1 1 .2. 1 ),
, , dec
, .
int
(
int) . , :
void my_code (onth .)
{
if (. == 1 7 ) do_som.ething ( ) ; // 1 7 - ?
if (. monday) do_som.ething_else ( ) ; //
==

11 ?

onth enwn class,


. monday " enwn,
enwn class, , ,
, .
enum
class , ""
enwn: enwn class ++ 1 1 .
370 9 :

9 6
. .


, ++.
(operator overloadlg). ,
.
.
enu.m class Month
Jan=l , F , &r , Apr , & , Jun , Jul , Auq , Sep , Oct , Nov , Dec
1;

Month operator++ (onth& m) 11

m = (m.==Dec) ? Jan : Month ( int (m) +l ) ; //


return m;

? : ":
m Jan, (m--Dec) , Month (int (m) + 1 )
.
. Month :
Month m = Sep ;
++m; // Oc t
++m; // Nov
++m; // Dec
++m; // Jan ( )

,
Month ,
.
? :
vector<strinq> month_t ;

ostream& operator<< (ostream& os , Month m)


(
return os << month_tl [ int (m) ] ;

, month_t
, , , month_t [r]
"arch" - (.
1 0. 1 1 .3).
,
, ++,
+, -, *, /, %, [ ] , ( ) , ", ! , &, <. <=, > >=. -
; , , -
9. 7 . 371

** $=, ++ .

; , -.
<= (" ").
+,
! ("").
,
.

.
int operator+ ( int , int) ; 11 :
11 +
Vector operator+ ( const Vector& , const Vector &) ; 11
Vector operator+= ( const Vector& , int) ; 11


, ,
. , ,
: + ;
* - ; [ ] - ; ( ) -
.. , , :
, +
, . ,

. ,

. .
.
,
+, -, * , / ,
, =, ==, ! =, <, [ ] ( ) .

9 . 7 .
,
. ++
s truct,
.
?
?
, ,
++ .
372 9 :

.
.
.
( )
(. 1 4.2.4).
.
-
(. 9. 7.4).
(. 1 7. 5).
. 5.5, ,
.
: "
. ".
, . ,
,
. ,
, , - , .
.
, . ,
, ! .
, .
. , ,
.

, ++.

9 . 7 1
. .
Date 9.4.3,
int. .
Date dl { 4 , 5 , 2005 } ; // 4 , 2005
Date d2 { 2005 , 4 , 5 } ; // 5 4 ?

( )
. (
) .
. - ,
: , 4 / 5 ClllA 5 . - 4 .
,
- .
Month.
9 . 7 . 1 373

enum class onth {


j an=l , f , mar , apr , may , jun , jul , aug , sep , oct , nov , dec
};

// Da t e ( Mon th)
class Date
pulic :
Date ( int , onth m, int d) ; /!
11
11 . . .
private :
int ; 11
Month m ;
int d ; //
};

Month .
. , Month
. , ,
, , .
.
Date dxl { l998 , 4 , 3 } ; 11 : 2 -
11 Mon th
Date dx2 { 1998 , 4 ,Month : : r } ; 11 : 2 -
11 Mon th
Date dx2 { 4 ,Month : : r , 1998 } ; 11 :
11 1 998
Date dx2 {onth : : mar , 4 , 1998 } ; 11 : 2 -
11 Mon th
Date dx3 { 1998 , Month : : mar , 30 } ; 11

.
mar : Month : : mar.
Month . mar. Month (Month - ),
mar - ( -
). : : ,
( 8. 7). . () -
.
, ,
. ,
. ,
. ,
.

? ,
, Month; , ,
.
374 9 :

,
.
, , (
Date). - :
class Year // [mi n , max)
static const int in = 1800 ;
static const int = 2200 ;
pulic :
class Invalid { ) ;
Year ( int ) : { ) { if ( x<in 1 1 max<=x) throw Invalid { ) ; )
int year ( ) { return ; )
private :
int ;
);

class Date {
pulic :
Date ( Year , Month m, int d) ; 11
11
11 . . .
private :
Year ;
Month m ;
int d ; / /
);

.
Date dxl { Year { l 998 ) , 4 , 3 ) ; 11 :
11 - Mon th
Date dx2 { Year { 1 998 ) , 4 , onth : : mar ) ; 11 :
11 - Mon th
Date dx2 { 4 , Month : : mar , Year { l998 ) ) ; 11 :
11 - Yea r
Date dx2 {Month : : mar , 4 , Year { l 998 ) ) ; 11 : - Mon th
Date d { Year { l 998 ) , Month : : mar , 3 0 ) ; 11


.
Date dx2 { Year { 4 ) , Month : : mar , 1998 ) ; // :
11 Yea r : : Inva l i d


? , ,
Date.
Year.
, .
.
,
9 . 7 . 375

, .
, ,
, . , " -
".
static const
min max.
. -
static ,
,
. ,
, constexpr
const.

9. 7 . 2.
, -
. ,
: , ,
(..
).
.
,
?
Date onth :
, :
. .
, . , Date

, .
Date holiday { 1 978 , Month : : jul , 4 } ; //
Date d2 = holiday ;
Date d = Date { 1 978 , Month : : jul , 4 } ;
holiday = Date { 1 978 , onth : : dec , 2 4 } ; //
d = holiday ;

, . Date { 1978 , Month : : dec , 24 }


Date,
, :
cout << Date { 1978 , Month : : dec , 24 } ;


. ,
, .
376 9 :

?
(. 1 8.3).
(.
1 4.2.4).

9.7. .

. . ++
, ,
. , Date :
: Date ( int , Month , int) , ,
Date . .

. .
Date dO ; 11 :
Date dl { } ; 11 :
Date d2 ( 1998 } ; 11 :
Date d ( l , 2 , 3 , 4 } ; 11 :
Date d4 ( 1 , 1 1 jan 11 , 2 } ; 11 :
Date dS ( l , Month : : j an , 2 } ; 11 :
11
Date d ( dS } ; 11 :
11 JJI1

, ,
Date, - Date.
;
.
, ?"
.
strinq sl ; 11 : " "
vector<strinq> vl ; // :


. , vector string
,
.
{ }
. , ,
:
strinq sl = strinq ( } ; // :
1 1
""
vector<strinq> v l = vector<strinq> ( } ; // :
//
9. 7 . 377

.
string sl ; // : " "
vector<string> vl ; // :

, int doue,
. int{ } -
, doue { } - . .
, , -
. ,
string vector.
string ;
for ( int i=O ; i<s . size ( ) ; ++i) // :
11 !
s [ i ] = toupper ( s [ i ] ) ; 11 :
// !
vector<string> v ;
v . push_back ( "bad" ) ; // :
// !

s v ,
, (
: . 1 7.5) , .
. ,
! ,
. ,
. (. 9.4.3) .
, . .
:
string sl = " " ;
vector<string> vl { } ;

.
string 11 11 .
vector . { }
.
. u
, .

.
' .
Date
, ( ,
).

1 " . - .. .
378 9 :

class Date {
pulic :
11 . . .
Date ( ) ; 11
11 . . .
private :
int ;
onth . ;
int d ;
};

r .
XXI .
Date : : Date ( )
: { 2001 } , .{Month : : j an } , d { l }

.
:
class Date
pulic :
11 . . .
Date ( ) ; //
Date (year , Mon th , day) ;
Date ( int ) ; // 1
11 . . .
private :
int { 2001 } ;
Month . {Month : : jan} ;
int d {1} ;
);


:
Date : : Date ( int ) 11 1
: { }
{
if ( ! is_valid ( ) ) throw Invalid { ) ; //

Date ( int) (m) (d),


(Mon th : : j an 1).
, ,
uuuu (in-class initializer).
, ,
, r ( ).

9 . 7 . 379

, ,
8.6.2.
const Date& default_date ( )
{
static Date dd{ 2001 , Month : : j an , 1 ) ;
return dd ;

static , dd
,
defaul t_date ( ) .
defaul t_date ( ) . default_
date ( ) Date.
Date : : Date ( )
: y { default_date ( ) . year ( ) ) ,
m{ default_date ( ) . month ( ) } ,
d{ default_date ( ) . day ( ) }

,
, ; ,
. default_date ( ) .
Date,
Date :
vector<Date> birthdays ( lO) ;


.
vector<Date> birthdays ( lO , default_date ( ) ) ; 11
11 Da t e
vector<Date> birthdays2 = { 11
11 Da t e
default_date ( ) , default_date ( ) , default_date ( ) ,
default_date ( ) , default_date ( ) , default_date ( ) ,
default_date ( ) , default_date ( ) , default_date ( ) ,
default_date ( )
};

,
. vector<int>
( 1 8.2).

9 . 7 . 4. -
,
(, "" ), - ; ,
380 9 :

, .
,
const, :
void some_function (Date& d , const Date& s tart_of_term)

int = d . day ( ) ; 11
int = start_of_term. day ( ) ; // ( ?)
d . add_day ( ) ; 11
s tart_of_term. add_day ( ) ; 11

, d - ,
s tart_of_term - ; , some_function ( )
s tart_of_term.
? , ,
s tart_of_term (const) .
day ( ) day start_of_term?
, Date s tart_of_term .
day ( ) , .
day ( ) Da te.
, , day ( )
Date, .
,
.
, -
: , .
. .
clas s Date
pul i c :
11 . . .
int day ( ) const ; // :
11
Month DLOnth ( ) con s t ; // :
11
int year ( ) con s t ; 11 :
11

void add_day ( int n ) ; // :


11
void add_DLOnth ( int n ) ; // :
//
void add_j'ear ( int n ) ; // :
//
private :
int ; //
Month . ; 11
int d ; / /
9. 7 . 381

1;

Date d ( 2000 , onth : : jan , 2 0 } ;


const Date cd ( 2001 , onth : : fe , 2 1 } ;

cout << d . day ( } << " - "


<< cd . day ( ) << ' \n ' ; 11
d . add_day ( l ) ; 11
cd . add_day ( l ) ; // : cd -

const -
, -
. - ,
:
int Date : : day ( } const
(
++d; // :
11 -
return d ;

, .
,
.

9 . 7 . 5.
( ) ,
. ,
,
(.. -),
. ,
.
, "
" ; ,
- , ,
: ,
. ,
, .
Date! , ,
. :

next_Sunday ( ) , next_workday ( ) .. -
,
, .
.
382 9 :

. ,
. .
.
Date, ,
. 1 1 900 ,
(. . ).
-.
(helper functions).
Date next_Sunday ( const Date& d)
{
// d , d . day () ,
// d . mon th () d . yea r () ;
// Da te

Date next_weekday ( const Date& d) /* . . . */ 1

bool leapyear ( int ) { /* . . . */

bool operator == ( const Date& , const Date& )


{
return a . year ( ) = b . year ( )
& & . 1110nth ( )== . 1110nth ( )
& & a . day ( ) == b . day ( ) ;

bool operator ! = ( const Date& , const Date& )


{
return ! ( ;

-11,
11 .
, ,
: .
,
.
. (
:
leapyear ( ) ) .
(. 8. 7):
namespace Chrono {
enum class onth { / * . . . */ } ;
class Date { / * . " */ 1 ;
bool is_date ( int , Month m, int d) ; 11 t rue
11
Date next_Sunday ( const Date& d) { / * . . . */ }
9.8. Date 383

Date next_weekday ( cont Date& d) { /* . . . */ )


bool leapyear ( int ) { / * . . . */ } // . . 1 0
bool operator== ( cont Date& , cont Date& ) {
/* . . . */ }
11 . . .

== ! = .
. ! = ,
=

, ,
,
.
, i s_date ( ) ,
Da te : : check ( ) ,
Date. ,
, Date ,
, "30 2008 " , "30
2008 " - . , ,
(, "30 1 066 "),
( ) Date.

9 .8 . Date
, ,
Date. ,
. . . , (,
).
Chrono . h.
1 1 Chrono . h

namepace Chrono
enum clas Month
j an=l , f , mar , apr , may , jun ,
jul , aug , sep , oct, nov , dec
};

class Date {
pulic :
class Invalid { } ; //
Date (int , Month m, int d) ; //
11
Date ( ) ; 11
//

1 1 :
int day ( ) const { return d; }
Month month ( ) const { return m ;
384 9 :

int year ( ) const ( return ;

// :
void add day ( int n) ;
_
void add_month ( int n) ;
void add_year ( int n) ;
private :
int ;
onth m ;
int d ;
};

bool is_date ( int , Honth m, int d) ; // true


bool leapyear ( int ) ; // t r ue
bool operator- -= ( const Date& , const Date& ;
bool operator ! = ( cons t Date& , const Date& ) ;
ostream& operator<< ( os tream& os , const Date& d) ;
istream& operator>> ( istream& i s , Date& dd) ;
} // Ch rono

Chrono . .
11 Chrono . cpp
ftinclude " Chrono . h"
namespace Chrono (
// - :
Date : : Date ( int , Honth mm, int dd)
: ( } , m{mm} , d ( dd }

i f ( ! i s_date (yy , mm , dd) ) throw Invalid( } ;

const Date& default_date ( )

static Date dd ( 2001 , Honth : : j an , 1 } ; // XXI


return dd;

Date : : Date ( )
: y ( default_date ( ) . year ( ) } ,
m( defaul t_date ( ) . month ( ) } ,
d { default_date ( ) . day ( ) }

void Date : : add_day ( int n)

11 . . .

void Date : : add_month ( int n)


(
9.8. Date 385

11 . . .

void Date : : add_year ( int n)


(
if (==onth : : fe && d2 9 &&
! leapyear (y+n) ) ( //
m = n th : : mar ; // 2 9 1
d = 1;

y+=n ;

11 :
bool is_date ( int , onth m, int d)
(
11
if ( d<=O) return fals e ; // d
if (m<onth : : j an 1 1 onth : : dec<m) return fal se ;

int days_in_month 31 ; // 31
switch (m) {
case Month : : fe : //
days_in_month = ( leapyear (y) ) ?2 9 : 28 ;
break ;
case Month : : apr : case onth : : jun :
case onth : : sep : case onth : : nov :
days_in_month = 30 ; 11 30
break ;

if (da.ys_in_month<d) return false ;


return true ;

bool leapyear ( int )

// . . 10

bool operator== ( const Date& , const Date&


{
return . year ( ) b . year ( )
&& . mon th ( ) b . month ( )
& & a . day ( ) . da.y ( ) ;
--

bool operator ! = ( const Date& , const Date&


{
return ! ( =) ;
386 9 :

ostream& operator<< (ostream& os , const Date& d)

return os << ' ( ' << d. year ( )


<< , ' << int ( d . month ( ) )

<< ' , ' << d . day ( ) << ' ) ' ;

istream& operator>> ( istrea.m& i s , Date& dd)

int , m, d ;
char chl , ch2 , h , ch4 ;
is >> chl >> >> ch2 >> m >> h >> d >> ch4 ;
if ( ! i s ) return is ;
if ( chl ! = ' ( ' 1 1 ch2 ! = ' , ' 1 1
h ! = ' , ' 1 1 ch4 ! = ' ) ' ) { / /
is . clear ( ios_base : : failit) ; //
return is ;

dd = Date (y , Month (m) , d) ; 11 dd


return is ;

enum class Day {


sunday , monday , tuesday , wednesday ,
thursday , friday , saturday
};

Day day_of_week ( const Date& d)


{
11 . . .

Date next_Sunday ( const Date& d)

11 . . .

Date next_weekday ( const Date& d)

11 . . .

11 Ch rono

, >> << Date,


1 0.8 1 0.9.
9 Date
. . 387



Da te,
. Date
today, 1 9 1 963 .
Date tomorrow ,
today
add_day ( ) . today tomorrow,
<<. 9.8.
.
. ,
[ l , l 2J, -
[ 1 .3 1 J .
, (2009. 1 3 ,-5).
1 . 9.4. 1 .
2. 9.4.2.
3. 9.4.3.
4. 9 . 7 . 1 .
5. 9.7.4.


1 . ?
2. ?
3. , Date,
?
4. Date . init_
day ( ) ?
5. ? .
6. ,
? ?
7. ?
. (
).
8. ?
9. , -
const?
1 .
?
388 9 :

11
class
const
enwn
struct


1 . .
9. 1 (, ).
2. Name_pairs, (.
), - string, -
doue.
vector<string> ( name) vector<doue> ( age).
read names ( ) ,
_

. read_ages ( ) ,
.
print ( ) , (name [ i ] ,age [ i J ) (
) . name.
sort ( ) name

age.
"" -. (
. ).
. Name_pair : : print ( ) ()
ra tor<< ! = Name_pair.
=

4. "" 8.4.
.
, ;
" ".
5.
Book.
. Book
ISBN. ,
. ,
, . .
. , ,
. .
Book; . ISBN
9 .8. Date 389

n-n-n-x, n - ; - . ISBN
string.
6. Book. == ,
ISBN . ! =
ISBN. << ,
ISBN .
7. Book Genre.
. . .
.
Genre
Book -.
8. Patron .
, .
. ,
. , .
,
(bool ) , , .
9. Library. k
tron. Transaction
Book, Patron Date .
Transaction. .
.
( ) . .
.
, .
, . ,
.
, . ,
Transaction
Transaction. . ,
, .
1 0. leapyear ( ) 9.8.
1 1 .
Date, . next workday ( ) (
. . ,
) week_of_year ( ) ( .
1 . - ) .
1 2 . Date ,
1 1 970 ( ),
long int.
9 . 8 . .
390 9 :


(, ,
. .. ).
1 3.
Rational. :
, 5/6 ( , . 83333).
. , , .
. ,
doue. Rational?
1 4 . Money ,
,
4/5 (0, 5 : , 0,5,
).
long,
, $ 1 23.45. ,
long int.
1 5. n, (
). ,
long int.
. , n*n
, USDl . 23+D5 . 00 ,
.
(USD) (D).
1 6. >>.
(. USDl . 23 DS . 00)
Money. <<.
1 7. , Rational
. Money.
1 8 . , Rational
, doue.


. .
. , .
++
.
. (
) - ,
. :
. ,
.
L\ aC\ b
\\

\!\

----=-=
392 10

./1- -
4.#- 4.4.- 1

t ' J.,,
- ?. ttr ( ?. )


++ :
-. ,
, ,
-
.
:
, ,
.
.
.
.
394 1 D

1 0. 1 . 1 0.8. ,
10.2. -
10.3. 10. 9. ,
1 0.4.
10.5. 10. 10.
10.6. - 10. 1 1 .

1 0.7.
10.11 . 1 .
10.7. 1 .
10.11 .2.

10.7.2.
10.11 ..

1 0. 1 .
.

. 4. 1 -
.
, ,

.
,
, (
),
,
, .
,
, ,

, . , .

- ,
-.

.

, -
-.
.

(),
1 D.1 . 395

-.
-
. , ,
.
1 . -
.
2. .

-
. ,
-, ,
++.
,
.
.
() ( , ,
,
).
.

( , ..).
,
- ,
. ,
-, ,

. ,

396 1 D

.
: -
-
.
1 iostream
.

. -
1 2- 1 6.

1 0.2. -
++
istream ostream - .
istream cin
ostream cout,
(
iostream).
ostream .

.
" -" (. , ,
).
ostream .

" - "
( 1 2 ,34) ostream

1 23

- , os tream

, . ostream
,
. -
1 D.2. - 397

,
.
istream .

.
"-" (. , ,
).
istream .

" - "
( 1 2,34) istream

1 23

ostream.
istream .
. istream,
, , , .
<Enter> ( / ).
. . <Enter>.
<Backspace>.
-
. . .
. -, , ,
. . ..
ostream
.
.
. istream
. ostream. ,
. 1 1 .2, ,
, - 1 1 ..2. .
, .
. ,
- .
398 1

1 0. .
.
.
-
.
-
.
, .
: 1: 2:

: , .
. , ,
.
. ,

( 1 1 ..2).
. .
, ,
. ( 1 1 .2 1 1 .).
ostream .
,
. istream : . -
.

iostream
( ) ( )

,
. , ,
, , .
. , .
(..
). -, ( -).

.
.
,
1 0.4. 399

1 . ;
2. ( );
3. ;
4. ( ) .
,
1 . ;
2. ( ) ;
3. ;
4. ( ).
,
ostream, ,
, cout, istream, ,
, cin. , ,
( 1 1 .3.3), ,
. ,
ostream istream.

1 0.4.
,
. ifstream -

istream , ofstream ostream


-

, fstream iostream,
, .
,
:
cout << " , 'l' : " ;
strinq iname ;
cin >> iname ;
ifstream i s t { iname } ; // i s t
- i n a me
if ( ! ist) error ( " name) ;
" ,

ifstream , name,
. ! ist ,
.
. istream. ,
, >> Point,
:
vector<Point> points ;
for ( Point ; ist>>p ; )
points . push_back (p) ;
400 1

, ofstream.
.
cout << "'l' : " ;
strinq oname ;
cin >> n ;
ofstream ost { n } ; // os t
-

if ( ! ost) rrr ( " O'l'JCP'l' 11w " , n) ;

ofstream , name,
. ! ost ,
.
, ostream. :
for ( int : points )
ost << ' ( ' << . << ' , ' << . << 11 ) \n" ;

.
. .
; . .
,
. - .
,
. ,
.

ostream i stream,
. :
void fill_from_file (vector<Point>& points , strinq& name)
{
ifstream ist { name } ; 11
if ( ! ist) error ( 11 11 O'l'JCP'l' 11 " , name) ;
11 ... i s t . . .
//

open ( ) close ( ) ( . 7. 1 )
.
. -
, . . :
ifstream ifs ;
11 . . .
ifs >> foo ; 11 :
11 . . .
ifs . open (name , // . . .
ios_base : : in) ; // . . .
11 . . .
ifs . close ( ) ; //
1 0.5. 401

11 " .
ifs >> bar ; 11 :
11 . . .

, ,
. , ,
, :
fstream fs ;
fs . open ( " foo " , ios_base : : in) ; 11
11 close ()
fs . open ( " foo " , ios_base : : out) ; 11 : i fs
if ( ! fs) error ( " :110 " ) ;

.
open ( ) close ( ) ?
,
. ,
. ,
,
, ,
iostream ( ++).
1 1 , ,
,
, .
, , ,

.
,
, .

1 0. 5 .
,
. ,
, .
60 . 7
1 60 . 6
2 60 .
59 . 22

(, ).
23,
. : ,
(, ,
402 1 D

), , (
, ) .
.
Reading.
s truct Readinq { //
int hour ; // [ 0 , 23}
doue teperature ; //
};

:
vector<Readinq> temps ; //
int hour ;
dou.le temperature ;
while ( is t >> hour >> temperature) {
if (hour < 1 1 23 < hour) rrr ( " " ) ;
teps . push_back (Readinq{ hour , temperature } ) ;

. istream ist
( ifstream) , ,
(cin) istream. ,
, , istream
. , , - ,
ist istream
. :

.
, .
, ,
. ,
(ofstream)
ostream. , , ,
.
for ( int i=O ; i < teps . si ze ( ) ; ++i )
ost << ' ( ' << temps [ i ] . hour << ' , '
<< teps [ i ] . temperature << " ) \n" ;

,
(, ).

,
.
#include " s td lib facilities . h"

s truct Readinq { 11
1 0.6. - 403

int hour ; // [ 0 , 23]


doue temperature ; //
};

int main ( )
{
cout << " : " ;
strinq iname ;
cin >> iname ;
ifstrea ist { iname } ; // i s t i name
if ( ! ist) rrr ( " w " , iname) ;

strinq n ;
cout << " : " ;
cin >> n ;
ofstream ost { n } ; // os t
if ( ! ost) rrr ( " w .n " , n) ;

vector<Readinq> temps ; //
int hour ;
doue temperature ;
while ( ist >> hour >> temperature) {
if (hour < 1 1 23 <hour) rrr ( " " ) ;
temps . push back ( Readinq {hour , temperature ) ) ;
_

for ( int i=O ; i<temps . si ze ( ) ; ++i )


ost << ' ( ' << temps [ i ] . hour << ' , '
<< temps [ i ] . temperature << " ) \n" ;

1 0.6 . -
, .

? ? -
, ( ,
, ..), - ,
, - ,
, ..
!
i s tream ,
(stream state).


good
eof ( ) ( )
fail ( ) ( , , )
bad ( ) ( , )
404 1

, fail ( ) bad ( )
,
- .
:
, fail ( ) . ,
( ) .
, .
bad ( ) , ,
,
. bad ( )
fail ( ) :
.

int i = ;
cin >> i ;
if ( ! cin) { // ()
if ( cin . bad ( ) ) error ( "cin " ) ; // !
if ( cin . eof ( ) ) {
11
11

if ( cin . fail ( ) ) { //
cin . clear ( ) ; //
11

! cin " cin "


" cin - ", " cin
good ( ) ".
" ". cin .
clear ( ) fail ( )
.

fai l ( ) , , , .
fail ( ) ,
, ;
clear ( ) , cin . clear ( ) cin
good ( ) .

.
, ,
* (<Ctrl+Z>
Windows <Ctrl+D> Unix) :
1 2 4 5 *

:
void fill_vector ( is tream& ist, vector<int>& v , char terminator)
// i s t v
//
1 D . 6. - 405

for ( int i ; ist >> i ; ) v . push_back ( i ) ;


if ( ist . eof ( ) ) return ; // :

if ( ist . bad ( ) ) error ( " ist " ) ; 11 !


if ( i st . fail ( ) ) { //
ist . clear ( ) ; // ,
11
char ;
i s t >> ; // ( -
// )
if ( ! = terminator) { //
i s t . unqet ( ) ; //
i s t . clear ( ios_base : : failbit) ; //
// fa i l ()

, ,
, . ,
- , , fill_vector ( ) ,
fail ( ) .
, , ,
,
fail ( ) . i s t . clear (ios_base : : failit) .

clear ( ) : clear ( )
() iostream,
() . fail ( ) . ,
, .
ist, unqet ( ) ; ,
fil l_vector ( ) ,
. unqet ( )
putack ( ) ( 6.8.2, . 7 . 3),
. , ,
.
fill_vector ( ) ,
, fail ( ) eof ( ) .
, runtime_error,
error ( ) , , -
istream d ( ) .
.
, ,
bad ( ) , - .
, istream
.
406 1

11 i s t ,
// ba d
ist . exceptions ( i s t . exceptions ( ) ( ios_base : : badit) ;

, :
ist bad ( ) ,
ios_base : : failure. exceptions ( )
. .
bad ( ) :
void fill_vector ( is tream& ist , vector<int>& v, char terminator)
11 i s t v
11

for ( int i ; ist >> i ; ) v . push back ( i ) ;


_
if ( is t . eof ( ) ) return ; // :

11 good () , ba d () , eof () ; , fa i l (}
i s t . clear ( ) ; // ,
11
char ;
ist >> ; // ( -

11 )
if ( ! = terminator) { //
ist . unget ( ) ; //
i s t . clear ( ios_base : : failbit) ; 11
11 fa i l ()

ios base, ,
iostream, , badit, ,
failure, .
: : , ios_base : : badit ( .7.2) .
iostream:
. , iostream
,
,
(
.
). , iostream,
h ++ Programmig Language (Stroustrup) Stan
dard ++ IOStreams and Loca/es (Langer).
ostream ,
istream: good ( ) , fail ( ) . eof ( ) bad ( ) . .
,
, . .
, r,
,
1 .7. 407

, ,
.

1 . 7 .
. , ,
.
,
, .
.
.
-
.
, ,
. ,
,
, .
1 1 0 ().
cout << " 1 10 : \n " ;
int n = ;
while ( cin>>n) { 11
if ( l<=n && n<=lO) break ; 11
cout << " , " << n
<< " 'l' [ 1 , 10 ] ; \n" ;
}
11 . . .

, .
break ( .6),
.
cout << "'l' 1 10 : \n" ;
int n = ;
while ( cin>>n && ! ( l<=n&&n<=lO ) ) //
cout << " , << n "

<< " [ 1 , 1 0 ] ; \n" ;


11 . . .

, "" .
, ""?
, ,
. t
6 ( t
6), ,
n, .
. ( -
408 1

) (
<Ctrl+Z> Windows <Ctrl+D>
Unix). n,
. ,
, .
1 . , ,
?
2. , (
)?
3. , (
- , )?
?
?"
.
1 . .
2. , -
(, ).
3. .

, - ,
.
, . ..
, ,
. ,
, , ,
. ,
.
.
.
. , ,
"" ; ""
. ,
.
,
; ,
. ,
,
, :
, .
cin ( cin . clear ( ) ),
1 0. 7 . 409

(
<Ctrl+Z>?) .
, , ,
,
, -
; . . ,
. ,
.
( ). .

1 0 . 7 . 1 .
,
.
.
cout << " 1 1 0 : \n " ;
int n = ;

while ( true )

cin >> n ;
if ( cin) ( // ;
if (1 <= n && n <= 1 0 ) break ;
cout << "'1' , " << n
<< " [ 1 , 10 ] ; \n" ;

else if (cin . fail ( ) ) ( // ,


//
cin . clear ( ) ; // good ( ) ;
/ /
cout << " ; \n " ;
for ( char ch ; cin>>ch && ! isdigit ( ch) ; ) //
/ * * / ; //
if ( ! cin) error ( "Boa " ) ; 11
cin . unget ( ) ; / / ,
11
else (
error ( "Boa " ) ; // eof bad:
11
)
11 , ,
11 [ 1 , 1 0 ]

.
,
. ,
,
410 1

. ? .
.
.
.
.
"" .
.
,
. ,
. -
"" (.. ) .
void skip_to_int ( )
{
if ( cin . fail ( ) ) // ,
//
cin . clear ( ) ; // good () ;
for ( char ch ; cin>>ch ; ) { //
if ( i sdiqit ( ch) 1 1 ch == ' - ' ) {
cin . unqet ( ) ; 11 ,
11
return ;

error ( " " ) ; // eof bad

skip_to_int ( ) .
:
cout << " 1 10 : \n" ;
int n = ;
while ( true) {
if ( cin>>n) { // ;
if ( l<=n && n<=lO ) break ;
cout << " , " << n
<< " .w [ 1 , 10 ] ; \n" ;

else
cout << "'l' , ; \n" ;
skip_to_int ( ) ;

}
// , ,
11 [ 1 , 1 0 }
1 0.7. 411

,
, .
?
: " :
in t, - ".
int qet_int ( ) ; 11 cin i n t

int qet_int ( int low , int hiqh) ; 11 c i n i n t


11 [ l o w , h i gh }

,
. .
int qet_int ( )
(
int n = ;
while ( true )
if ( cin >> n) return n ;
cout << " ; \n" ;
skip_to_int ( ) ;

get_int ( ) ,
, .
get int ( ) ,
( get_i nt ( )
) .
get int ( ) ,
get_int ( ) :
int qet_int ( int low , int hiqh)

cout << " "


<< low << " " << hiqh << " JU : \n" ;
while ( true) (
int n = qet_int ( ) ;
if (low<=n && n<=hiqh) return n ;
cout << " , " << n
<< " . [ "<< low
<< '<< hiqh << " ] ; \n " ;
'

get_int ( ) ,
. cin ,
, .

.
41 2 1

int n = qet_int ( l , 10 ) ;
cout << " n : " << n << endl ;

int m = qet_int ( 2 , 0 0 ) ;
cout << " m : " << m << endl ;

.
(, ) .
get_int ( )
.

1 . 7 .2.
get_int ( ) -
. .
, .
. get_int ( ) .
:
int strenqth = qet_int ( l , 10 , "Bee " ,
" , " ) ;
cout << " : " << strenqth << ' \n ' ;

int altitude = qet_int ( 0 , 50000 ,


" " ,
" , " ) ;
cout << " : " << altitude << " \n" ;

:
int qet_int ( int low, int hiqh ,
const strinq& qreetinq ,
const strinq& sorry)

1 . 1
cout << qreetinq << " : [ " << low << << hiqh << " ] \n" ;
while ( true) (
int n = qet_int ( ) ;
if ( low<=n && n<=hiqh) return n ;
1 . 1
cout << sorry << " : [ " << low << << hiqh << " ] \n" ;

,
.
,
(, . ,
. , ).
.
, :
get in t ( ) "".
1 . . . 41 3

,
, ,
"" . , .
,
, -
. , .
,
. ,
error ( ) (.
5. 6.3); , .

1 . . ,
<< , ,
.
,
.
- .
, ,
<< .

.
<<.
. ,
,
<<.
,
.
Date 9.8,
. , .
ostream& operator<< (ostream& os , const Date& d)
{
return os << ' ( ' << d . year ( )
<< ' , ' << int ( d . onth ( ) )
<< ' , ' << d . day ( ) << ' ) ' ;

. "30 2004 "


(2004 , 8 , 30 ) .
, ,

.
41 4 1

9.6 , ,
,
. . -
<< Date,
cout << dl ;

dl Da te,
operator<< ( cout , dl ) ;

, ostream&
operator<< ( ) .
"" . ,
.
cout << dl << d2 ;

<<.
.
cout << dl << d2 ; // opera tor<< (cou t , dl ) << d2 ;
11 opera tor<< (ope ra t o r<< (co u t , dl ) , d2) ;

, dl cout,
d2 ,
.
dl d2 .
.

1 . 9 . ,
>>
. ,
.
Date 9.8,
, <<.
.
i stream& operator>> ( i s tream& i s , Date& dd)
{
int , m, d ;
char chl , ch2 , h , ch4 ;
is >> chl >> >> ch2 >> m >> h >> d >> ch4 ;
if ( ! is ) return i s ;
if ( chl ! = ' ( ' 1 1 ch2 ! = ' , ' 1 1 h ! = ' , ' 1 1 ch4 ! = ' ) ' )
/ /
i s . clear ( ios_base : : failbit) ;
return i s ;
1 0.1 . 41 5

dd = Date { y , Date : : Month (m) , d } ; // dd


return is ;

>> , (2004 , 8 , 20 ) ,
Date . ,
, .
, .
>> ,
( , , ) , fail ,
eof bad, Date .
istream - clear ( ) .
, ios base : : failbi t fail ( ) .
_

Date
; . ,
operator>> () ,
, :
,
. (2004 ,
8 , 30 } . , } ,
,
, unget ( )
. operator>> ( )
Date, ( 2 0 0 4 , 8 , 32 ) , Date
,
operator>> ( ) .

1 0 . 1 .
1 0 . 5 , .
(. 1 0. 6)
, .
, .

. , ,
ist istream.
for (y_type var ; ist>>var ; ) { //
// r va r

11 - va r
}
11 ba d ;
11 .
i f ( i s t . bad ( ) ) error ( " noxo O'l'OIC " ) ;
41 6 1 D

if ( is t . fail ( ) )
// ?
}
11 ,

, ,
, ,
, , .
1 0.6, , istream
JUi failure .
.
// - : i s t
i s t . exceptions ( i s t . exceptions ( ) l ios_base : : badit) ;

-
.
for (y_type var ; i st>>var ; ) { //
11 va r

// - va r

if ( i st . fail ( ) ) 11 ' 1 ' - /


ist . clear ( ) ;
char ch ;
if ( ! ( i s t >> ch && ch == ' 1 ' ) )
rrr ( ": " ) ;

}
// ,


- , . . ,
error ( ) .
,
,
, , JUi
, .. .
.
, . ,

.
. .
11 - : i s t
ist . exceptions ( i s t . exceptions ( ) l ios_base : : badit) ;

void end_of_loop ( i s tream& i s t , char term, const string& essage)


1 D.1 1 . r 41 7

if ( i s t . fail ( ) ) { // term
11 /
ist . clear ( ) ;
char ch ;
if ( ist >> ch && ch -- term) return ; //
error (essage ) ;


for (y_type var ; ist>>var ; ) { //
// va r

// - va r

// ,
end of loop ( i s t , ' 1 ' , " " ) ;
// :

end of loop ( ) , ,
; fail ( ) . , r

.

1 0. 1 1 .
" " .
,
. ,
,
.
, .
{ year,
, , 1 900,
} .
, .
{ month,
, j an,
} .
.
( ,
, , ) .
41 8 1

.
year 1990 1
{year 1991 { onth jun 1 1
{year 1992 { onth j an ( 1 61 . 5 ) 1 ( onth f ( 1 1 6 4 ) ( 2 2 65 . 2 1 1 1
{year 2000
{ month f (1 1 68 1 (2 3 66 . 6 6 ) ( 1 67 . 2 ) 1
{ onth dec ( 15 15 - 9 . 2 ) ( 1 5 14 -8 . 8 ) ( 14 - 2 ) 1

.
.


(, HTML L), -
, .
, , .
,
.
, , ,
,
.
,
. ,
, (
HTML L) . ,
. , .
, -
,
, . , .

1 0.1 1 .1 .
? .
, Year, onth Reading,
. Year Month
;
, ,
, ,
.. ,
Year onth
: Month , Year -
. Reading? ,
().
Reading ( , )
1 0. 1 1 . 41 9

Month. , : ,
. ,
- ,
.

.
- , .
- ,
.
Year ,
1 2 Month, Month ,
-

30 Day, Day 24 (
-

).
. . Day, Month Year -

. .
Month Day
Year , ,
. , " "
, .
const int not_a_readinq = -7777 ; //

,
, "
". .
const int not_a_month = -1 ;

:
struct Day {
vector<doule> hour { vector<doule> ( 2 4 , not_a_readinq) } ;
};

. Day 24 ,
not_a_reading.
struct Month { !/
int month { not_a_month } ; // { 0 , 1 1 } ( 0)
vector<Day> day { 32 } ; // { 1 , 3 1 }
};

da ta [ ] .
,

struct Year { /!
int year ; //
vector<onth> month { 12 } ; // { 0 , 1 1 } ( 0)
};
420 1

, - , Month Year
month year .
" " (
, 2 4 , 32 12). ,
. -
( )
. ,
, , "
";
(. 7.6. 1 ) . 32
, , ; 32
"" .
,
struct Day {
vector<doule> hour { 24 , not_a_reading } ;
};

, , - ! -
(24 -1).
, int
,
( ) ( 1 8.2) .

1 0. 1 1 .2.
Reading ,
:
struct Readinq
int day ;
int hour ;
doule teperature ;
};

istream& operator>> ( istream& i s , Reading& r )


1 1 i s r
// : ( 3 4 9. 7
11 ,

char chl ;
if ( i s >> chl && chl ! = ' ( ) { // Rea di n g ?
'

is . unqet ( ) ;
i s . clear ( ios_base : : failbit) ;
return i s ;

char ch2 ;
1 0. 1 1 . 421

int d;
int h ;
doule t ;
is >> d >> h >> t >> ch2 ;
if ( ! is 1 1 ch2 ! = ) )
error ( "noxa " ) ; 11
r . day = d ;
r . hour = h ;
r . teperature t;
return is ;

, . ,
fail ( ) .
:- . ,

, error ( ) .
: Month , ,
: : Reading,
(: >> : Reading).
istrellill& operator>> ( istrellill& is , Month& )
11 Mon th is

11 : { mon th feb . . . }

char ch = ;
if ( i s ch && ch ! = ' { ' ) {
i s . unget ( ) ;
is . clear ( ios_base : : failbit) ; 11 Mon th
return i s ;

string onth_arker ;
strinc;r m;
is >> onth arker >> m;
if ( ! is 1 1 onth_arker ! = "onth " )
error ( " Heaepoe onth" ) ;
. onth = onth_to_int (m) ;

Readinc;r r ;
int duplicates = ;
int invalids = ;
for (Readinc;r r ; is >> r ; ) (
if ( i s_valid ( r ) ) {
if (. day [r . day] . hour [ r . hour] != not_a_reading)
++duplicates ;
. day [r . day] . hour [ r . hour] = r . teperature ;

else
++invalids ;
422 1

if ( invalids)
error ( " Heepre onth , " , invalids ) ;
if (duplicate s )
rrr ( " 111 1D1 Month , "
duplicate s ) ;
end_of_loop ( is , ' } ' , " Month " ) ;
return is ;

month_to_int ( ) :
, j un,
[ , 1 1 ] . end_of_loop ( )
1 0. 1 .
Readinqs
( - ).
>> Month
Readinq, .
constexpr int iplausie_min -200 ;
constexpr int iplausie_ax = 200 ;

l i s_valid ( const Readinq& r )


1 1
{
if ( r . dsy <1 1 1 l<r . day) return fal se ;
if ( r . hour<O 1 1 23<r . hour) return false ;
if ( r . teperature < iplausie_min 1 1
iplausie_max < r . teperature)
return false ;
return true ;

, Year. >>
Year >> Month.
istream& operator>> ( istream& i s , Year& )
11 Yea r is
// : ( r 1 9 72 . . . J

char ch ;
is >> ch ;
if ( ch ! = ' { ' )
is . unqet ( ) ;
is . clear ( ios : : failbit) ;
return i s ;

strinq year_marker ;
int ;
i s >> year_marker >> ;
i f ( ! is 1 1 year_marker ! ="year " )
1 0.1 1 . 423

rrr ( " Year" ) ;


y . year = ;
while ( true) {
Month m; //

if ( ! ( i s >> m) ) break ;
y . month [ m . month] m;
=

end_of_loop ( is , ' } ' , " Year" ) ;


return is ;

"'' , "
", . .
, -
for (Month m; is >> m;
y . month [m. month] = m;

, .
. ,
. , operator>> ( i s tream& i s ,
Month& m) m ;
Reading. ,
is >> m
m. , Month
.
is >> m,
. Month.
m ,
.
, operator>> ( i s tream& is , Month& m)
m .
for (Month m; i s >> m; ) {
y . month [m. month] m; =

m = Month { } ; / / " "

.
11
cout << "'l' .n\n" ;
string iname ;
cin >> iname ;
ifstream ist { iname } ;
if ( ! ifs) rrr ( " l'l' .n " , iname) ;

// bad ()
ifs . exceptions ( ifs . exceptions ( ) l ios_base : : badit) ;
424 1

11
cout << " w:r \n " ;
s trinq n. ;
cin >> n. ;
ofstream ost { ona.me } ;
if ( ! ofs) rrr ( " 'l' w 11 , n.) ;

// :
vector<Year> ys ;
while ( true ) {
Year ; //
// Yea r
if ( ! ( ifs>>y) ) break ;
ys . push_back (y) ;

cout << " " << ys . size ( ) << " I \n" ;

for (Year& : ys ) print_year (ofs , y) ;

print_year ( )
.

1 0.1 1 ..
>> Month .
.

.
if, .:
i f ( s = " j an" )
1D = 1 ;

else if ( s== " fe " )


ID = 2 ;

;
. ,
,
.
vector<string>.
.
vector<strinq> month_input_t = {
" jan" , 11 f 11 , "mar" , " apr " , " . " , " jun" ,
" jul " , "auq" , " sep " , " oct." , "nov" , "dec"
};

int onth_to_int ( s trinq s )


// s ? ,
1 0.1 1 . 425

11 ( 0 , 1 1 } , - -1
{
for ( int i=O ; i<12 ; ++i ) if (onth_input_tl [ i ] ==s) return i ;
return -1 ;

. ++
- map<string , i nt>
2 1 .6. 1 .
, .
int.
. ,
string int
int string.
vector<strinq> onth_print_tl = {
"January" , " Feruary" , "&rch" , "April " ,
"& " , "June" , " July" , "Auqust" , " Septeer" ,
"October" , "Novemer" , "Decemer"
};

strinq int_to_month ( int i)


11 ( 0 , 1 1 }
{
if ( i<O 1 1 12<=i) rrr ( " " ) ;
return onth_print_tl [ i ] ;

,
? , ?
,
- . - , - .
. , ,
. - .
- .
(
) .
. .
. -
.
426 1

1 . . (. 1 0.4).
Point, -
.
2. 1 0.4,
(, ).
Point original__points .
3. original__points, ,
.
4. ofstream mydata . txt.
Windows . txt
(,
WordPad).
5. ofstream, ifstream
mydata . txt. myda ta . txt
processed__points.
6. .
7. -
! , ( )
.


1 .
?
2. istream?
3. ostream?
4. ?
5. ?
6.
.
7. .
8. .
9. .
1 0.
.
1 0. 1 1 . r 427

,
.
( ).
.
1 1 . ?
1 2 . ?
1 3. () ?
1 4. ,
clear ( ) istream.
1 5. << >>
?

d ( ) iostream
uffer istream
clear () ofstream
close ( ) open ( )
eof ( ) ostream
fail ( ) unget ( )
good ( )
ifstream

1 . , ,
.
2. , ,
Reading, 1 0. 5 .
50 .
store_temps . cpp. - raw_
temps . txt.
3. , raw temps . txt,
. 2. ,
. temp_stats . .
4. s tore_temps . cpp . 2,
f .
temp_stats . cpp,

.
5. print_year ( ) , 1 0. 1 1 .2.
428 1

6. Roman int (
int) << >>. Roman_int
as_int ( ) , int, . ,
r Roman int, cout << " " << r
<< 11 11 << r . as_int ( ) << ' \n ' ; .
7. 7,
, , XXI + CIV cxxv. =

8. ,
,
; ,
.
9. , ,
, ,
, .
1 0 . 7 from .
. to .
( , )
. , 7 .3,
. ,
.
1 1 . . ,
.
, "bears : 17 elephants 9 end" , 26.


,
-.
.
- ,
()
.
. , (
) , - , ,
, ,
.

"" .

' ,
' ?- #. -

,f 9 (Uk/ E)
-

,
-, 1 0 ,
.
, ,
,
.
.

.
430 1 1

11 . 1 . 11 ..1 .
11 .2. 11 ..2.
1 1 .2.1 . 11 ...
11 .2.2. 1 1 .4.
11 .2.3. 11 .5. .t
11 .6.
11 .2.4. 1 1 .7.
11 .2.5.
1 1 .. 11 .8.

1 1 . 1 .
iostream ( ++,
-)
. "" ,
.
, ,
1234 .
: 1 , 2, 4 .

. . ,
(, ) ,
. , ,
,
. .
, ,
( ).
,
.
.
,
-
, ,
, ,
. ,
, .
,
.
1 1 .2. 431

1 1 . 2.
.
,
. , 1 . 25 (
) 1 . 24 6 7 0 4 7 7 ,
( 1 2 5)
.

( 1 . 2467) 1 . 25 (
, . . ) .


. (ostream)
.
<<.
,
, . . .
.
( , );
-; ""
true ( vrai or sandt) , 1,
,
ASCII (, Unicode) . ,
,
. ,
,
, Standard ++ IOStreams d Locales
(Langer); 2 1 h ++ Programmtng Language
; 22 27 ISO ++.

.

1 1 .2 . 1 .
(
8). (
1 0) ( 1 6).
, .2. 1 . 1 .

.
, .
,
.
,
432 11

,
( ),
(
). -
++ ( 1 970- ),
. .
, ( ) 1234
, :
cout << 1234 << 11 \t (decial ) \n "
< < hex < < 1 2 3 4 < < 11 \t ( hexadecial ) \n"
<< oct << 1234 << 11 \ t ( octal ) \n" ;

' \ t ' " ".


:
1234 (decial )
4d2 (hexadecial )
2 322 ( octal)

<< hex << oct .


<< hex ,
, << oct
,
. .
cout << 1234 << ' \t ' << hex 1234
<< ' \t ' << oct 1234 ' \n ' ;
cout << 1234 ' \n ' ; 11
//

:
1234 4d2 2 322
2322 //

,
: , oct, hex dec ( )
, " " (sticky). -
, . hex oct

(manipulators).


,
. .

.
1 1 .2. 433

, ,
. , ,
, 1 1
1 1 , 9 (.. 1 1 )
1 7 (. . 11 ).
,
, .
.
cout << 1234 << ' \t ' << hex << 1234
<< ' \ t ' << oct << 1234 << ' \n ' ;
cout << showbase << dec ; //
cout << 1234 << ' \t ' << hex << 1234
<< ' \ t ' << oct << 1234 << ' \n ' ;

:
1234 4d2 2 322
1234 Ox4d2 02322

. .
, ( ).
++.
.
cout << 1234 << ' \t ' << Ox4d2 << ' \t ' << 02322 << ' \n ' ;

:
1234 1234 1234

, showbase
, oct hex. noshowbase
showbase,
,
.
, .


oct
dec
hex
showbase
noshowbase
434 1 1

1 1 .2.2.
>> ,
,
.
int ;
int ;
int ;
int d ;
cin >> >> hex >> >> oct >> >> d ;
cout << << ' \t ' << << ' \ t ' << << ' \ t ' << d << ' \n ' ;


1234 4d2 2 322 2322


1234 1234 1234 1234

, oct, dec hex


, .


, ,
. :

1234 1234 1234 1234
. . ,
.


, >>.
, ,
cin . unsetf ( ios : : dec) ; 11 ( . .
11 )
cin . unsetf (ios : : oct) ; 11
11 (. . 1 2 )
cin . unsetf ( ios : : hex) ; 11
11 (. . 1 2 )

- unsetf ( ) ( ),
. ,
cin >> >> >> >> d ;


1234 Ox4d2 02322 02322
1 1 .2. 435


1234 1234 1234 1234

1 1 .2..
,
(. , ).
, ,
.
ios tream , . .
cout << 1234 . 56789 << " \ t\t (o ID) \"
<< fixed << 1234 . 56 7 8 9 << 11 \ t (11) \ 1 1
<< scientific << 1234 . 5 6 7 8 9 << " \ t ( ) \n" ;

:
1234 . 57 ( )
1234 . 567890 ( )
1 . 234568+003 ( )

fixed, s cientific defaul tfloat


.
defaultfloat ,
( (general format)).
:
cout 1234 . 56789 ' \t '
fixed << 1234 . 5 6789 << ' \ t '
scientific << 123 4 . 56789 << ' \n ' ;
cout 1234 . 56789 ' \n ' ; // " "
cout defaultfloat << 1234 . 5 6789 11
<< \t ' << fixed << 1234 . 56789
<< ' \ t ' << scientific << 1234 . 5 6 7 8 9 << ' \n ' ;

:
1234 . 57 1234 . 567890 1 . 234568+003
1 . 234568+003 // " "
1234 . 57 1234 . 567890 1 . 234568+003


fixed
scientific ;
(1 , 1 0), . .

defaul tfloat fixed scientific
defaultfloat
436 1 1

1 1 .2.4.

defaultfloat. ,
( defaul tfloat).
, - . .
1234 . 567 1234 . 57
1 . 2345678 1 . 23457
, , 4/5: 4 -
, 5 9 . ,
-

,
1234567 1234567 ( )
1234567 . 1 . 234 57+ 0 0 6
ostream , 1234567 .
fixed, ,
scientific,
. , defaultfloat
scientific fixed,
defaultfloat,
.


,
1234567 . 89, defaul tfloat, - fixed,
- scientific.
?

,
setprecision ( ) , :
cout < < 1234 . 5 6789 < < \t '
<< fixed << 1234 . 5 6789 << ' \ t '
<< scientific << 123 4 . 56789 << ' \n ' ;
cout << defaultfloat << setprecision ( 5 )
<< 1234 . 5 6789 < < ' \t '
<< fixed << 1234 . 5 6789 << 1 \t '
<< scientific << 1234 . 5 6789 << ' \n ' ;
cout << defaultfloat << setprecision ( 8 )
<< 1234 . 5 6789 << ' \t '
<< fixed << 1234 . 56789 << ' \ t '
<< scientific << 1234 . 56789 << ' \n ' ;

(
) :
1 1 .2. 437

1234 . 57 1234 . 5 6 7 8 90 1 . 2 345 68+003


1234 . 6 1234 . 5 6 7 8 9 1 . 23457+003
1234 . 5679 1 23 4 . 5 6 7 8 9000 1 . 2 3456789+3

, .


defaul tfloat
scientific
fixed

, (
defaul tfloat , ),
. ,
, : "
"

1 1 .2.5.

. .
. .
(fields). ,
setw ( ) :
cout << 123456 << ' 1 ' //
<< setw ( 4 ) << 123456 << ' 1 ' // 1 23 4 5 6
11 4
<< setw ( 8 ) << 123456 << ' 1 ' // 8
<< 123456 << " 1 \n" ; 11 " "

:
123456 1 12345 6 1 123456 1 12345 6 1

123456. -
, ,
. 123456 ,
, . ? ,
1 1234 1 1 3456 1 ,
,
, ,
. -
, .
, " ".
(, ) -
, .
438 1 1


:
cout << 12345 << ' 1 ' << setw ( 4 ) << 12345 << ' 1 '
<< setw ( B ) << 12345 << ' 1 ' << 12345 << " l \n" ;
cout << 1234 . 5 << ' 1 ' << setw ( 4 ) << 1234 . 5 << ' 1 '
<< setw ( S ) << 1234 . 5 << ' 1 ' << 1234 . 5 << " l \n " ;
cout << "asdfq" << ' 1 ' << setw ( 4 ) << "asdfq" << ' 1 '
<< setw ( B ) << " asdfq" << ' 1 ' << " asdfq" << " l \n " ;

:
12345 1 12345 1 12345 1 12345 1
1234 . 5 1 1234 . 5 1 1234 . 5 1 1234 . 5 1
asdfq l asdfq l asdfq l asdfq l

,
("").
, ,
. .
, .

&
. , ,
.
,
.

1 1 ..
++ - ,
. 1 0.3, -
, .
: 1: 2:

, .
iostream
. .
,
. , istream,
, ostream,
.
1 1 . . 439

1 1 .. 1 .
.
ifstream , ofstream -
. .
.


ios_s : : ( )
ios base : : ate ( )
ios_s : : inary ( )
ios s : : in
ios s : : out
ios s : : trunc

:
ofstream ofl { namel } ; 11 i o s_ ba se : : o u t
ifstream ifl { name2 } ; // i o s_ ba se : : in
ofstream ofs { name , ios_ase : : app } ; // ofs t ream
// ios base : : o u t
fstream fs{ "myfile " ,
ios_ase : : in 1 ios_ase : : out } ; /! ,

1 - (
. 5 . 5), .
,
.

.
, ,
good ( ) .
if { ! fs} // :


.
ifstream ifs { " redunqs " } ;
if ( ! ifs) // "readings "

, . .
,
. . ( )
,
.
440 1 1

ofstream ofs ( "no-such-file" } ; / /


11 "no - s u ch - fi l e "
ifstream ifs ( "no-file-of-this-name " } ; // : i fs
/ / good ()

"" .
"" .
, istream,
- , ostream.

1 1 ..2.
1 23
. .
int n = 123 ;
strinq s 11 12 3 11 ;
=

123 () .
, , ,
int (4 , .. 32
). 123 12345,
- .
123 .
11 12 34 5 11 ,
( string).
,
, , .

1 23 :
12131 ? ? 1 1? ? ?

1 2345 :
121314 5
1 1? ? ?

1 23 : 1 23

1 2345 : 1 2345

, -
, ,
1 23456 1 23 456.
.
.

1 23456 :
12131415161 111
1 23 456 : l2ll 141sll 1

(, int)
1 1 .. 441

(, s tring)
. ios tream
; . istream
.
ostream
. ,
istream ostream .
- (blnary 1/0).
ios_base : : inary. ,
,
. , ,
.
int ain ( )

// i s t ream
cout << " \n" ;
strinq iname ;
cin >> iname ;
ifstream ifs { inae , ios_base : : binary } ; // ,
//
11 -
if ( ! ifs) rrr { " !'l' " , inae) ;

// os t ream
cout << " \n" ;
strinq n ;
cin >> n ;
ofstream ofs { onae , ios_base : : binary } ; // ,
//
// -
if ( ! ofs) error ( " !'l' " , oname) ;

vector<int> v ;

// :
for (int ; ifs . read (as_ytes {x) , sizeof ( int) ) ; ) 11
v . push_back (x) ; 11

11 ... v

// :
for (int : v)
ofs . write ( as_bytes (x) , sizeof (int) ) ; 11
return ;

ios_base : : inary
:
442 1 1

ifstream ifs { name . c_str ( ) , ios_base : : binary } ;


oftream ofs { name . c_str ( ) , ios_base : : binary } ;

,
. -
-
>> <<.
,
(, " asdf" , s, d, f,
123 1 , 2 , 3).
,
. inary,
( - ) , .
, inary ,
.
int? ,
; .
int ( )
, .
.
ifs . read ( as_ytes ( i ) , sizeof ( int) ) //
ofs . write (as_bytes ( v [ i ] ) , sizeof ( int) ) //

wri te ( ) ostream read ( ) istream


( as bytes ( ) )
(), sizeof.
, ,
. , int
1234, (
) 0 0 , 0 0 , 0 4 , d2 :
-

i: 00 00 04 d2

as_bytes ( ) .
( ,
, 1 7.8 1 9.3):
template<class >
char* _t (& i) //
//

void* addr z &i ; //


//
1 1 .3. 443

return static_cast<char*> ( addr) ; 11

static_cast
,
. 1 7 1 8.
, .
. ,
read ( ) wri te ( ) .
. .
,
-
. ,
.
,
:
.
-.
ios tream, Ul\.1,
-
. .
-, -
, .

1 1 ...

.
. , ,
,
.
. ,
.
, , /
("read/get posltion"), . ,
/ ("write/ put posltion").

: : 6

.
fstrea. fs { n } ; //
if ( ! fs) error ( " " , n) ;
444 1 1

fs . seekq ( S) ; // (g "ge t ")


11 ( )
char ch ;
fs>>ch ; 11
cout << " CllOn - " << ch << ' ( ' << int (ch) << 11 ) \n" ;
fs . seekp ( l) ; /! ( "pu t ")
/!
fs<< ' ' ; / /

, seekg ( ) seekp ( )
.
.
: .
, (
seekg ( ) seekp ( ) ) .
.

1 1 .4.
istrea.m
ostrea.m
string. istrea.m,
string, istringstrea.m, ostream,
s tring, ostringstrea.m. ,
istringstream .
doule str_to_doule ( strinq s)
/! s
//

istrinqstream i s { s } ; // s

doule d ;
i s >> d ;
i f ( ! is) error ( "OlllC\ a doule : 11 , s ) ;
return d ;

doule dl = str_to_doule ( " l2 . 4 " ) ; 11


doule d2 str_to_doule ( " l . 34e-3 " ) ;
doule d = str_to_doule ( " twelve point three " ) ; 11 error ()

,
istringstream, eof ( ) .
, istringstream
; istringstream
istream.
ostringstream, , .
, .
1 1 .4. 445

(
1 6. 5), :
void my_code (strinq lael , Temperature t)
{
11 . . .
ostrinqstream os ; //
os << setw ( B ) << lael << " : "
<< fixed << setprecision ( S ) << t . t << tep . unit ;
someobject . display ( Point ( l00 , 100) , os . str ( ) . c_str ( ) ) ;
11 . . .

- str ( ) ostringstream
string, ostringstream.
c_str ( ) - string,
-

, .
stringstream ,
- . ,
string str_to_doue ( )
(. ) .
, my code ( ) ,
. , 1 1 . 7 stringstream
. ,
stringstream -
.
ostringstream
.
int seno = qet_next_numer ( ) ; //
ostrinqstream name ;
nas << "myfile" << seno << " . loq" ; 11 , myfi l e 1 7 . l og
ofstream loqfile { na.e . str ( ) } ; // myfi l e 1 7 . l o g

istringstream string,
, .
, ostringstream
string, .
stringstream,
: ss . s tr ( )
ss, ss . str ( s )
ss s . 1 1 . 7 ,
ss . str ( s ) .
446 1 1

1 1 . 5 . ,
>>
, .
, int >> .
, ,
string >> ,
(whltespace). istream
:
strinq n. ;
cin >> n. ; 11 : Denn i s Ri t ch i e
cout << n << ' \n ' ; 1 1 : Den n i s

,
, ?
getline ( ) :
strinq name ;
qetline ( cin , name) ; 11 : Denn i s Ri t ch i e
cout << name << ' \n ; 11 : Den n i s Ri t ch i e

. ?
: " ,
>>". : "
". , ,
>>. , ,
- , :
strinq first_name ;
strinq second_name ;
strinqstream ss ( name) ;
ss >> first_name ; 11 Denn i s
ss >> second_name ; 1 1 Ri t ch i e

first_name second_name
.

,
.
. ,

, :
,
.

,
.
1 1 .6. 447

string command ;
getline ( cin , command) ; 11
stringstream ss { command ) ;

vector<string> words ;
for ( s tring s ; ss>>s ;
words . push_back ( s ) ; //

. .
. .

1 1 .6.
, , ,
, .
, ,
.
. , .
, .
7.8.2. . 1+4 *x<=y/ z * S
.
1 + 4 * <= / z * 5

>>. ,
, <=
( < = ).
z* (
* ).
:
for ( char ch ; cin . get ( ch) ; )
if (isspace ( ch) ) { // ch ,
11 (
11 )

if (isdigit (ch) ) {
11

else if (isalpha ( ch) )


11

else
11

istream : : get ( ) .
. >>.
448 1 1

get ( ) istream,
.
:
? ? .
.


isspace () ( ' ' , ' \ t ' , ' \n ' ..)?
isalpha () ( ' ' ' z ' , ' ' z ' ) ( : ' ' )?
" " '

isdigit (c) ( 1 1 " 1 9 1 )?


isxdigit (c) (
' ' " ' f ' 1 1 " 1 F 1 )?
isupper () ?
islower () ?
isalnum (c) ?
iscntrl () (ASCll 0"3 1 1 27)?
ispunct (c) , , , ,

isprint ( ) (ASCll 1 1"1
)?
isgraph () isalpha ( ) isdigi t ( )
ispunct () ( : )?

,
( 1 1 ) . , isalnum () isalpha ( )
1 1 isdigi t () ; , ,
.
,
, .


toupper ( )
tolower ()

, , ,
. , Right, right rigHT, .
, (, rigHT
<Caps Lock>).
tolower ( ) ,
: right.
string.
void tolower ( string& s) //
1 1 . 7. 449

for (char&x : ) = tolower (x) ;

string,
(. 8 . 5 . 5).
,
, .
tolower ( ) , toupper ( ) ,
, -
,
.

1 1 . 7 .

iostream .
. ,
istream , ,
,
, >> . , ,
?
4.6.3,
. , ,

planned , the guet arrived ; then ,

planned,
the
guet
arrived;
then ,

: "planned , " "arrived ; "


. , ,
.

. ?
, ,
"" .
trinq line ;
qetline (cin , line) ; 11 l in e
for (char& ch : line) 1 1
witch (ch) {
450 1 1

case ' ; ' : case 1 1 . case 1 , 1 .. case ' ? ' : case ' ! ' :
' '
ch = ;

strinqstream ss ( line) ; 11 i s tream s s l i n e


vector<strinq> vs ;
11
for ( strinq word ; ss>>word ;
vs . push_back ( word) ;

, :

planned
the
quests
arrived
then

. .
, ?

. ? ?
, ?
ps . whi tespace ( 11 ; : , 11 ) ; // , ,
//
for ( strinq word ; ps>>word ; )
vs . push_back ( word) ;

. , ps?
. .
-. .
, .. .
. .
as . not

not
as
not

, .
istream >>.
, istream, ,
, .

(, ..) ;
"
" . ,
1 1 .7. 451

; , .
Punct s tream.
class Punct stream { // i s t rea m ,
//
pulic :
Punct_strea. ( i stream& is)
: source { is } , sensitive { true } { 1

void whitespace (const strinq& s ) 11 s


{ white = ; 1 //
void add_white (char } //
{ white += ; 1 11
bool is_whitespace (char ) ; //
// ?

void case_sensitive (bool ) sensitive = ; 1


bool is_case_sensitive ( ) return sensitive ;

Punct_strea.& operator>> ( s trinq& s ) ;


operator bool ( ) ;
private :
istream& source ; //
istrinqstrea. buffer ; //
strinq whi te ; //
bool sensitive ; // ?
1;

, , -
istream , " "
, istringstream .
, , Punct_stream
: case_sensi tive ( ) ,
, , .
, Punct_stream

n i tes doq !

man
ites
doq

Punct_stream istream,
, source. ,
,
. Punct_stream,
cin, ,
452 11

, ,
. :
Punct_stream ps ( cin) ; // ps cin
ps . whi tespace ( " ; : . " ) ; // ,
11
ps . case_sensitive (false) ; 11

,
>>. .
, istream
line. ,
, ( ' ' ) .
istringstream buffer.
buffer >>.
.
, buffer,
, .
Punct_stream& Punct_strea. : : operator>> ( s trinq& s)
(
while ( ! (buffer>>s) ) 11
// buffer
if (buffer . bad ( ) 1 1 ! source . qood ( ) ) return * this ;
buffer . clear ( ) ;

strinq line ;
qetline ( source , line) ; 11 l i n e
1 1 so urce

11
for ( char& ch : line)
if ( is_whitespace ( ch) )
ch '= ' ; //
else if ( ! sensitive)
ch = tolower ( ch) ; //

buffer . str ( line) ; //

return * this ;

.
.
while ( ! (buffers ) )

buffer istringstream ,
buffer>>s s ,
w "; .
1 1 .7. 453

, buffer
. , buffer>>s
, . . ! (buffer>>s ) ,
buffer source. ,
buffer>>s ;
buffer .
while ( ! (buffer>>s ) ) { //
if (buffer . bad ( ) 1 1 ! source . qood ( ) ) return *this ;
buffer . clear ( ) ;

// buffer

buffer bad ( )
. :
buffer
. buffer,
" ", ,
- " " (eof ( ) ) buffer:
, buffer .
,
. ,
.
strinq line ;
qetline ( source , line) ; // l i n e
// so urce

//
for (char& ch : line)
if ( is_whitespace ( ch) )
'
ch ' ;
//
else if ( ! sensitive)
ch tolower ( ch) ; //

buffer . str ( line) ; 11

buffer,
. i s_whi tespace ()
Punct_stream, .
tolower ( ) - ,
.
(. 1 1 .6).
line istringstream.
buffer . s tr ( line) ;
: " buffer
istringstream line".
454 1 1

. ""
source getline ( ) .
,
! source . good ( ) .
. >> * this (
1 7. 1 0).
. . :
,
.
bool Punct_strea.. : : is_whitespace ( char )
(
for ( char w : white)
if ( c==w) return true ;
return false ;

. istringstream
(.
) , ,
.
.
Punct_strea.. : : operator bool ( )
(
return ! ( source . fail ( ) 1 1 source . bad ( ) ) && source . qood ( ) ;

istream
>>. .
while (ps>>s) ( / * . . . * / }

.
ps>>s. .
ps>>s Punct_stream,
Punct_stream l .
operator l ( ) Punct
,

stream. - operator l ( )
Punct_stream l . , true,
Punct_stream .
.
int 111&i n ( )
11
11 , ,
//

Punct_strea.. ps ( cin} ;
1 1 . 7. 455

ps . whitespace ( " ; : , . ? ! ( ) \ 11 { } <>/ &$@#% "' * 1 " ) ; // \ "


11 "
ps . case_sensitive ( false) ;

cout << " , cnoa\n" ;


vector<strinq> vs ;
for ( strinq word ; ps>>word ; )
vs . push_back ( word) ; //
sort (vs . beqin ( ) , vs . end ( ) ) ; 11
11
for ( int i=O ; i<vs . si ze ( ) ; ++i ) //
if ( i==O 1 1 vs [ i ] ! =vs [i-1] ) cout << vs [ i ] << ' \n ' ;

.
if ( i==O 1 1 vs [ i ] ! =vs [i-1 ] )

.
here are only two kinds of lanquaqes : lanquaqes that people complain
aout, and lanquaqes that people don ' t use .

:
aout
and
are
complain
don ' t
kind
lanquaqes
of
only
people
that
there
two
use

don ' t, dont?


whi tespace () .
: Punct_stream
istream, . ,
rdstate ( ) ,
eof ( ) , >>.
. ,
Punct_stream , istream.
Punct_i stream,
istream? ,
.
456 1 1

( - - ,
).
Punct_stream?
?
?
, ! !" , ! ,
?!" :
;
,
;
;
,, .
,
. .
. -

.
,
,
.

1 1 . 8.
- .
. ,
, . .
12 . 35.
12 , 35. ,
++
, -.
. ? .
?
, .
,
( , (Langer) Standard ++
IOStreams and Locales (Stroustrup) h ++ Program
ming Laguage). .
locale ( );
,,
.
1 1 .8. 457

;
iostream
streamuf. , iostream,
,
streamuf .
iostream iostream
, 38
h ++ Programmi.g Langua.ge .
++
- printf ( ) / scanf ( ) ,

. 27.6, . 1 0.2
(Kernlghan) (Ritchie) h Programming
Langua.ge, .
(
) -;
( -) .
1 1 1 .
- .
. .
1 2- 1 6 .

1 . Test_output . cpp.
irthyear .
2. irthyear .
.
3. .
4.
? , .
5. .
6. - ? ?
.
7. . 2
.
8. .
..
cin >> >>oct >> >> hex >> >> d ;
cout << << 1 \t ' << << ' \t ' << << 1 \t ' << d << ' \n ' ;
:
458 1 1

1234 1234 1234 1234

.
9. ,
1234567 . 8 9 : defaultfloat, - fixed
scientific.
? ?
1 0 . , , ,
.
, .

r
1 . - ?
2. << hex?
.
? ?
4. ,
.
5. ?
6. ,
.
7.
?
8. ?
9. , setprecision ( ) setw ( ) .
1 . ?
1 1 . "
": h, scientific, setprecision, showbase, setw?
1 2. .
1 3. .
.
1 4. ,
stringstream.
1 5. ?
1 6. ,
?
1 7. , , ?
1 8. isalnum () ?
1 1 .8. 459

defaultfloat
fixed
noshowbase
scientific
setprecision
showbase

1 . .
.
2. ,
, ,
. : getline ( ) .
3. . .
, Once upon time ! nc pn tm ! .
;
.
4. multi_inpu t . cpp,
,
,
;
.
:
43 () 67
0123 ( ) 83
65 ( 'l' ) 65

5. .
,
1 1 .6. ,
(, - , -
).
6. , .
( . ) , ( ; ) . ( , ) ,
(?), (-), ( ' ) .
, ( 11 ) .
. 11 - don ' t use the as-if rule . 11
11 don t use the as if rule 11
7. ,
don ' t do not, can ' t - cannot .. ;
460 1 1

( ,
"
do not use the as-if rule 11 );
.
8.
( .
1 1 .7). .
,
. .
9. - 1 1 .3.2 :
.
.
,
.
1 0. vector<string> spli t ( const string& s ) .
s,
.
1 1 . vector<string> split ( const string& s , const
s tring& w) , s,
. .
"" .
w.
1 2 . .
, asdfghjkl lkjhgfdsa. :
.
.
1 3. ( .
). . Norwegian Blue parrot
parrot Blue Norwegian. .
.
1 4 . .
(.
1 1 .6) .
1 5. . ,
. ,
, .
.
1 6. . .
,
.
, .
1 1 .8. 461

. ,
7 5 5 7 1 1 7 5 :

5
7 2
117

,
.

, ,
, . ,

, , -
.

f- -, .
/, 19() - 1t: .

- l .s"f.., (./,,., .... )f

(
,
) ,
,
, ,
. Line , Lines ,
Polygon , Axis Text , Shape .
Shape
,
.
. 1 3
, 1 4
-

1 - CUIA C alvln and Hob


bes. - . .
464 1 2

1
1 2. 1 . 1 2.7.2.
1 2.2. 1 2.7.3.
1 2.3. 1 2.7.4.
1 2.4. 1 2 .7.5.
r 1 2.7.6.
1 2.7.7.
1 2.7.8.
1 2.5.
1 2.7.9.
1 2.6. Shape
1 2 .7.10.
1 2.7. 1 2.8.
Shape
1 2.8. 1 .
1 2.7. 1 .
main

1 2. 1 . ?
-
(graphtcal user iterface - GUI)?
. ?
, ,
,
. , ? , -
, ,
, .
.
.
, ,

.
. .
,
,
,
. ( )
1 5.
11.
. ,
( )
. .
!
1 2.2. 465

11 .
,
. ,
, ,
. ,
, , ,
, ,
.
, 1 3 ,
.
- 1111 .


. -
.

, ()

,
.
-
.
, -
,
(
22),
,
,
- .
. 11 .
.
, ,
. ,
, , " ",
.

1 2 .2.
- ios tream
. ,
,
466 1 2

. ,
.
, rotf, leX, Word, HTML L (
). .
<hr>
<h2>

</h2>
:
<ul>
<li><></> , EPddd , . . . </li>
<li><></> , Eiddd , . . . </li>
<li><>n</> , ESddd , . . . </li>
</ul>
<> . . .
<>

HTML, (<h2> . . .
</h2>). (<ul> . . . </ul>) (<li> . . . </li>)
(<>). ,
. I -
, ,
,
, .
(, ,
), .

: ,
.
- , , , -
( ,
).

.
.
,
, . "" -
,
. , ,
" ( - "") ", " ",
" " () "
, ", ,
, .
1 2.3. 467



attach() draw()

" "

attach()

,
, ..
" ", ,

, . ,

.
.

1 2. .
- ,
. .
. , :
#include " Siple_window . h" 11
11
#include "Graph . h" //
//
int ma.in ( )
{
using namespace Graph_lib ; //
11 Graph_ l i b
Point tl { 100 , 100 } ; //

Siple_window win { tl , 600 , 400 , "Canvas" } ; //

Polygon poly ; 11 ()

poly . add ( Point { 00 , 200 } ) ; //


poly . add ( Point { S0 , 100 } ) ; 11
poly . add ( Point { 400 , 200 } ) ; 11

poly . set_color ( Color : : red) ; // poly


468 1 2

win . attach (poly) ; 11 poly

win . wait_for_button ( ) ; //
11

, .

, .

.
#include " Simple_window . h" 11
//
#include "Graph . h" 11
11

main ( ) ,
Graph_lib.
usinq namespace Graph_lib ; //
11 Graph_ l i b

.
.
1 2.3. 469

Point tl ( l00 , 100 } ; 11

.
Simple_window win ( tl , 600 , 400 , "Canvas " } ; //

Simple_window,
Graph_lib. Simple_window
win: . win Simple_window.
-

win tl,
, 600
400. , , , .
, ,
. Canvas
. ,
Canvas .
.
Polyqon poly ; 11 ()

poly . add ( Point ( 00 , 200 } ) ; 11


poly . add ( Point ( S0 , 100 } ) ; //
poly . add ( Point ( 400 , 200 } ) ; //

poly, .
Polygon
: ,
. , .
,
.
,
.
poly . set_color ( Color : : red) ; // po ly

poly win.
win . attach (poly) ; 11 poly

, .
(, Simple_window) (
poly), (Color : : red)
win, .
.
win . wait_for_button ( ) ; 11
11


, .
470 1 2

wai t_for_utton ( ) ,
, Next Simple_window.
,
. ,
. .
.

, .
Next? Simple_window. 1 6
Simple_window "" Window,
, , ,
.
Next
,
(" ").
,
, .
Microsoft Windows,
.
, (
). ,
.
, .
( - Canvas).
1 2. 4. 471

1 2.4.



. -

. - ,
, .
, -
.. , ++
,
-.
.

, ,


.

, , FLTK (Fast
Light Tool it, "full tick") www . fl tk .
org. .
(Windows, Unix, , Linux .).
,
.
.
, ,
. ,
600
++,
FLTK 370 .
- www . fl tk . org, .
.
, 1 2- 1 6,
.
, .
FLTK. . ,
( ) .
" "
.
472 1 2

/GUI
( - FLTK)


(Windows, Linux)



.
,
,
, ( ,
1 6).

1 2. 5 .
- .
. - .
.

.
. .

. .
(0 ,.""'---- (200,0) --

(50,50)

(0, 1 00) (200, 1 00)


!
1 2.6. Shape 473

, ,
. ,
( , )
, - , .
:
1 024768, 1 280 1 024, 1 450 1 050 1 600 1 200.

,
. , . ,
. ,

Simple_window win ( tl , 600 , 400 , " Canvas " } ;

, ,
600 , - 400,
599 399 . ,
, (canvas). 600400
"" , .. , ,
; ,
.

1 2.6. Shape

.

Wlndow [ Llne_style ) Color )

, , ,
. , . ,
Polygon , Shape;
, Polygon Shape.
:
Simple_window, Window;
Shape, Text, Polygon, Line, Lines, Rectangle, Function .. ;
474 12

Color, Line_style, Point;


Axis .
( 1 6)
:
Button, In_box, enu ..

,
Spline, Grid, Block_chart, Pie_chart ..


.

1 2 . 7 . Shape

: Simple_window, Window, Shape , Text,
Polygon, Line, Lines, Rectangle, Color, Line_style, Point, Axis.
- ,
, .
.
,
.
, ,
, . ,
.

1 2. 7 . 1 . main
,
.
#include "Window . h " 11
#include " Graph . h "

#include " Siple_window . h" // Nex t
#include "Graph . h "

, , , Window . h ,
. Graph . h - ,
( ) .
Graph_lib.
using namespace,
Graph_lib .
1 2. 7 . Shape 475

using namespace Graph_lib ;

, main ( ) ,
( ), .
int main ( )
try
{
// . . . . . .

catch (exception& )
11
return 1 ;

catch ( . . . )
11
return 2 ;

main ( ) ,
exception. , ,
std_lib_facili ties . h

<s tdexcept>.

1 2. 7 .2.
(. 5,
5.6.3), main ( ) :
Point tl { l00 , 100 } ; //

Simple_window win { tl , 600 , 400 , "Canvas " } ;


// t l
11 : 600400
// : Ca n va s
win . wait_for_button ( ) ; //

Simple window, . .
Next, . ,
Simple_window
Simple_window . h, Window . h. ,
:
Point{ 1 0 0 , 100 } . ,
. , Point ,
-


(, ). :
Simple_window win { Point { l00 , 100 } , 600 , 400 , "Canvas " } ;
476 1 2

( 1 00, 1 00) ,
. 600 - , 400 -
, "Canvas" - , .
,

. win . wait_for_button ( ) .
.

(
) . , ,
, , ,
.
, ,
. ,
,

. , ,
Microsoft ++,
(, Borland GNU).
1 2. 7 . Shape 477


, .

( )
. ,
.

1 2. 7 ..
,
- . ?
, - ,
- , .
, , . ,
, . ,

, :
,
. . .
Axis xa {Axis : : x , Point (20 , 300) , 280 , 10 , " axis " ) ; // Axi s
11 Ax i s - Shape
11 Ax i s : : x
11 - (2 0 , 3 0 0 )
// - 2 8 0
11 1 0
// " " -
win . attach () ; // wi n
478 1 2

win . set_lael ( " Canvas i 2 " ) ; 11


win . wait_for_button ( ) ; 11 !

: Axis ,
.

, Axi s : : .
, " axis". ,
, . ,
- .
-
, "- "
, , _max-bottom_
margin, " ", 300 (.
4.3. 1 1 5. 6.2).
,
" Canvas i2 " - set label ( )
Window.
.
Axis ya {Axis : : y , Point { 20 , 300 ) , 280 , 10 , " axis " } ;
ya . set_color ( Color : : cyan) ; 11
ya . lael . set_color ( Color : : dark_red) ; 11
win . attach (ya) ;
win . set_lael ( "Canvas i " ) ;
win . wait_for_button ( ) ; 11

, ,
, -.
1 2. 7 . Shape 479


. ,
.
. ,
, .

1 2. 7 .4.
? ,
. ,
, .
Function sine { sin , 0 , 100 , Point { 2 0 , 150 } , 1000 , 50 , 50 } ;
11
11 sin () [ 0 , 1 0 0 ) ( 0 , 0) (20, 1 5 0 ) ,
11 1 0 0 0 ;
11 5 0 )f(

win . attach ( sine} ;


win . set_la.el ( "Canvas # 4 " ) ;
win . wait_for_button ( ) ;

Function sine ,
sin () .
1 5.3. ,
(
Point) ,
, .
480 1 2

Canvas #4 f:Jrg:ll)
.. 1

. .
,
.

1 2. 7 .5.
.
1 5.
, .
,
(, )
. Polygon
, .
, ,
.
sine . set_color ( Color : : ue) ; 11

Polyqon poly ; 11 Polygon - Shape


poly . add ( Point ( 00 , 200 } ) ; 11
poly . add ( Point ( S0 , 100 } ) ;
poly . add ( Point ( 400 , 200 } ) ;

poly . set_color (Color : : red) ;


poly . set_style (Line_style : : dash) ;
win . attach (poly) ;
win . set_lael ( "Canvas lt5 " ) ;
win . wait_for_button ( ) ;
1 2 .7 . Shape 481

(sine) .
, . ,
1 2.3,
. .
Polygon . ,
, .. ( - 1 3. 5) .
.

1 2. 7 . 6.
- , -
- .
( ) -
. .
( ,
). .
. ,
.

.
. ,
Rectangle,
Polygon. Rectangle
. .
Rectanqle r { Point { 200 , 200 } , 100 , 50 } ; // , ,
win . attach ( r) ;
482 12

win . set_lael ( "Canvas # 6 11 ) ;


win . wait_for_button ( ) ;

Canvas #6 l/Qlg]
s

x axls

, , , ,
, Rectangle
. Closed_polyline,
Rectangle (
Open_polyline, ).
Closed_yolyline poly_rect ;
poly_rect . add ( Point { l00 , 50 } ) ;
poly_rect . add ( Point { 200 , 50 } ) ;
poly_rect . add ( Point { 200 , 100 } ) ;
poly_rect . add ( Point { l00 , 100 } ) ;
win . attach (poly_rect) ;

::u poly_rect
. poly_rect
Rectangle "" .
, .
poly_rect . add ( Point ( 50 , 75 ) ) ;

.
1 2 . 7 . Shape 483

, Rectangle
, ,
, . -
, , Rectangle
.
484 1 2

1 2. 7 . 7 .
,
. .
r . set_fill_color ( Color : : yellow) ; 11
poly . set_style ( Line_style (Line_style : : dash , 4 ) ) ;
poly_rect . set_style (Line_style (Line_style : : dash , 2 ) ) ;
poly_rect . set_fill_color (Color : : green) ;
win . set_lael ( " Canvas # 7 " ) ;
win . wait_for_button ( ) ;

,
(poly) , (
). poly_
rect ( ).

poly_rect,
, .
( 1 3.9).
( ).

1 2. 7 .8.
,
,
- -
.
1 2. 7 . Shape 485

, , , ,
,
Text.
Text t { Point { lS0 , 150 } , " Hello , qraphical world ! " } ;
win . attach ( t) ;
win . set_lael ( "Canvas 118 " ) ;
win . wait_for_button ( ) ;

, ,
.
: , ,
".
.
,
.
, : Axis (.
1 2. 7.3) Text. ,
.
t . set_font ( Font : : ties_bold) ;
t . set_font_size ( 20 ) ;
win . set_lael ( "Canvas 119 " ) ;
win . wait_for_button ( ) ;

"Hello , graphical world ! " 20


Times.
486 1 2

1 2. 7 . 9 .
.
Ia.qe i i { Point { l00 , 50 } , " imaqe . jpq" } ; // 4 0 0 2 1 2
win . attach ( ii) ; // jpg
win . set_lael ( "Canvas #10" ) ;
win . wait_for_button ( ) ;

image . jpg - ,
.
1 2. 7. Shape 487


. ,
.
ii . move ( l00 , 200) ;
win . set_lael ( " Canvas #11 " ) ;
win . wait_for_button ( ) ;

, , ,
. , ,
.

1 2. 7 . 1 .

Circle c { Point { l00 , 20 0 } , 50 } ;
Ellipse e { Point ( l00 , 200 ) , 75 , 25 } ;
e . set_color ( Color : : dark_red) ;
ark m{ Point ( l00 , 200 } , ' x ' } ;

ostrinqstream oss ;
oss << " screen size : " << _ ( ) << " * " << _max ( )
<< " ; window size : " << win . x_max ( ) << " * " << win . y_max ( ) ;
Text sizes ( Point ( l00 , 20 } , oss . str ( ) } ;

Imaqe cal ( Point ( 225 , 22 5 } , " snow_cpp . qif" } ; // 320 * 2 4 0 , gi f


cal . set_mak ( Point ( 40 , 40 } , 200 , 150 ) ; 11

win . attach (c) ;


488 1 2

win . attach (m) ;


win . attach (e) ;

win . attach ( sizes) ;


win . attach ( cal) ;
win . set_lael ( "Canvas #12 " ) ;
win . wait_for_button ( ) ;

, ?
?

, ,
. ,
, : . -
, ,
, ostringstrea.m (.
1 1 .4) .

1 2 . 8 .
,
. , Shape
, .
,
, , .
main ( ) ,
, ,
1 2.8. 489

, FLTK
( ).
. . .
(main ( ) ..).
(Window, Shape, Polygon ..).
FLTK.
++.
, .
.

.

Point.h:

struct Point ( . };
FLTK

// :
class Window (".};
Graph.h:

// :
struct Shape ( ". } ;

// GUI:
window.cpp: struct ln_box ( ". } ;

Graph.cpp: GUl.cpp:
Simple_window.h:
Ko GUI
// :
class Simple_window ( " . } ;

chapter 1 2.:

#include "Graph.h"
#include "Simple_window.h"
int main() ( ". }

,
.
490 1 2

1 2.8. 1 .

.

Point . h Window . cpp
Window . h Graph . cpp
Simple_window . h GUI . cpp
Graph . h

GUI . h
1 6
.

-
"Hello , World ! -
"

.
1 . , Simple_window
600400 window, ,
. ,
FLT. ,
Graph . h Simple_Window . h,
Graph . Window . .
2. 1 2.7,
.
3. , (,
, )
.


1 . ?
2. ?
3. ?
4. ?
5.
( )?
6.
?
1 2.8. 491

7. ?
8. ?
9. ?
1 . ?
1 1 . ,
?
1 2. ,
.
1 3. ?
1 4. ,
?
1 5. ?
1 6.
.
1 7 . , Window.
. ?
1 8. , Shape,
. ?

GUI GUI
HTML
JPEG
XML
FLTK

;
Simple_window.
1 . Rectangle
Polygon. Polygon ,
Rectangle - .
2. Rectangle 1 0030
"Howdy!"
3. 1 50 .
. .
492 1 2

4. - ,
.
5.
,
, - .
6. , ,
? , ,
? , .
7. , : ,
. ,
.
8. . ,
.
9. .
.
1 0 . 1 2.8.
1 1 . , .

, .
, .. :
N- (N+ l ) -. :
<cmath> (
24.8, . 9.2)
1 2 . - ,

rn ,n > .

u,
, . ,
, . .
, , m, n N . N
, , , m n.
(
" "). N
( ,

N-1 , .. ).
1 3. .
.
1 2.8. 493


.
, -
, - . -
, .
, ,
Circle Polygon.
, , ,
. ,
(
).
,
. , -

.
: ,
, .

51, NA, ,

NA,
tJ

1 2 ,

.
.
,
,
Point, Color , Polygon Openyolyline ,
.
,
, .
496 1 3

1 3. 1 . 1 3 . 10.
1 3.2. Point Line
1 3.3. Lines 1 3. 1 1 . Text
1 3.4. Color 1 3 . 1 2. Circle
1 3.5. Line_style 1 3. 1 3. Ellipse
1 3.6. Openyolyline 1 3 . 1 4. arkedyolyline
1 3.7. Closedyolyline 1 3 . 1 5 . arks
1 3.8. Poly9on 1 3. 1 6. ark
1 3.9. Rectan9le 1 3. 1 7. Ima9e

1 . 1 .

. ""
, . .
.
.
. !

.
J.l. . .
. .
- .

.
,
. ,
.
.

. 1 4
.

. ,
.
++.

.
1 3. 1 . 497


Color ,
Line_style
Point
Window
Line , ,
Point
Open_polyline ,
Point
Closed_polyline Open_polyline, ,

Polyqon Closed_polyline,

Text
Lines , Point
Rectanqle - ,

Circle ,
Ellipse ,
Function ,
Axis
ark , (, )
arks , (, )
arked_polyline Open_polyline ,
Imaqe

Function Axis 1 5. 1 6
.


Window ,
Simple_window Next
Button , ,

In , ,

Out , ,

Menu Button

.
498 1


Point . h Point
Graph . h
Window . h Window
Simple_window . h Simple window
_

GUI . h Button GUI


Graph . cpp Graph . h
Window . ccp Window . h
GUI . cpp GUI . h

,
,
Shape Widget.

Shape Widget
Vector ref ,

, , .
, ,
- (

). .
.
,
.
,
, .
, ? -?
- ,
(
- ).
, , , -
.

1 3 .2. Point Line



. ,
. ,
-
1 .2. Point Line 499

(, ). 1 2.5,
( ) max ( ) ( );
_

( ) _max ( ) (
).
Point . h, Point
-

int ( ):
struct Point {
int , ;
};

bool operator== ( Point , Point ) { return . == . && . =- . ;


bool operator ! = ( Point , Point ) { return ! ( --) ; }

Graph . h Shape,
1 4, Line.
struct Line : Shape { // Line - Shape ,
11
Line ( Point pl , Point 2) ; 11 Line
11 Poi n t
1;

Line Shape.
-

" : Shape". Shape (base class) Line.


Shape ,
Line. ,
Line Openyolyline. , (. 1 4).
Line Point.
"" ( #include ,
1 2 . 3),
.
//
constexpr Point { 100 , 100 }

Simple_window winl { x , 600 , 400 , " wo lines " } ;


Line horizontal { x , Point { 200 , 100 } } ; //
Line vertical { Point { 150 , 50 } , //
Point { 150 , 150 } } ;
winl . attach (horizontal ) ; //
winl . attach (vertical) ;
winl . wait_for_button ( ) ; 11

, .
, ,
, Line .
, ,
Line vertical { Point { 150 , 50 } , Point { 150 , 150 } } ;
500 1

() , ( 1 50, 50) ( 1 50. 1 50).


, ,
, . Line
.
Line : : Line ( Point pl , Point 2) //
{
add (pl) ; 11 pl
add (p2 ) ; 11 2

, . ?
Line ? Shape.
1 4 , Shape ,
, , , ,
add ( ) , Shape Point.
, Line
. ".
,
.
1 3.3. Lines 501

Simple_window
t tach ( ) . "",
,
Shape.

1 .. Lines
, . ,
, ,
, , , , ,
, ..
" " Lines .
struct Lines : Shape { 11
Lines ( ) { } 11
11 :
Lines (initializer_list<Point> lst) ;
void draw_lines ( ) const;
void add ( Point pl , Point 2 ) ; 11 ,
}; 11

Lines ,
Point. .
1 3. 2
, :
Lines ;
x . add ( Point { l00 , 100 } , Point { 200 , 10 0 } ) ; 11 :
11
x . add ( Point { l50 , 50 } , Point { l50 , 150 } ) ; 11 :
11

(
), Line.
, , -
.
Line
Lines
, . Lines,
,
. .
, Lines, .
, ,
Line, .
.
,
502 1

. ,
Lines grid.

int size = win . x_max ( ) ; / /


int y_size = win . y_max ( ) ;
int x_qrid = 80 ;
int y_qrid = 40 ;
Lines grid;
for ( int x=x_qrid ; x<x_size ; x+=x_grid)
grid. add ( Point { x , 0 } , Point { x , y_size } ) ; 11
for ( int = y_grid; y<y_size ; y+=y_grid)
grid. add ( Point{ O ,y} , Point { x_size , y } } ; 11

,
_max ( ) _max ( ) . ,
. , .
.
, .
.
Lines . -
Lines? Lines .
add () , ,
, .
void Lines : : add { Point pl , Point 2)
{
Shape : : add (pl) ;
Shape : : add (2 ) ;
1 .. Lines 503

Shape : : ,
add (pl ) (
) add ( ) Lines,
Shape.
draw lines ( ) ,
add ( ) .
void Lines : : draw_lines ( ) const
{
if (color ( ) . visibility ( ) )
for (int i=l ; i<numer_of_yoints ( ) ; i+=2)
fl_line (point ( i-1) . x , point ( i-1) . ,
point ( i ) . x , point ( i ) . ) ;

. Lines : : draw_lines ( )
( 1 ) ,
fl_line ( ) .
(visility) Color ( 1 3.4),
-

, , ,
.
1 4, draw_lines ( ) "
". , ,
add ( ) Lines .
Shape numer_of__points ( ) point ( ) (.
1 4.2), .
Shape. -
504 13

draw_lines () const (. 9 . 7 . 4),


.
Lines
( ):
, add () .
, .
, initializer_list
, .
( 1 8. 2)
Lines, , 1 , 2, . . , "

:
Lines = {
{ Point { l00 , 100 } , Point { 200 , 100 } } , //
{ Point { 150 , 50 } , Point { 150 , 150 } } //
};
:
Lines = {
( ( 100 , 100 } , {200 , 100 } } , //
( ( 150 , 50 } , ( 150 , 150 } } //
};
:
void Lines : : Lina ( initializer_lit<pair<Point , Point>> lst)
{
for ( auto : lt) add (p . first , p . econd) ;

auto pair
<Point , Point>. first second
. initializer_l i s t pair
(. .6.4 .6.3 ).

1 .4. Color
Color , .
-

:
grid . set_color (Color : : red) ;

, grid,
. ,
:
Color
.
13.4. Color 505

struct Color {
enWll Color_type
red =FL_RED ,
u =FL_BLUE ,
green =FL_GREEN ,
yellow =FL_YELLOW ,
white =FL_WIE ,
k =FL_BLACK ,
magenta =FL_AGENA,
cyan =FL_CYAN ,
dark red =FL_DAR_RED ,
dark_green =FL_DAR_GREEN ,
dark_yellow =FL_DAR_YELLOW,
dark u =FL_DAR_BLUE ,
dark_agenta=FL_DAR_AGENA,
dark_cyan =FL DAR CYAN
};

enWll Transparency { invisile = , visie=255 } ;

Color (Color_type } : c { Fl_Color ( cc} } , v{visile} { }


Color (Color_type , Transparency vv)
: c { Fl_Color (cc) ) , v { vv } { }
Color (int ) : c { Fl_Color ( cc) } , v{visile} { }
Color (Transparency vv) : c { Fl_Color ( ) } , v { vv } { } //
11

int as_int ( ) const { return ; }

char visibility ( ) const { return v ; }


void set_visibility (Transparency vv) { v=vv ; }
506 1

private :
char v; //
Fl Color ;
);

Color .
Fl_Color FLT.
Fl_Color Color_type.
.
( ) .
.
. Color : : dark_lue.
"" ,
(
0-255; , Color ( 99)
- ).
1 3.9.
RGB (Red, Green , Blue - ,
, ). .
. .
"RGB color" ,
, http : //en . wikipedia . org/wiki/RGB_color_model www .
rapidtaes . com/we/color/RGB_Color . htm. (. . 1 3 1 4.)
Color,
Color_type,
int. .
,
, , Color
,
. ,
.
Fl_Color, FLT.
.
RGB ( ).
as int ( ) .
, as_int ( ) -,
Color, .
v,
Color : : visie Color : : invisie, .
: " ". ,
, .
1 .5. Line_style 507

1 3.5. Line style


_
,
. . - ,
. Line_style :
grid. set_style (Line_style : : dot) ;

, grid,
, .

, .
() , .
Line_style :
struct Line_style {
enum Line_style_type {
solid =FL_SOLID , 11 ------

dash =FL_DASH , // - - - -
dot =FL_DOT , 11
dashdot =FL_DASDOT , 11 -
dashdotdot=FL_DASDODOT , 11 - . . - . .
) ;

Line_style (Line_style_type ss) : s { s s } , w{ O } { }


Line_style (Line_style_type lst, int ww) : s { lst} , w{ww} { }
Line_style (int ss) : s { s s } , w{ O } { }
int width ( ) const { return w; }
int style ( ) const { return s ; }
private :
int s ;
508 1

int w;
};
,
Line_style, ,
Color. ,
F int.
?
. FLTK
Fl_linestyle,
.
int , , .
, ;
, ( ,
).
, .
- ,
, ,
.
Line_style u" :
(, i ) (
). .
. ,
:
qrid . set_style (Line_style (Line_style : : dash , 2 ) ) ;
.


t 1 ' 1 1
1 1
1 1 1 1 1 t
1 f 1 1 t

'1 1 1
---------}-------}------------------}--------}--------- -------------


1 t 1
1 1 !
1 ' ' 1 1 1
1 1 1
--------- -------- --------- --------- ------ -+- ---------r -------- - -- -

1
t 1
1 -1 -1 1
1
' 1 1 1 \"
1 1

:
! 1 1,
-------- -------- --------- -------- --------- -------- --------- ---

1 1 : : : l
1 1 1
-------------------------------------------------------------}.----

4-1
11
1 1 1
1 '
1 1 1 1 1 1
1 1 1 '
--------- r - ---- -------- ---------} ---------------------
--"-----

1 t 1 1
i 1 1 1 t 1
1 1
j '( j 1 1 1 1

1
-------------------------------------------------------------------
1 1 J f 1 J
1


1
'

1 1
1 1 ' t 1 J
-------- --------- ------- -- ------- ----- ------------- --------- --

1
1
t1
J
1
1
1
1 f
' 1 1
J
1 '
1

1
---------}-----------------}---------}-----------------}------------
1
t
1
1
1 1
1

1 '
t

1
1 1 t 1 1 1 1

:
-----------------1- --"----- -------- --------- -------- --------} --

! i : : ; 1
1 1 1
1
1 1 1
1 1 1 1 1 1
1 3.6. Open_polyline 509

, .
.
. Lines, Open_polyline
Polygon.
, Line.
.
horizontal . set_color (Color : : red) ;
vertical . set_color (Color : : green) ;
, .

!
f 1

1 3.6. Open_Polyline
Open_polyline ,
, .
poly "", polylie -
, . .
Open_yolyline opl = {
{ 100 , 100 } , { 150 , 200 } , { 250 , 250 } , { 300 , 200 }
};
, ,
:
. Open_polyline ,
-

"Connect the Dots" (" ").


Open_polyline .
510 1 3

struct Open_yolyline : Shape { //


usinq Shape : : Shape ; // Shape ( . 1 6)
void add (Point ) { Shape : : add (p) ; )
);
-. . ,
, Shape.
add ( ) Openyolyline
add ( ) Shape (.. Shape : : add ( ) ).
draw_lines ( ) . Shape
.
.
using Shape : : Shape using-.
, Openyolyline .
Shape. Shape
( 9.7.3) ( 1 8.2).
using-
Openyolyline.
Lines,
add ( ) .

1 3.7. Closed_Polyline
Closedyolyline Openyolyline,
, . ,
Closedyolyline ,
Openyolyline 1 3.6.
1 3.7. Closed_polyline 511

Closed_yolyline cpl =
{ 100 , 100 } , { 150 , 200 } , { 250 , 250 } , { 300 , 200 }
};

, ,
1 3.6, .

Closed_Polyline .
struct Closed_yolyline
: Open_yolyline { //
using Open_yolyline : : Open_yolyline ; //
11 Open_polyline ( . 1 6)
void draw_lines ( ) const ;
};

void Closed_yolyline : : draw_lines ( ) const


{
Open_yolyline : : draw_lines ( ) ; 11 ,
// -
if (2<numer_of_yoints ( ) & & color ( ) . visibility ( ) )
fl_line (point (numer_of_yoints ( ) -1) .,
point (numer_of_yoints ( ) -1) . ,
point ( O ) .,
point ( O ) .) ;

using- ( . 1 6) , Closed_Poly
line . Open_Polyline. Closed_Poly
line draw_lines ( ) ,
, .
512 1 1

, ,
Closed_polyline Open _yolyline.
" " ("programming
difference"). ,
(Closed_yolyline) (Open_yolyline).
, ?
fl_line () FLTK.
int, .
. , ,
, FLTK
.
fl line () ,
.
FLTK
, .

1 .8. Polygon
Polygon Closed_yolyline.
, Polygon
. , Closed_yolyline, ,
, ,
.
cpl . add ( Point ( l00 , 250) ) ;
.
1 3.8. Polygon 513

Closed_
polyline . Polygon ,
Closed_polyline,
? .
Polygon - Closed_polyline,
. ,
, Polygon - Closed_polyline,
Point, ,
Polygon.
Polygon :
struct Polyqon : Closed_yolyline { //
//
usinq Closed_yolyline : : Closed_yolyline ; //
!/ Closed_polyl i n e
void add (Point ) ;
void draw_lines ( ) const ;
};

void Polyqon : : add (Point )


1
/! ,
11 ( )
Closed_yolyline : : add(p) ;

draw_lines ( )
Closed_polyline, .
, add ( ) .
,
N2, - Polygon, N .
N (N - 1)/2 intersect ( ) . ,

, Polygon
. ,
Polygon, 24 ,
24 (24 - 1)/2 = 276 intersect ( ) . ,
, 2000
, 2 .
,
.

:
Polyqon poly =
{ 100 , 100 } { 150 , 200 } , { 250 , 250 } , { 300 , 200 }
/

1;
51 4 1

, : Polyqon,
( ) : Closed_Polyline.

, : Polyqon
. .
. Polyqon : : add () .
.
.
.
: Polyqon ,
- " " - .
. ,
,
: Polyqon. add ()
,
,
: , .

1 3.9. Rectangle
.
(
, , , ,
),
( , ).
,
1 .9. Rectangle 51 5


, . .
struct Rectanqle : Shape {
Rectanqle (Point :, int ww, int hh);
Rectanqle (Point , Point ) ;
void draw_lines ( ) const;

int heiqht ( ) const return h ;


int width ( ) const return w ;
private :
int h ; //
int w; //
};

(
) , .
i :
Rectanqle : : Rectanqle ( Point :, int ww, int hh)
: w{ww} , h{hh )

if (h<=O 1 1 w<=O)
error ( "Ollla : : " ) ;
add(ry) ;

Rectanqle : : Rectanqle ( Point , Point )


: w{ y . x-x . x} , h { y . y-x . y }

i f (h<=O 1 1 w<=O)
error ( "Ollla : 1111) ;
add (x) ;

h
w ( ; . 9.4.4)
Shape (
add ( ) ). ,
- .
,
,
, .
, , ,
, . Polygon
Circle. " " - ..
-
, .
set_fill_color ( )
516 1

( Shape
).
Rectanqle rectOO { Point { lS0 , 10 0 } , 200 , 100 } ;
Rectanqle rectll { Point { 50 , 50 } , Point { 250 , 150 } } ;
Rectanqle rectl2 { Point { S0 , 150 } , Point { 250 , 250 } } ; // rec t l 1
Rectanqle rect2 l { Point{ 250 , 50 } , 200 , 100 } ; 11 rec tl 1
Rectanqle rect22 { Point ( 250 , 150 } , 200 , 100 } ; 11 rec t21

rectOO . set_fill_color (Color : : yellow) ;


rectll . set_fill_color ( Color : : ue) ;
rectl2 . set_fill_color (Color : : red) ;
rect21 . set_fill_color (Color : : qreen) ;
.

.
; rectOO .
(. 1 4. 2.3).
.
rectll . move ( 400 , 0 ) ; // rect21
rectll . set_fill_color (Color : : white) ;
winl2 . set_lael ( "rectanqles 2 " ) ;

.
,
rectl l . , , ""; .
.
1 .9. Rectangle 51 7
-

rectangles 2 (Q)

, :
. , :
. . :
Window ( .3)
( Window : : put_on_top ( ) ). ,

win12 . put_on_top (rect00) ;
win12 . set_lael ( "rectanqles 3 " ) ;
.
518 1 111

, .
. . ,
.
rectOO . set_color (Color : : invisile ) ;
rectll . set_color ( Color : : invisie) ;
rect12 . set_color (Color : : invisile ) ;
rect21 . set_color (Color : : invisile) ;
rect22 . set_color (Color : : invisile ) ;
.

rect22 .
invisie. .
-
, - - draw_lines ( ) Rectangle
.
void Rectanqle : : draw_lines ( ) const
{
if (fill_color ( ) . visibility ( ) ) { //
fl_color ( fill_color ( ) . as_int ( ) ) ;
fl_rectf (point ( O ) . x , point ( O ) . y , w , h) ;

if (color ( ) . visibility ( ) ) { //
fl_color ( color ( ) . as_int ( ) ) ;
fl_rect (point ( O ) . x , point ( O ) . y , w, h) ;
1 3.1 D. 519

. FLTK
(fl_rectf ( ) ) (fl_rect ( ) )
. .

(
) .

1 .1 .
.
. .
,
256 , ; .
256 1 6 1 6.

256 ,
. , ""
(.) , -

(i,J1. .
.
vector<Rectangle>. , . ,
(),
.
1 4., :
, :
teplate<class > class Vector ref
puic:
520 1

11
void push_back (T&) ; 11
void push_back (T*) ; //

& operator [ ] ( int i) ; 11 :


const & operator [ ] ( int i ) const ; //

int size ( ) const;


};
vector
.
Vector_ref<Rectanqle> rect;
Rectanqle x{ Point{ l00 , 200 } , Point{ 200 , 300 } } ;

11
rect . push_back (x) ;

11
rect . push_back (new Rectanqle { Point { 50 , 60 } , Point { 80 , 90 } } ) ;

//
for ( int i=O ; i<rect . size ( ) ; ++i) rect [ i ] . move ( l0 , 10) ;

new 1 7, Vector_ref
. ,
.
new ( Rectangle)
( { Point{ 50 , 60 } , Point{ 80 , 90} } ) .
,
.
Rectangle Vector_ref
. ,
, 256 .
Vector_ref<Rectanqle> vr ;

for (int i = ; i<l ; ++i )


for ( int j = ; j<l ; ++j )
vr . push_back (new Rectanqle { Point { i*20 , j *20 } , 20 , 20 } ) ;
vr [vr . size ( ) -1 ) . set_fill_color (Color { i *l+j } ) ;
win20 . attach (vr [vr . si ze ( ) -1 ] ) ;

Vector_ref. 256
Rectangle, Window
1 6 1 6. Rectangle , 1, 2, 3, 4
..
.
1 .1 1 . Text 521

1 . 1 1 . Text
, .
. "" Closed_yolyline
1 3.8
Text t ( Point (200 , 200) ," closed polyline that isn ' t polyqon" ) ;
t . set_color (Color : : u) ;
. .
522 1 3 11

Text , ,
Point.
. , ,
.
;
, . string,
Text (.
1 2. 7 . 7 1 2. 7.8) (. 1 1 .4).
Text .
struct Text : Shape {
11
Text (Point , const strinq& s )
la{ s } ,
{ add (x) ;

void draw_lines ( ) const ;


void set_lael (const strinq& s ) { l s; }
strinq lael ( ) const { return l ; }

void set_font (Font f) { fnt = f ; }


Font font ( ) const { return fnt ; }

void set_font_size ( int s) { fnt_sz = s ; }


int font_size ( ) const { return fnt_sz ;
private :
strinq ls; //
Font fnt { fl_font ( ) } ;
int fnt sz { (fl_size ( ) <14 ) ? 14 : fl_size ( ) } ;
};
, 1 4
, FLTK ,
. ,
.
FLTK,

,
.
, ,
.
Text - draw_lines ( ) ,
, .
void Text : : draw_lines () const
{
fl_draw ( la . c_str ( ) , point ( O } . x , point ( O ) .) ;
1 3.1 2. Circle 523

, r,
(. Openyolyline Circle).
set_color ( ) . ,
. - color ( ) .
. .
.
class Font { //
puic :
enum Font_type {
helvetica =FL_HELVETICA ,
helvetica bold =FL_HELVETICA_BOLD,
helvetica_italic =FL_HELVETICA_ITALIC ,
helvetica_old_italic=FL_HELVETICA_BOLD_ITALIC ,
courier =FL_COURIER,
courier bold =FL_COURIER_OLD,
courier italic =FL_COURIER_ITALIC ,
courier bold italic =FL_COURIER_BOLD_ITALIC ,
tilD8S =FL_TIES ,
times bold =FL_TIES_BOLD ,
ti&s i talic =FL_TIES_ITALIC ,
times bold italic =FL_TIES_BOLD_ITALIC ,
symol =FL_SYOL ,
screen =FL_SCREEN,
screen bold =FL_SCREEN_BOLD ,
zapf_dingbats =FL ZAPF DINGATS
};

Font (Font_type ff) : f { ff } { }


Font (int ff) : f { ff } { }

int as_int ( ) const { return f ; }


private :
int f ;
};
Font
Color (. 1 3. 4) Line style (. 1 3. 5).

1 . 1 2. Ci rcle
. .
, Circle Ellipse. Circle
.
struct Circle : Shape {
Circle (Point , int rr) ; //

void draw_lines ( ) const ;


524 1 3

Point center ( ) const ;


int radius ( ) const { return r ; }
void set_radius (int rr)
{ //
set_point ( O , Point { center ( ) . x-rr , center ( ) . y-rr} ) ;
r=rr ;

private :
int r ;
};
Circle :
Circle cl { Point { l00 , 200 } , 50 } ;
Circle c2 { Point{ 150 , 200 } , 100 } ;
Circle c3 { Point{ 200 , 20 0 } , 150 } ;

,
.

Circle ,
. ,
. .
. FLTK
. ,
(. )
. .
Circle : : Circle ( Point , int rr) //
: r (rr)
1 .1 . Ellipse 525

add (Point { p . x-r , p . y-r } } ; //

Point Circle : : center ( ) const

return {point ( O ) . x+r , point ( O ) . y+r } ;

void Circle : : draw_lines ( ) const

if (color ( ) . visibility ( ) )
fl_arc (point ( O ) . x , point ( O ) . y , r+r , r+r , 0 , 360) ;

fl arc () . _

. .
- ,
,
. ,
360 , fl arc () _

( ): . . 1 .

1 3. 1 3. Ellipse
,
. . . ,
.
.
struct Ellipse : Shape {
11 ,
Ellipse (Point , int w, int h) ;

void draw_lines ( ) const;

Point center ( ) const;


Point focusl ( ) const;
Point focus2 ( ) const;

void set_major (int ww)


{ //
set_Point ( O , Point{ center ( ) . x-ww , center ( ) . y-h } ;
w=ww ;

int major ( ) const { return w; }

void set_inor (int hh}


{ //
set_Point ( O , Point ( center ( ) . x-w , center ( ) . y-hh } ) ;
526 1

h=hh ;

int llli. nor ( ) const { return h ; }


private :
int w;
int h;
1;

Ellipse :
Ellipse el { Point { 200 , 200 } , 50 , 50 } ;
Ellipse e2 { Point { 200 , 200 } , 100 , 50 } ;
Ellipse e3 { Point { 200 , 200 } , 100 , 150 } ;

.

E l l ipse, maj or ( ) = =
minor ( ) , .

. Ellipse,
:
Point Ellipse : : focusl ( ) const
{
if (h<=w) // :
return { center ( ) . x+int ( sqrt (doule (w*w-h*h) ) ) ,
center ( ) . } ;
else // :
return { center ( ) . ,
center ( ) . y+int ( sqrt (doule (h*h-w*w) ) ) } ;
1 3.14. Marked _polyline 527

Circle Ellipse?

. . ,
- , .
, Circle Ellipse.

( ;
).
. , ,
Circle Ellipse, ,
, -
set maj or ( ) set minor ( ) . ,
_ _

( ),
set major ( ) major ( ) ! =minor ( ) , -
_ ,

, .
, (
major ( ) ! =minor ( ) ), ( major ( ) -inor ( ) ).
( Ellipse), .
, Circle
.
, :
. ,
, -
, -
.
, . -
. , -
. ,
,
. ,
.

1 . 1 4. Marked_polyline
.
, ,
. arked_polyline.
.
arkedyolyline mpl { " 1234 " } ;
mpl . add ( Point { l00 , 100 } ) ;
mpl . add ( Point { lS0 , 200 } } ;
mpl . add ( Point { 250 , 250 } ) ;
mpl . add ( Point { 00 , 2 00 } ) ;
528 1 3

marked polyline [QI

arked_yolyline :
struct &rked_yolyline : Open_yolyline {
arked_yolyline (const strinq& m) : mark {m)
{
if (111- "") mark = " * " ;
-=

arked_yolyline ( const strinq& m,


initializer list<Point> lst) ;
void draw_lines ( ) const;
private :
strinq mark ;
};
Open_yolyline,
" Point, .
, - . , draw lines ()
:
void &rked_yolyline : : draw_lines ( ) const
{
Open_yolyline : : draw_lines ( ) ;
for ( int i=O ; i<numer_of_;ioints ( ) ; ++i )
draw_mark (point ( i ) , mark [i%mark . size ( ) ] ) ;

Open_yolyline : : draw_lines () ,
.
1 3. 1 5. Marks 529

, : mark
[ i%mark . size ( ) ] ,
, ,
arked_polyline. % (
). draw lines ( ) _

draw mark ( ) _ .

void draw_ark ( Point , char )


{
constexpr int dx = 4 ;
constexpr int dy = 4 ;
strinq m{ 1 , } ; //
fl_draw (m. c_str ( ) , y . x-dx , y . y+dy) ;

dx dy
. m .
, ,
Open_polyline:
arked_polyline (const strinq& m, initializer list<Point> lst)
: Open_polyline { lst} , ark {m}

if (m==" " ) mark = "*" ;

.
, draw lines ()
_

.
. .

arked_polyline mpl { " 1234" , { { 100 , 100 1 , { 150 , 200 1 ,
{ 250 , 250 } , { 300 , 200 } } } ;

1 .1 5. Marks
.
arks. . ,
. .
arks { "" , { { 100 , 100 1 , { 150 , 200 1 , { 250 , 250 1 , { 300 , 200 1 1 1 ;
:
, arks
,
. (, ).
.
530 1

arks - arked_polyline
.
struct &rks : &rked_polyline {
arks ( const strinq& m) : &rked_polyline (m}
{
set_color (Color{ Color : : invisile } ) ;

arked_polyline ( const strinq& m, initializer list<Point> lst)


: &rked_polyline {m, lst}

set_color (Color { Color : : invisile } ) ;

};
: arked polyline { m }
arked_polyline arks.
, (9.4.4).

1 3. 1 6. Mark
Point Window.
-, .
,
, , 1 3. 2 ,
arks. "",
arks, .
, ,
1 3. 1 2, :
1 3. 1 6. Mark 531

ark ml { Point { l00 , 200 } , ' x ' } ;


ark m2 { Point { lS0 , 200 } , ' y ' } ;
&rk m. { Point { 200 , 200 } , ' z ' } ;
cl . set_color (Color : : ue) ;
c2 . set_color (Color : : red) ;
c . set_color (Color : : qreen) ;

. .

ark arks,
-

.
struct ark : arks {
ark ( Point , char ) : arks { strinq { l , c } }
{
add (xy) ;

1;
string { 1 , } string,
, .
ark arks
, .
, ?
" "?
. .
, ,
.
?
, .
5 32 1 3

.
, , , + * , .

1 . 1 7. Image
.
, . ,

. ,
(rita_path . gif), ".
.

Hurricane Rita
Sep tember 21 , 2005
4 CDT Wednesday
NWS TPCINatlonal Hurrlcane Center
Advlsory 1 7
Current Cent.er Locatlon 24.4 N . W
Sustalned Wlnd 165 mph
Current Movement W at 1 3 mph
Current Center Locatlon
Forecast Center Posltlons
Sustalned wlnd > 73 mph
D Sustalned wind < mph
:::::.,. Potential Day 1 - Track Area
Potentlal Day 4-5 Track Area
Hurrlcane Watch
Troplcal Storm Watch


, (ri ta . jpg).
Iage rita { Point{ 0 , 0 } , "rita . jpq" } ;
Image path { Point { 0 , 0 } , "rita_yath . gif" } ;
path . set_mask ( Point{ S0 , 250 } , 600 , 400) ; 11
1 .1 7. lmage 533

win . attach (path) ;


win . attach (rita) ;

set_mask ( ) ,
.
600400 ri ta_path . gif ( path)
,
(50,250) . - ,
.
, ,
. path
"", , rita.
.
: JPEG GIF.
enwn class Suffix
enwn Encodinq ( none , jpq , qif } ;
};

Image.
struct Imaqe : Shape (
Imaqe (Point , strinq file_name , Suffix Suffix : : none) ;
Imaqe ( ) ( delete ; }
void draw_lines ( ) const ;
void set_mask ( Point , int ww , int hh)
( w-ww ; hhh ; . ; = . ; }
private :
int w , h ; // " "
534 1 3

11 (, )
int , ;
Fl_Ima.qe* ;
Text fn ;
};
Image
, ,
( ) .
(, ,
), Image Bad image.
Bad_image :
struct Bad_ima.qe : Fl_Ima.qe
Bad_ima.qe ( int h , int w) : Fl_Ima.qe { h , w , 0 } { )
void draw ( int , int , int,
int , int , int) { draw_epty (x,y) ; }
};
,
Image
.
// , ,
11 ,
Ima.qe : : Imaqe ( Point , strinq , Suffix )
: w { O } , h { O } , fn { xy , " " }

add (xy) ;
if ( ! can_open ( s ) ) { // s ?
fn . set_lael ( " :w \ " "++ ' " ' ) ;
= new Bad_ima.qe ( 0 , 20 ) ; 11 " "
return ;

if ( == Suffix : : none) = qet_encodinq ( s ) ;

switch (e) { //
case Suffix : : jpq :
= new Fl_JPEG_Ima.qe { s . c_str ( ) } ;
break;
case Suffix : : qif :
= new Fl GIF_Ima.qe { s . c_s tr ( ) } ;
break ;
default : //
fn . st_ll ( " n '1'II \ 11 11 +s+ 1 11 1 ) ;
= new Bad_imaqe { 0 , 20 } ; // " "
1 3. 1 7 . lmage 535

I ,
, (Fl_JPEG_Image Fl_GIF_
Image) . new
. ( new
1 7)
FLTK . FLTK
, s . _s tr ( )
s .
can_open ( ) , .
.
bool can_open ( const strinq& s)
11 , s
11

ifstream ff ( s . c_str ( ) ) ;
return ff ;

. -
. .
, ,
.
, get_
encoding ( ) :
.
map ( 2 1 .6).

1 . Simple_window 800 1 .
2 . 88 800800
( 1 1 00
).
3. , ,
, ( Rectangle) .
4. 200200 ( JPEG
GIF) (
). ,
200 , ,
set mask ( ) .
. .
536 1

5. 1 1 00 .
. Next.
wai t for button ( ) .
. .


1 . -
?
2 .
,
?
3.
?
4. ?
5. Line -
?
6. Point?
7. Line_s tyle.
8. Color.
9. RGB?
1 0. Line
Lines. ?
1 1 . Shape?
1 2 . Closed_polyline
Point?
1 3 . , Shape.
Window?
1 4 . Rectangle Polygon
Point ()?
1 5. Polygon Closed
polyline?
1 6. :
?
1 7. Triangle (
Rectangle)?
1 8. Shape ?
1 9 . Shape ?
1 3. 1 7. lmage 537

20. Text?
2 1 . ?
22. Vector_ref ?
23. Circle Ellipse?
24. , Image
, ,
?
25. ?

GIF
JPEG
Vector ref



n11
. ,
,
.
1 . Arc, . : fl_arc ( ) .

2. . ,
.
3. Arrow, .
4. n ( ) , s ( ) , ( ) , w ( ) , center ( ) , ne ( ) , se ( ) , sw ( )
nw ( ) . Rectangle
Point. "
",
. , nw (r) - (-)
Rectangle r.
5. . 4 Circle Ellipse.
.
.
6. . ,
1 2.6. .
538 1

.
.
7. RGB ( ).
8. Reqular_hexagon ( ).

.
9. Regular_hexagon
( ).
1 0. Regular_polygon.
. ( )
.
1 1 . 300200 .
400 300 ,
. . .
.
.
1 2. .
( .
Next) .
1 3. 1 3. 1 , .
.
1 4. . -
.
1 5. .
1 6. .
1 7.
.
1 8. Poly, .
. .
: .
1 9. Star.
.
. .

1 2 , .
" " :
.


l, 1
- (V)

, ,
: ,
,


. ,

.
,
,
-
: ,
.
,
,

. ,
"
"
.
540 14

1 4. 1 . 14.3.
1 4. 1 . 1 . 1 4.3. 1 .
1 4. 1 .2 . 1 4..2. -
1 4 . 1 ..
1 4 . 1 .4. 1 4.3.3.
1 4 .2 . Shape 1 4.3.4.
1 4 .2 . 1 . 1 4..5.
1 4 .2.2. 1 4.4. -
1 4 .2..
1 4.2.4.

1 4. 1 .

? .
" " ,
?

1 4.1 . 1 .
- .

. -
, , ,
. ,
.
-
.
, ,
. .
Window - , .
Line - , .
int - .
Color - .
Shape -
.
, Shape. ,
, .. .
; ,
14.1. 541

. :
Shape .

;
.
.
,
.
, .
,
. ,
,
. ,
.

. , .

. , ,
,
?
? ?
? ?
.
? " !"
,
.
.
.
.
.
.
. . . .

.
.
,
.
, ,
.
,
,
. ,
. ,
542 1 4

FLTK, ,
( r ) . ,
, .
1 7 1 8 .
,
.

. .
, Open_polyline, Closed_polyline, Poly
gon, Rectangle, arked_polyline, arks ark
( Polyline");
.
,
. .
Shape. .

. .
", ,
, .

1 .1 .2.
.
- , ,
. ,
, ,
.
.
. , ,
, , -
. . ,
. :
.
Point.
Line ln ( Point { l00 , 200 } , Point{ 00 , 40 0 } ) ;
ark ( Point { lOO , 200 } , ' ' ) ; 11 " "
Circle c (Point { 200 , 200 } , 250) ;

. , Point.
. . ,
, . .
void draw_line ( Point pl , Point 2) ; 11 pl 2 ( )
void draw_line (int xl , int yl , int 2 , int 2 ) ; 11 (x l , yl ) (2 , 2)
14.1. 543

,
,
.
Point
: . .
draw_rectangle ( Point { l00 , 200 } , 300 , 400) ; //
draw_rectangle ( 100 , 200 , 300 , 400 ) ; //


, . .
? , ( 1 00,200)
(300,400)? , ( 1 00,200),
300 400? ,
( )? Point,
.
, ,
(. ,
).

.
.
, , ,
, add ( ) , ,
, draw_lines ( ) .
( ), ,
( ).
, ,
.
(generic); 1 9-2 1 .

1 4. 1 . .
. ,
, "
(attach) Shape Window, -
(add) Line Shape?"
" -",
? . .
.
Open__polyline opl ;
opl . add (Point { l00 , 100 } ) ;
opl . add ( Point { lS0 , 20 0 } ) ;
opl . add ( Point { 250 , 250 } ) ;
544 14

opl . opl ,
add ( ) ;
. .
. ,
:
win . attach ( opl) ;
win opl;
win opl , .
opl , win
. , win opl,
. opl,
win opl
. attach ( )
add ( ) .

Window:

Open_polyline:
( 1 00, 1 00)
( 1 50,200)
(250,250)

add ( )
(), attach ( ) -
( ).
Window,
,
add ( ) , t tach ( ) .
Window. .
, , ,
, .
void f ( Simple_window& w)
{
Rectanqle r { Point{ l00 , 200 } , 50 , 30 } ;
w . attach (r) ;
11 r !

int main ( )
{
Simple_window win { Point{ l00 , 100 } , 600 , 400 , "Moe J" } ;
1 4. 1 . 545

11 . . .
f (win) ; // !
11 . . .
win . wait_for_button ( ) ;

f ( ) wait_for_
button ( ) , r win , win
. 1 7
, ,
, ,
" " wai t_for_button ( ) .
Vector_ref,
1 3 . 1 0 .4.
, f ( ) .
Window (
8 . 5 . 6). :
attach (r) const Window,
attach ( )
Window, r.

1 4. 1 .4.
. ,
, : " ?" "
?" ,
.
puic private,
,
protected. .
- , , lael string;
, ,
. . ,
, , lael,
, . .
struct Circle
11 . . .
private :
int r ; //
1;

Circle c { Point{ l00 , 200 } , 50 } ;


.r = - 9; // OI\ ? - :

// Ci rcl e : : r
546 14

1 3,
- .
"" ,
Circle.
,
.
, ,
"" ,
, .
(.. Window)
.
,
, ,
.

1 4.2. Shape
Shape ,
Window .
,
Window, , .
.
, ,
. Line_style Color
( , ).
Point
, .
, ,
, , .
, .
, .
class Shape { // ,
pulic :
void draw ( ) const ; //
virtual void move (int dx , int dy) ; //
// +=dx +=dy

void set_color ( Color col) ;


Color color ( ) const ;
void set_style (Line_style sty) ;
Line_style style ( ) const;
1 4.2. Shape 547

void set_fill_color (Color col ) ;


Color fill_color ( ) const ;

Point point ( int i ) const ; 11


int nuer_of_yoints ( ) const ; 11

Shape ( const Shape&) = delete ; 11


Shape& operator= ( const Shape&) 11
delete ;

virtual shape ( )
protected :
Shape { } ;
Shape (initializer_list<Point> lst) ; //
virtual void draw_lines ( ) const ; //
void add ( Point ) ; //
void set_yoint (int i , Point ) ; // poin t s [ i } =p ;
private :
vector<Point> points ; 11
11
Color lcolor { fl_color ( ) } ; 11
// ( )
Line_style ls { O } ;
Color fcolor { Color : : invisile } ; //

};

, II
r
. - .
, ,
.
.

1 4.2. 1 .
Shape:
protected :
Shape { } ;
Shape (initializer_list<Point> lst) ; //

protected. ,
,
Shape ( : Shape). , Shape
II ,
Line Open_polyline. protected : ,
-

Shape :
Shape ss ; // : Shape
548 14

Shape ,
.
, -
Shape , , ,
,
. ,
Shape,
, ,
, Circle Closed_
polyline. ! ?
- : "
?" , Shape,
. ,
.
Shape
.

.
FLT. .
FLTK, .
Shape, Color Line_style.
vector<Points> .

, add ( )
Shape:
Shape : : Shape (initializer_list<Point> lst)
{
for ( Point : lst) add (p) ;

(abstract),
. -
- -
W1. (pure vlrtual functlon),
1 4. 3 . 5 . ,
,
(concrete). ,
.
, .
-
. ?
? :
-

. "Canon EOS 6000" -


1 4.2. Shape 549

,
(
). , - () ,
canon EOS 6000" - () ,
( ) - .

virtual shape ( ) { )
.
, 1 7. 5 . 2 .

1 4.2.2.
Shape - .
private :
vector<Point> points ; 11
11
Color lcolor{ fl_color ( ) } ; 11
11 ( )
Line_style l s { O } ;
Color fcolor {Color : : invisile } ; //

-
, . ,
,
.
.
- Shape ,
.
. -
, : ,
, ( ) set_X ( )
, :
void Shape : : set_color ( Color col )
{
lcolor = col ;

Color Shape : : color ( ) const

return lcolor ;

,
. . .
,
550 14 r

.
- . ,
const. ,
Shape (. 9.7.4).
Shape points Point.
.
Point points add ( ) :
void Shape : : add ( Point ) //
{
points . push_back (p) ;

, points .
Shape ,
- - , Shape -
-.
,
, - -
. .

.
, Shape, Circle Polygon,
", . Shape " ",
. ,
, . .
Circle Rectangle
. .
? (. 1 2.7.6.)
Lines (
; . 1 3.3).
Open_Polyline arks
.
Polygon -
add ( ) , ( 1 3.8).
add ( )
protected (..
), ,
. add ( )
puic (.. ) private (..
Shape).

.
1 4.2. Shape 551

set_
point ( ) . "",
, . ,
Regular_hexagon,
. .
. ,
,
. set_Point ( )
, ,
Shape.
. Mutae_rectangle,
Rectangle ,
.
points private,
. ,
.
void Shape : : set_.Point ( int i , Point ) //
{
points [ i ] = ;

Point Shape : : point (int i) const

return points [ i ] ;

int Shape : : numer_of_.Points ( ) const

return points . size ( ) ;

-
:
void Lines : : draw_lines ( ) const
11 ,

for (int i=l ; i<numer_of_.Points ( ) ; i+=2)


fl_line (point ( i-1) . x , point ( i-1) . y , point ( i ) . x ,point (i ) . ) ;


. ?
? ? ,
(inline).
numer_of_Points ( )
. points .
size ( ) .
552 1 r

, , .
Shape:
struct Shape 11
// ( , )
Shape ( ) ;
Shape (initializer_list<Point>) ;
void draw ( ) const ; /!
virtual void draw_lines ( ) const ; 11
virtual void move ( int dx , int dy) ; 11
virtual shape ( ) ;

vector<Point> points ; //
Color lcolor ;
Line_style ls ;
Color fcolor ;
};

- (pri va te :
protected : )? I ,

.
( . 9.4.3).
, Shape.
Shape :
Fl_Color lcolor ;
int line_style ;
, ( ,
int,
, Fl_Color )
.
,

( lcolor
line_style) .
,
. , s . add ()
, s . points . push_back () .

1 4.2..
, Shape.
void draw ( ) const ; //
// dra w_ l i nes
virtual void draw_lines ( ) const; //
1 4.2. Shape 553

Shape - .
Shape
, (.
1 4.4) : - Shape.
FLTK .
.
draw ( ) ,
draw_lines ( ) .
draw_lines ( ) .

draw ( ) .
FLTK. ,
draw_lines ( ) . ,
. .
void Shape : : draw ( ) const
{
Fl_Color oldc = fl_color ( ) ;
//
// }'I!
fl_color (lcolor . as_int ( ) ) ; //
fl_line_style ( ls . style ( ) , ls . width ( ) } ; //
draw_lines ( ) ;
fl_color (oldc) ; 11
fl_line_style ( O ) ; 11

, FLTK
.
. ,
, .
.
.
, Shape : : draw ( )
.
draw_lines ( ) , ,
. ,
draw_lines ( ) .
.
, draw_
lines ( ) . , ,
- Shape , -
.
Shape .
vector<Point>. -
554 1 4

. , (..
) ,
- .

. : ,
Shape, .
. Text, Rectangle Circle
, . .
. ,
"", . .
Circle , , ,
. Circle
, , .
, - .
Circle draw_lines ( ) ,
draw_lines ( )
Shape. virtual
Shape : : draw_lines ( ) :
struct Shape (
11 . . .
virtual void draw_lines ( ) const ;
11
11 dra w l i nes ()
11 . . .
};

struct Circle : Shape


11 . . .
void draw_lines ( ) const ; // " " Shape : : dra w_ l ines ()
11 . . .
};
. draw_lines ( ) Shape
- Circle,
Circle, - Rectangle,
Rectangle. virtual
draw_lines ( ) :
Shape, draw_
lines ( ) ( , draw_lines ( ) Shape) ,
draw_lines ( ) Shape.
1 3 , Text, Circle, Closed_polyline
.. ,
, (overriding).
, ,
Shape, draw_lines ( ) protected. ,
1 4.2. Shape 555

" " -
draw ( ) , - " ,
draw ( ) , Shape.
,
1 2. 2 . , , Window.
Window Shape - draw ( ) .
draw ( ) draw_lines ( )
. gui_main ( )
.

Shape
draw_lines() draw()
Clrcle
draw_llnes() draw()
Window

draw()
Shape attach()
draw_lines() gui_main()
Square
draw_llnes()

gui_main ( ) ?
; wait_for_button ( ) ,
.
move () Shape ;
.
void Shape : : move ( int dx, int dy) // +=dx + =dy
(
for (int i = ; i<points . size ( ) ; ++i ) {
points [ i] . x+=dx ;
points [ i] . y+=dy ;

draw_lines ( ) , move ( ) ,
.
Shape.
Axis (. 1 2. 7.3 1 5.4).
move () Shape;

556 1 4

. . .
Shape, move ( ) .

1 &.2.4.
Shape
:
Shape (const Shape&) =delete ; //
Shape& operator= ( const Shape& ) =delete ;

,
:
void my_fct (Open_polyline& , const Circle& )
{
Open_polyline 2 = ; // :
11
vector<Shape> v ;
v .push_back (c) ; 11 :
11
// . . .
= 2 ; 11 :
11 Shape

!
push_back ( ) ;
( push_back ( )
). ?
,
. -
my_fct ( ) . Circle
v, Shape
; Circle ,
Shape , sizeof ( Shape) < si zeof (Circle) .
-

v . push_back () , Circle ""


. v
; Circle ( r),
.

Shape: polnts Circle: polnts


color color
ls ls
fcolor fcolor
1 4.3. 557

2
:
&rked_Polyline mp { "" 1 ;
Circle ( , 10) ;
my_fct (mp , c) ; 11 Open_polyl ine
11 Ma rked_polyl ine

Open_polyline
mark, string.
.

. , -

=delete, Shape.
( ) - ,
.
, .
, Shape
, . Shape
Window, . , Window
Shape,
. , ,
,
. .
,
,
. .
clone ( ) . , clone ( )
,
, Shape.

1 4..

; , ( )
, ,
.
,
.
. .
.
, Circle Shape;
. Circle Shape
558 14

Shape Circle.
( Circle)
( Shape) .
(inheritance),
.
(subclass), -
(su perclass).
. ++

,

. , Window
draw_lines ( ) Shape,
Circle, draw_lines ( )
Circle, draw_lines ( ) Shape.
11 (run-time
polymorphlsm), (dyamlc dispatch)
(run-tlme dispatch),

, .
. .
,
, .
(encapsulation).
, -
-
(object-oiiented programming). , ++
-
. , 20
2 1 , ++
. ++ - -
Simula67, ,
- (
22).
! ?
?
,
.
.

.
.
14.3. 559

Open_polyline
. ,
(Shape). .

.

Rectangle
Marked_polyline Closed_polyllne

Marks Polygon

Mark

1 . . 1 .
?
9.4. 1 . : -
. ,
- -
, :
Shape: Circle:
points points
lcolor lcolor
ls ls
fcolor fcolor

Circle - Shape (
, Shape)
Shape. , Circle
r,
-.
560 14

,
" Shape:
,
draw lines ( ) Shape.
. vt (
), vptr (
, ).
1 7 1 8; .

.
. :

draw_lines ( ) - .
vt,
move ( ) .
; vt (
). .
. draw_lines ( ) , ,
draw_lines ( ) vt, .
. .
Circle, Circle : : draw_lines ( ) .
. , Open_polyline,
vt . Shape,
Shape : : draw_lines ( ) . ,
Circle move ( ) .
x . move ( ) . Circle,
Shape : : move ( ) . .
, vptr
vt
.
. - .
1 4.3. 561

Shape .
Shape , Open_polyline
. " Shape",
- .
vt
. , vt
.
,
. ,
-
.
. ,
(, Circle : : draw_
lines ( ) ),
vt ,
(overridlng). , Circl e : : draw_lines ( )
Shape : : draw_ lines ( ) .
vt ?
. -
? . (
) , .
- , . ,
, "
". ? ?
? ?
, .
(
).
,
.
. .

1 ,.3.2.
, ,
:
struct Circle : Shape { /* . . . */ } ;

,
s truct, (. 9.3),
. -
:
class Circle : puic Shape { pulic : / * . . . */ } ;
562 1 4 r

Circle ,
,
. , ,
. .
puic,
. .
class Circle : Shape { puic : /* . . . */ } ; // ,
Shape
Circle, - Shape
Circle. .
.
, .

virtual ,
, virtual .
struct Shape {
11 . . .
virtual void draw_lines ( ) const ;
virtual void move ( ) ;
11 . . .
};

virtual void Shape : : draw_lines ( ) const { / * . . . */ } //


void Shape : : move ( ) { / * . . . * / } // OI-i

1 4.. .
,
,
, :
struct Circle : Shape {
void draw_lines ( int) const ; 11 ,
11 ( i n t ?)
void drawlines ( ) cons t ; 11 ,
11 ( ? )

void draw_lines ( ) ; 11 , ( cons t ?)


11 . . .
};

,
Shape : : draw_lines ( ) ( ),
.
. ,
.
1 4.. 563

draw_lines ( ) ,
,
.
struct (
virtual void f ( ) const ( cout << 11 : : f 11 ; }
void q ( ) const ( cout << 11 : : q 11 ; } //
};

struct D :
void f ( ) const ( cout << 11D : : f 11 ; } // B : : f
void q ( ) ( cout << 11D : : g 11 ; }
};

struct DD : D (
void f ( ) ( cout << 11 DD : : f 11 ; } 11 D : : f
11 ( con s t )
void q ( ) const ( cout << 11DD : : g 11 ; }
};


f ( ) . .
, f ( )
q ( ) , , ,
( , ).
void call ( const & )
11 D , ca l l ()
-

11 D. DD - -

11 D, D ,
-

// ca l l () DD

b.f() ;
b.q() ;

int main ( )

;
D d;
DD dd;

call (b) ;
call (d) ;
call (dd) ;

b.f() ;
b.q() ;

d. f ( ) ;
d.g() ;
564 14 r

dd . f ( ) ;
dd . g ( ) ;

:
: : f : : g D : : f : : g D : : f : : g : : f : : g D : : f D : : g DD : : f DD : : g
, , ,
.
, ,

. , .
, .
, ,
override.
struct {
virtual void f ( ) const { cout << 11 : : f 11 ;
void g ( ) const { cout << ": : g 11 ; } //
};
struct D :
void f ( ) const override 11 B : : f
{ cout << "D : : f 11 ;
void g ( ) override // :
1 cout << "D : : g 11 ; 11 B : : g
};
struct DD : D {
void f ( ) override // : D : : f
{ cout << "DD : : f 11 ; 11 ( con s t)
void g ( ) const override 11 :
{ cout << "DD : : g " ; // D : : g
};

override
, .

1 4..4.
++ .
.
(private) .
priva te, r
.
(protected).
protected, r
, .
1 4.. 565

. (pu\ic).
puic, .
.

.- - - - - - - - - - - - - - - - - - - - - --------- ---------- ---- ---- -


: :
1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - - - ;

private, protected
puic.
D private,
, puic protected,
D.
D protected,
, puic protected,
D , D.
D puic, ,
puic, .

.
. ,
++
++, ISO ++.
(..
); (
, , , )
.

1 4 . .5.
- ,
.
; .
, -
566 14

.
( rl. , " .)
.
.
"" ( . ),
" " (
) "" (
) .
( ).
1 4.2 . 1 , ,
protected. ,
-
: ,
. .
class ( 11
pulic :
virtual void f ( ) = ; //
virtual void 9 ( ) = ;
);

; 11 : -

= ,
: : f ( ) : : g ( ) "", ..
.
, .
"":
class Dl : pulic (
pulic :
void f ( ) override ;
void 9 ( ) override ;
);

Dl dl ; 11

, ,
:
class D2 : pulic (
pulic :
void f ( ) override ;
11 g ()
};

D2 d2 ; // : D2 - ( )

class D pulic D2 (
1 4.4. - 567

pulic :
void g ( ) override ;
) ;

D d ; 11 ok


; . , ,
- ( ),
, ( -
, ) .

1 4.4. -
, Circle
Shape Circle
Shape. ,
( ).
11 . .
Shape ( , ),
Circle (
Shape) .
11 u11. Circle
-. (,
-). Shape.
, (.. ,

),
. , Never_do_this,
Shape .
Shape : : draw lines ( ) ,
. 1 00 . "
" , , ,
Never_do_this Shape,
(.. ),
Shape. !
,
,
(""; -
Shape) ("";
- . Shape).
568 14


,
(. Circle). -
, (
- Shape).
,
: " "
Shape : : draw ( ) , . .
draw_lines ( ) Shape,
. " ", Shape
, . , FLTK

!
attach ( ) .
Window Shape (
Window : : attach ( ) Shape&; . .3).
, Shape ,
Shape ,
.
, .
. - " J" .
: -
. , .
.
(. Shape
),
(. 1 7- 1 9 vector;
).
- .
.
.
. Shape.
, .
-
,
Shape
-
.
.
,
(. 1 4.3.5).
1 4.4. - 569

, ,
,
. -
.
1 . 81 vf ( )
f ( ) . 81 .
, (,
"81 : : vf ( ) ) . . 81
"

.
2. D l , 8 1 ,
vf () . D l
vf ( ) f ( ) .
3. 81 (.. 81 &)
D l . vf ( )
f ( ) .
4. f ( ) Dl . 1-3 .
.
5. 81 pvf ( )
. 1 -4. .
6. D2, Dl,
pvf ( ) . D2
f ( ) , vf ( ) pvf ( ) .
7. 82 pvf ( ) .
D2 1 - string -,
pvf ( ) ; D21 : : pvf ( )
string. D22,
D21, ,
int. f ( ) 82 &
,

pvf ( ) .
f ( ) D21 D22.


1 . ?
2. ?
3. ?
4. Shape?
5 70 14

5. , -
?
6. ?
7. ?
8. private?
9. -
?
1 0. ?
1 1 . ?
1 2 . ?
1 3 . , ?
1 4. ?
1 5.
?
1 6 . ?
1 7.
?
1 8. - ?
1 9. - ?
20. ?
2 1 .
?
22. - ?

11
private
protected
puio

-



1 . , Smiley () Frowny (),
Circle .
1 4.4. - 571

, Smiley Frowny,
.
2. Shape. ?
3.
. ?
4. Immoile_Circle,
Circle, .
5. Striped_rectangle,

(, ).
,
.
6. Striped_circle, Stri
ped_rectangle.
7. Striped_closed__polyline,
Striped_rectangle ( ).
8. Octagon, .
. -
( . Shape) .
9. Group, Shape
Group.
: Vector_ref. Group, ,
,
.
1 0 . Pseudo_window, Window.
.
, . ,
- ,
. .
( ),
Simple_window.
1 1 . Binary_ tree, Shape.
, (levels==O ,
, levels==l ,
, levels==2 , ,
levels= ,
, , . , ..).
.
( ).
5 72 14

P.S.
(, , ).
1 2. Binary tree ,
. Binary_
tree ,
. (. ).
1 . Binary tree ,
( ). ,
(, , ,
, ). ,
,
.
1 4. Binary_tree , .
,
Binary_tree. ; .
, , ,
" lrrlr" (
"1", "r") .
1 5. .
I terator, next ( ) .
doue* (. 1 7) .
I tera tor Vector-i tera tor Li s t-i tera tor .
next ( ) Vector_i terator
vector<doule>. List_
i terator list<doule>.
Vector_iterator vector<doule>,
next ( )
. .
. .
void print ( I terator&) .
vector<doule> list<doule>.
1 6 . Controller,
: on ( ) , off ( ) , set_level ( int) show ( ) .
Controller .
, show ( )
, ,
.
Shape; ""
.
Controller.
14.4. - 573

1 7. . ++,
exception, runtime_error out_of_range (. 5.6.3),
(
what ( ) . ,
).
++
.



. . - ,
,
, (
)
.
, . . , -
. ,
( private protected),
( ), -
(
)
.

-
- 4./1 (V)

,

, .

. ,

.

,
.
576 1 5

1 5 . 1 . 1 5 .5 .
1 5 .2. 1 5 .6.
1 5.. Function
1 5 . . 1 . r 1 5 .6. 1 .
1 5 . .2. r 1 5 .6.2.
1 5... - 1 5.6..
1 5.4. 1 5.6.4. r

1 5. 1 .
.
,
, -
. - .
,
. .
.
. ,
.
-
, .

1 5. 2.
,
. .
.
, .
, ,
- ,

doue one (doule) { return 1 ; }

. :
1.
, .
, one ( ) .
. 1 ; .
( , ) = ( , 1 ) .
15.2. 577

,
, - ,
.
doule slope (doule ) { return /2 ;

. .
, /2. . ( , ) = ( , /2 ) .
( 2 , 1) .
- .
,
.
doule square (doule ) { return ; )

( ),
, .
(0 , ) , . . ( , ) == ( , ) . ,
,
, - ( 0 , 0) .
,
.
constexpr int xmax = 600 ; //
constexpr int = 400 ;

constexpr int x_oriq = xmax/2 ; // ( 0 , 0)


constexpr int y_oriq = ymax/2 ;
constexpr Point oriq{x_oriq , y_oriq} ;
5 78 15

constexpr int r min -10 ; 11 [ -1 0 , 1 1 )


constexpr int r = 11 ;

constexpr int n_.Points = 400 ; 11

constexpr int scale 30 ; 11


constexpr int y_scale 30 ;

Simple_window win { Point { l00 , 100 } , xmax , ymax , "Function graphing" } ;

Function s { one , r_min , r_max , orig , n_.Points , x_scale , y_scale} ;


Function s2 { slope , r_lllin , r_max , orig , n_.POints , x_scale ,y_scale } ;
Function s 3 { square , r_min , r_max , orig, n_.POints , x_scale ,y_scale} ;

win . attach ( s) ;
win . attach (s2 ) ;
win . attach ( s3) ;
win . wait_for_button ( ) ;

,
" ". ,
,
, .
.
Function: s, s2 s.
Function s { one , r_min , r_max , orig , n_.Points , x_scale , y_scale } ;
Function s2 { slope , r_min , r_max , orig , n_.Points , x_scale , y_scale } ;
Function s 3 { square , r_min , r_max , orig , n_.Points , x_scale , y_scale } ;
Function ,
( doule,
doule) .
( ).
( orig) Function,
( , ) .
,
, .
,
.
.
, 1 5.3. .

. ,
, . ,
, .
.
Text (. 1 3. 1 1 ) .
1 5.2. 579

Text ts { Point { lOO , y_oriq-40 } , "one" } ;


Text ts2 { Point { lOO , y_oriq+y_oriq/2-20 } , "x/2 " } ;
Text ts { Point{x_oriq-100 , 20 } , "x*x" } ;
win . set_lael ( " Function qraphinq : lael functions " } ;
win . wait_for_button ( ) ;


, ,
Next.
.
, /2
( 0 , 0 ) ( 0 . 5 , . 25) , one
/2 ( 2 , 1 ) , ;
, .
Axis
( 15 .4).
constexpr int xlenqth xmax-4 0 ; 11
constexpr int ylenqth ymax-40 ;

Axis {Axis : : x , Point { 20 , y_oriq} ,



xlenqth , xlenqth/x_scale , "one notch 1" } ;
Axis (Axis : : y , Point { x_oriq , ylenqth+20 } ,
ylenqth , ylenqth/y_scale , "one notch 1" } ;
580 15

xlength/x_scale ,
,
1 , 2, 3 .. ( , )
. , .
, , ( 1 5.6),
. , ,
.
x . set_color (Color : : red) ;
y . set_color (Color : : red) ;
, .
,
- ,
. ,
. ,
.
, .
-
(, ). :
" - ".

1 5.. Function
Function
.
1 5.3. Function 581

struct Function : Shape {


11
Function (Fct f , doue rl , dou.le r2 , Point oriq ,
int count = 100 , dou.le xscale 25 , dou.le yscale
25) ;
};

Function Shape,
Shape.
f,
count , [ r l , r2 ) .
Function : : Function (Fct f , dou.le rl , dou.le r2 , Point ,
int count , dou.le xscale , dou.le yscale)
11 f (x) [rl , r2) ,
11 coun t . (0, 0)
11 ;
11 xsca l e ;
11 ysca l e

i f (r2-rl<O) rrr ( " " ) ;


if (count <) error ( "He nn'81'l' count " ) ;
dou.le dist (r2-rl ) /count ;
dou.le r rl ;
for (int i ; i<count; ++i )
add ( Point {xy . x+int (r*xscale) , xy . y-int (f (r) *yscale) ) ) ;
r + dist ;
582 1 5

xscale yscale
. ,
.
, Function
, ,
,
.
. ( Shape)
. Function,
,
. (. . 2).
Fct,
-? s td : : function .
w" . Fct ,
.
doue.

1 5..1 .
xscale
yscale Function.
D (default arguments).
.
.
Function s { one , r_lllin , r_ax , orig , n_J1oints ,
x_scale , y_scale } ;
Function s2 { slope , r_lllin , r_ , orig , n_J1oints ,
x_scale } ; // ysca le
Function s { square , r_lllin , r_max , orig ,
n_J1oints } ; // xsca le , ysca l e
Function s4 { sqrt , r_llli n , r_ , orig } ; // coun t , xsca l e ,
1 1 ysca l e

:
Function s { one , r_llli n , r_max , orig , n_J10ints ,
x_scale , y_scale } ;
Function s2 { slope , r_lllin , r_ax , orig , n_J1oints , x_scale , 25 } ;
Function s { square , r_lllin , r_max , orig , n_J1oints , 25 , 25} ;
Function s4 { sqrt , r_llli n , r_. , orig , 100 , 25 , 25 } ;

, ,
.

.
1 5 .3. Function 583

struct Function : Shape { //


Function (Fct f , doue rl , doule r2 , Point oriq,
int count , doule xscale , doule yscale) ;
11 :
Function (Fct f , doule rl , doule r2 , Point oriq ,
int count , doule xscale) ;
// :
Function (Fct f , doule rl , doule r2 , Point oriq, int count) ;
// coun t ; :
Function (Fct f , doule rl , doule r2 , Point oriq) ;
) ;


.
, , , -
.
,
.
, :
struct Function : Shape {
Function (Fct f , doule rl , doule r2 , Point oriq ,
int count = 100 , doule xscale , doule yscale) ; 11
);

,
:
struct Function : Shape {
Function (Fct f , doule rl , doule r2 , Point oriq ,
int count = 100 , doule xscale=25 , doule yscale=25) ;
};

. .
,
- . , .
Function,
: . ,
,
, .

1 5..2.
- (cos)
( , ,
) /2 .
doule slopinq_cos (doule ) { return cos (x) +slope (x) ;

.
584 1 5


Function s4 { cos , r_ID.in , r_max , oriq , 400 , 30 , 30 } ;
s4 . set_color (Color : : ue} ;
Function sS { slopinq_cos , r_ID.in , r_max , oriq , 400 , 30 , 30 } ;
x . lael . move ( -160 , 0 ) ;
x . notches . set_color ( Color : : dark_red) ;
,
( ) .
, ,
.
Function fl { loq , 0 . 00000 1 , r_max , oriq , 200 , 30 , 30 } ; // l n ()
Function f2 { sin , r_ID.in , r_m.ax , oriq , 200 , 30 , 30 } ; // s i n ()
f2 . set_color (Color : : ue) ;
Function f3 { cos , r_ID.in , r_m.ax , oriq , 200 , 30 , 30 } ; 11 cos ()
Function f4 { exp , r_ID.in , r_m.ax , oriq , 200 , 30 , 30 } ; 11 ()

log ( 0 ) (
), log
. :
.
, cos ( ) . sin ( ) sqrt ( ) .
<cmath>
.
24.8 .9.2.
1 5.3. Function 585

1 5... -
,
- . ++
-, ,
. . sloping_cos
:
Function s5 { [ ] (doue ) { return cos (x) +slope (x) ; } ,
r_in , r_ax , oriq , 400 , 30 , 30 } ;
[ ] (doue ) { return cos () +slope ( ) ; }
-, . . ,
, . [ ]
- (lambda lntroducer) .
- , (
) ( ).
-.
doue,
-

cos () +slope ( ) . ,
:
Function sS { [ ] (doue ) -> doue { return cos (x) +slope (x) ; } ,
r_in , r_ax , oriq , 400 , 30 , 30 } ;
-
. , -
, .
586 15

- , ,
, , (
, ).
, .
-
- : .
1 5.5, 2 1 .4.3.

1 5.4.
, , Axis (
, 1 5.6.4),
. Axis ,
. Axis
( ) .
.
struct Axis : Shape {
enum Orientation { , , z } ;
Axis (Orientation d , Point , int lenqth ,
int numer_of_notches=O , strinq lael = " " ) ;

void draw_lines ( ) const override ;


void move (int dx , int dy) override ;
void set_color (Color ) ;

Text lael ;
Lines notches ;
};
lahel notches ,
. , ,
. label
move ( ) . Axis - ,
.
Axis ,
numer_of_notches .
Axis : : Axis (Orientation d , Point , int lenqth ,
int n , strinq l) : lael ( Point { 0 , 0 } , la)

if (lenqth<O ) rrr ( " " ) ;


switch (d) {
case Axis : : :

Shape : : add () ; //
Shape : : add (Point{ xy . x+lenqth , xy . y } ) ;
1 5.4. 587

if (O<n) //
int dist = lenqth/n ;
int = xy . x+dist;
for ( int i = ; i<n ; ++i) (
notches . add ( Point { x , xy . y } , Point { x , xy . y-5 } ) ;
+= dist ;

lael . move (length/3 , xy . y+20 ) ; 11


break ;

case Axis : : y :
{
Shape : : add () ; // ,
Shape : : add (Point ( xy . x , xy . y-length } ) ;
if (O<n) { //
int dist = length/n ;
int = xy . y-dist;
for ( int i = ; i<n ; ++i ) {
notches . add ( Point { xy . x , y } , Point{ xy . x+S , y} ) ;
-= dist ;

lael . move (xy . x-10 , xy . y-length-10) ; //


break ;

case Axis : : z :
error ( "Oc z " ) ;


, ,
, ,
. ,
Shape. Axis ( Shape : : add ( ) ),
(notches).
;
, .
. ,
, .

Orientation.
Axis ,
Axis , :
void Axis : : draw_lines ( ) const
{
Shape : : draw_lines ( ) ;
588 1 5

notches . draw ( ) ; //
11
lael . draw ( ) ; 11
//

notches label draw ( ) ,


draw_lines ( ) ,
, .
Axi s : : Shape , .
, ,
,
, :
void Axis : : set_color (Color )
{
Shape : : set_color (c) ;
notches . set_color (c) ;
lael . set_color (c) ;

Axis : : move ( )
Axis :
void Axis : : .ove ( int dx, int dy)
{
Shape : : .v (dx , dy) ;
notches . .ove (dx , dy) ;
lael . .ove (dx , dy) ;

1 5. 5 .
:
"" . -
( ),
.
, ,
, .


2 4
= I +x+ - + - + - + K
21 31 41
, < ;
, ,
.
1 5.5. 589

.
, , , . .
:
() = //
expl (x) = 1 //
2 () = l+x // ; pow (x , 1 ) /fa c (l ) ==x
() = l+x+pow ( x , 2 ) /fac (2)
4 () = l+x+pow ( x , 2 ) /fac (2) +pow ( x , 3 ) /fac ( 3 )
expS (x) l+x+pow ( x , 2 ) /fac (2) +pow (x , 3 ) /fac ( ) +pow (x , 4 ) /fac ( 4 )

, .
pow ( , n ) ,
-

". ,
, .
int fac (int n) // fa ctori a l (n) ; !

int r = 1 ;
while (n>l)
r*=n ;
--n ;

return r ;

fac ( ) . 1 .
fac ( ) , - :
doue term (doue , int n) // -
return pow (x , n) /fac ( n) ;

term ( ) , n
:
doule expe (doule , int n) //
{
doule swn = ;
for (int i=O ; i<n ; ++i) swn+=term(x, i) ;
return swn;

.
"" ( ( )
), ,
( ) :
Function real_exp { exp , r_min , r_max , oriq , 200 , x_scale , y_scale } ;
real_exp . set_color (Color : : ue) ;
590 1 5

( ) ?
, Function
, ( ) .
++, , ,
.
-:
for ( int n = ; n<SO ; ++n)
ostrinqstream ss ;
ss << " approxiation ; n==" << n
win . set_lael ( ss . str ( ) ) ;
11 :
Function { [n] (doue ) { return expe (x , n ) ; } ,
r_min , r_ax , oriq , 200 , x_scale , y_scale } ;
win . attach (e) ;
win . wait_for_button ( ) ;
win . detach (e) ;

- [ n ) , -
n. ,
Function ( , n ) n
Function.
detach () .
Function for.
, Function,
, .
Window ,
. detach () ,
Window .
, "
" .
, ( ) 1, "
" ( , 1) .
, ,
( () ==) .
-

Next, , .
, .
, .
1 5.i 591

one natc h. "" 1

one notr-h "" 1

expl () =1,
.
( , 1 ) ,
.
592 15

(l+x),
, r ( , 1 )
.

(l+x+pow (x , 2 ) /fac ( 2 ) ) .

,
, -3.
1 5.5. 593

.
, .
,
: ,
.

, -
.
, -
594 1 5

.
int
, doue
. ,
, , ,
,
doule,
. , fac ( ) ,
int. fac ( )
, doule, .
. 1 1 5
24.2.

, , , ,
.
,
, .

1 5.6.
.

-
.
,
.
.
.
.
2008 .
-
, :
;
;
;
.
.
, " ", .
, , ,
.
1 5.6. 595

, ,
. ,
,
, .
, .

.

(
)
. ,
,
.
, .
(r , 'l' 'l') . .
(r , ICOJIQC'l'BO 'l' , ICOJIQC'l'BO I , IQ'l' 'l') .
,
. (r , 'l' 'l') , (r ,
'l' I) (r , IQ'l' 'l') .
( , ) . ,
, ,
. .
(year , s teel_production) ,
(steel_production) , (year),
.
Open_polyline (. 1 3.6).
596 1 5

(, ,
( , 1) ,
arks (.
1 3. 1 5).
,
.

1 5.6. 1 .

:
( 1 960 : 30 64 6 )
( 1970 24 69 7 )
( 1980 : 23 68 9 )

-
( 0- 1 5), - ( 1 5-64), -
( 65 ). -
. ,
. ,
.
, Distribution
.
struct Distribution {
int year , young , micldle , old ;
};

istream& operator>> ( istream& is , Distribution& d)


11 : ( : )

char chl ;
char ch2 ;
char ch3 = ;
Distribution dd;

if (is >> chl >> dd . year


>> ch2 >> dd . young >> dd . middle >> dd . old
ch3) {
if ( chl ! = ( ' 1 1 ch2 ! = ' : ' 1 1 ch3 ! = ' ) ' )
'

is . clear ( ios_base : : failbit) ;


return is ;

else
return is ;
d = dd;
1 5.6. 597

return is ;

,
1 . - ,
, . Distribution
>>.
" ", "
". Distribution
, .
, " ". ,

. ""
, (, ,
, , ),
.
Distribution,
.
strinq file name = " j apanese-aqe-data . txt" ;
ifstream ifs { file_name } ;
if ( ! ifs) rrr ( " l'l' " , file_name) ;

11

for (Distri.ution d ; ifs>>d; ) {


if ( d . year < base_year 1 1 end_year < d . year)
error ( "o " ) ;
if (d. younq + d . iddle + d . old ! = 100)
rrr ( "w conaCYJO'l'C" ) ;
11

. japanese-age-data . txt
. . ""
,
,
, . ,
japanese-age-data . txt
string, .
.
. >>
,
.
598 15

1 5.6.2.
?
1 5.6. ,
Open_polyline - .
.
"" .

. ,
, ( ) .
. . ,
2008, .
.
.
,
. - .
, -
, .
.
.
constexpr int XIDAX = 600 ; 11
constexpr int 400 ;
11 :
constexpr int xoffset = 100 ; 11
constexpr int yoffset = 60 ; 11

constexpr int xspace 40 ; 11


constexpr int yspace = 40 ;
11
constexpr int xlength xa.x-xoffset-xspace ;
constexpr int ylength = ymax-yoffset-yspace ;

()
( ).


, .

1 5.6..
,
. ,
. .
,
.
15.6. 599

xmax

yspace

ymax ylength
xoffset xspace
xlength

yoffset

constexpr int base_year = 1 960 ;


constexpr int end_year = 2040 ;

constexpr doue xscale = doule (xlenqth) / (end_year-base_year) ;


constexpr doule yscale = doule (ylenqth) /100 ;
(xscale yscale)
-
, .
,
doue (. 4.3.3).
,
(1960), xscale
xoffset. .
, .
( , ),
, .
class Scale { 11
int s ; 11
int v ; 11
doule l ;
pulic :
Scale (int , int vb , doule s ) : s () , vbase (v) , scale ( s )
{ )
int operator ( ) ( int v) const
{ return s + (v-vbase) *scale ; } // . 2 1 . 4
};

,
,
. :
600 1 5

Scale xs {xoffse t , base_year , xscale } ;


Scale ys {ymax-yoffset , O , -yscale } ;

,
ys , ,
, ,
. xs
. ys .
-

1 5.6..
, .
.
Window win { Point { l00 , 100 } , , m , "Aqing Japan" } ;

Axis {Axis : : x , Point{ xoffse t ,ymax-yoffset} , xlength ,


(end_year-base_year) /10 ,
"year 1960 1970 1980 1990 "
"2000 2010 2020 2030 2040 " } ;
x . lael . ove ( -100 , 0) ;

Axis {Axis : : y , Point { xoffse t , ymax-yoffset} , ylength , 10 ,


" % of population" } ;

Line current_year { Point { xs (2008) ,ys ( 0 ) } , Point { xs (2008) , ys ( 100) } } ;


current_year . set_style (Line_style : : dash) ;

Point{ xoffset , ymax-yoffset } ,


( 1960 , ) . ,
. . ,
.
.
base_year end_year, ,
, .
" " .
, .
. . ,
.
,
, .
.
"year 1960 1970 1980 1 990 "
"2000 2010 2020 2030 2040"
.
:
1 5.6. 601

"year 1960 1970 1980 1990 2000 2010 2020 2030 2040"

,
.
current_year ,
. ,

xs ys.
, .
Open_polyline .
Openolyline children ;
Openolyline adults ;
Openolyline aqed;

for (Distribution d; ifs>>d; ) (


if ( d . year < base__year 1 1 end__year < d . year)
error ( " o " ) ;
if (d . younq + d. iddle + d . old ! = 100 )
error ( "poe'l'I :r" ) ;
const int = xs ( d . year ) ;
children . add ( Point( x ,ys ( d . younq) } ) ;
adults . add ( Point ( x , ys ( d . iddle ) ) ) ;
aqed. add (Point ( x , ys ( d . old) } ) ;

xs ys
. " ", Scale,

-
.
,
.
Text children_lael ( Point ( 20 , children . point ( O ) . y ) , "aqe 0- 14 " } ;
children . set_color (Color : : red) ;
children_lael . set_color (Color : : red) ;

Text adults_lael ( Point ( 2 0 , adults . point ( 0 ) . y ) , " aqe 15-64 " ) ;


adults . set_color (Color : : ue) ;
adults_lael . set_color (Color : : u) ;

Text aqed_lael ( Point ( 20 , aqed . point ( 0 ) . ) , "aqe 65+" ) ;


aqed. set_color (Color : : dark_qreen) ;
aqed_lael . set_color ( Color : : dark_qreen) ;

Shape
Window
(. 1 4.2.3).
602 1 5

win . attach (children) ;


win . attach (adults) ;
win . attach (aqed) ;

win . attach (children_lael ) ;


win . attach (adults_lael) ;
win . attach (aqed_lael ) ;

win . attach (x) ;


win . attach (y) ;
win . attach (current__year) ;

qui_ain ( ) ;

main ( ) ,
Scale Dis tribution,
, Distribution.
, , .

Aging Japan (QJ


" of populetfon

yeer 1 960 1 970 1 980 1 990 2000 201 2020 2030 20-40


3
, .
1 . 600600 " ".
2 . , .
-
FLTK.
. Graph . Window . .
1 5.6. 603

4. 400 " l = = 20
" 20 .
(300,300).
5. .
Shape
.
l . doue one (doule ) { return 1 ; }
[- 1 0, l l J (.) (300,300),
400 ( ).
2. , 20
. .
3.
.
4. doule slope (doule ) { return /2 ; } .
5. Text
" / 2 " .
.
6. doule square (doule ) { return * ; } .
7. ( ).
8. .
9. sloping_cos ( ) ,
slope ( ) ( ) .
, .
1 . struct Person, name string
age int.
2. Person,
"GoofY" 63 ( cout) .
3. (>>) (<<) Person;
Person (cin)
(cout).
4. Person, name
age.
5. Person
- name ( ) age ( ) ,
.
6. >> <<
Person.
604 1 5

7. . .
age [, 1 50), na.me
; : " ' [ ] * & " % $ # @ ! .
error ( ) . .
8. Person
(cin) vector<Person>; (cout).
.
9. Person , na.me
first_na.me second_na.me.
.
>> . .


1 . ?
2 . .
?
. ?
.
4. ?
5. ? ?
6. , . ?
7. ?
8. ?
9. ?
1 0.
?
1 1 . ?
1 2.
?
1 3. ?
, ?
1 4. ?
?


1 5.6. 605

1 . ,
.
int fac (int n) { return n>l ? n*fac (n-1 ) : 1 ; } // !

, , fac ( 4 ) .
. , 4 > 1 , 4 *fac ( 3 ) , , ,
4 * * f ( 2 ) , , 4 *3*2 *fac ( 1 ) , .. 4 * 3 * 2 * 1 .
, . ,
, (recursive).
, 1 5.5, (iterative),
( while).
, fac ( )
, fac ( )
, 1 , 2, 3, 4 20.
fac ( ) ?
2 . Fct, Function,
, ,
. Fct ""
, ,
..
3. Fct .
,
. ,
.
4. sin ( ) . cos ( ) , sin () + cos () sin () *
*sin () +cos () *cos () . .
5. "" ( 1 5. 5) 1-1/3+1/5-1/7+1 / 9-1/11+ . . .
(Leibniz) rt/4.
6. .
vector<doue>,
N
"", .. .
7. ,

. .
8. ,
( ) :
( 1 70,7), ( 1 75,9), ( 1 80,23), ( 1 85, 1 7), ( 1 90,6), ( 1 95, 1 ) .
? . .
606 1 5

.
.
9.
,
. , ClllA
.
,
. .

, .
1 0 .
?
(, ).
1 1 .
(, , , ;
"")
. , , , ..

.
, ,
. ,
- .
, ?
" "? :
. ;
, ,
, .
-!

(grap
hical user interface - GUI)
,
, ,

.
-
.
,
.
, , ,

.
"
" .
.
,
1 7 1 8 . II
.
608 1 6

1 6. 1 . 1 6.4. Button
Widget
1 6.2. Next 1 6.4. 1 . Widqet
1 6.. 1 6.4.2. Button
1 6.3. 1 . 1 6.4.. In Out
- -

1 6.3.2. 1 6.4.4. enu


1 6.3.3. - 1 6.5.
1 6.6.
1 6.7.
1 6.8. G U l-

1 6. 1 .
. .
, ,
, -
.
.
,
,
(. ). .
.
/ ,
. (.
). .
.
iostream ++ (. 1 0 1 1 ).
.
(. 1 2- 1 5).
.
r
. , ,

( . .
, ..). ( )

.
. ,
"" Windows/Mac,
.
1 6.2. Next 609

-.
(markup language), HTML,
(scripting language).
,
.
(
).
,
,

.


,
- .
: -
-.
-
.
,
.
,
, ,
.

.
,

.

1 6.2. Next
Next,
1 2- 1 5?
. ,

. ,
, .
, ,
,
.
1 2- 1 5 :
610 16

// / ,

11 win
win . wait_for_button ( ) ;

11 / ,

11 win
win . wait_for_button ( ) ;

// / ,

11 win
win . wait_for_button ( ) ;

, wait_for_button ( ) .
, ,
.
,
( ).
:
11 / ,
11
cin >> var ; //

11 / ,
//
cin >> var ; //

11 / ,
//
cin >> var ; //

. cin >> var,
, "" ,
. (
),
, : . -
( ..).
- , .
, (,
"- Next'").
, , .
, -
.
,

; , -
1 6.3. 611

,
, ,
.
", ,
"; . ",
, - ,
,
". ,
.
"" - . -
, ,
.

1 6..
"" (..
)

.
, -
"". ,
(callback functlons),
. .
.
.
,
.

.
,
.
. - Window,
( Simple_window . h) Simple_window,
next_button.
struct Siple_window : Graph_lib : : Window {
Siple_window ( Point , int w , int h , const strinq& title) ;

void wait_for_button ( ) ; 11
private :
Button next_button ; 11 Nex t
bool button_pushed; 11
61 2 1 6

static void next (Addre ss , Addre ss ) ; 11


11 nex t_bu t ton
void next ( ) ; 11 ,
11 nex t_but ton
};

. Simple_window Window
Graph_lib.
. Graph_lib :
: Window, ( FLTK)
.
Window ..
Simple_window.
Simple_window : : Simple_window ( Point , int w , int h , const strinq& title)
: Window { xy , w , h , title } ,
next_button { Point{ x_.ax ( ) -70 , 0 } , 70 , 20 , "Next" , c_next } ,
button_yushed{false }

attach (next_button) ;

. Simple_window
(). (w, h) (ti tle) Window
Graph_lib .
next_button (Point (x_max ( ) -70 , 0 ) ;
- ). (70, 20). ("Next")
(c_next).
, Window:
.
attach ( ) next_button
Simple_window; . .
.
.
button_pushed -
; .
next ( ) .
. .
pri va te. .
struct Simple_window : Graph_li : : Window {
Simple_window (Point , int w , int h , const strinq& title) ;
void wait_for_button ( ) ; 11
11 . . .
};

. .
.
1 6.. 61 3

1 6.. 1 .
_next ( ) - .

, . -

, ,
(callback function).
c_next ( ) (_ - "callback").
, , -
, . , _
next , Next.
next "".
, .


_ R

/GUI

/GUI

"" .
,
FLTK, . .
.
. - ,
.
next ( ) . _next ( )
Simple_window ;
- "- " c_next ( ) ,
Next.
(
) ,
.
++. , Simple_window
614 16

Button. .
. , ,
,
.
.
- , :
static void c_next (Addre ss , Address) ; 11
11 nex t b u t ton
s tatic , c_next ( )
. ..
, . -
, .
,
,
-. Address , _next ( )
. w- ". .
++, ,
. . ,
w-".
.
,

( Widget).
. ,
. - ,
Widget; _next ( )
Simple_window.
:
void Siple_window : : c_next (Addre ss , Addre ss pw)
// Simple_ window: : t () pw
{
reference_to<Siple_window> (pw) . next ( ) ;

reference_to<Simple_window> (pw) .
, pw,
Simple_window; .
reference_to<Simple_window> (pw)
Simple_window. 1 7 1 8
. reference_to (,
) . 1 .
- Simple_window
,
1 6.3. 61 5

,
- . - next ( ) .
_next ( ) ,
, ,
, -
,
,
.
_next ( )
- next ( ) .
next ( ) , ( -
).
,
, , "
": c_next ( ) -
- , next ()
. , ( "")
, ; , .
1 6. 5- 1 6.7. , .
Simple_window.
Simple_window next_button
.
next_button
,
c_next ( ) .
_next ( )
- next ( ) .
next ( )
.
. ,
,
( ) . ,
.
,
.
.

.
61 6 1 6

. ,
.
(
).
,
.
, , next ( ) , ,

.

1 6. .2.
. next () Simple_window
() ? ,
,
. .
. , , next ( )
.
1 1 / ,
11

win . wait_for_button ( ) ; //

// /
. wa i t for
button ( ) .
void Simple_window : : wait_for_button ( )
11 :
// ( )
11 ,
11 b u t ton_pushed t rue .

while ( ! bu ttonyushed) Fl : : wai t ( ) ;


buttonyushed = false ;
Fl : : redraw ( ) ;

.
FLTK ,
. - .
FLTK wa i t ( ) . wa i t ( )
,
, . ,
Microsoft Windows
1 6.3. 61 7

,
. , Window
. Fl : : wai t ( ) ,
. . wai t ( )
- , ,
- .
, - Next. wai t ( )
_next ( ) (
). wait_for_button ( )
next ( ) button_
pushed true. :

void Siple_window : : next ( )


{
button_pushed = true ;

, - button_pushed.
bool button_pushed; //
11 fa l se
wai t_for_button ( )
button_pushed redraw ( ) ,
.
.

1 6... -
, Widget
: -
, - :
struct Siple_window : Graph_lib : : Window {
Siple_window{ Point , int w, int h , const strinq& title } ;
void wait_for_button ( ) ; 11
private :
Button next_button ; // "Nex t "
bool button_pushed; //
static void c_next (Addre ss , Addre ss ) ; 11
11 nex t bu t ton
void next ( ) ; 11 nex t_ b u t ton
);

- ( 1 5 . 3 . 3) ,
_next ( ) .
Simple_window:
Siple_window : : Siple_window ( Point , int w, int h ,
const strinq& title)
61 8 1 6

: Window { xy , w , h , title } ,
next_button { Point { x_ax ( ) -70 , 0 } , 70 , 20 , "Next" ,
[ ] (Addre ss , Addre ss pw)
{
reference to<Simple_window> (pw) . next ( ) ;

} 1

button_Pushed{ false }
{
attach (next_button) ;

1 6.4. Button Widget


, .
struct Button : Widqet {
Button (Point , int w, int h , const strinq& lael ,
Callback ) ;
void attach (Window&) ;
};
Button Widget
, w h, lael
. , , -
- (, ),
Widget.

1 6.4. 1 . Widqet
(wtdget) .
-

, (control) .
-



. Widget :
class Widqet {
// Widge t Fl_ widge t ,
-

// F1_ widge t ;
// ,
11 FLTK
pulic :
Widqet ( Point , int w, int h , const strinq& s ,
Callback ) ;

virtual void move ( int dx , int dy) ;


virtual void hide ( ) ;
virtual void show ( ) ;
virtual void attach (Window&) ;
1 6.4. Button Widget 61 9

Point loc ;
int width ;
int heiqht;
strinq lael ;
Callback do_i t ;

protected:
Window* own ; // Widge t
11 Window
Fl_Widqet* pw; // Widge t FLTK
1;
Widget ,
Button ( ,
Widget, enu; . 1 6.7).
hide ( ) Widget .
show ( ) Widget .

Widget .
Shape, move ( )
Widget , ,
, attach ( ) .
, attach ( )
(. 1 4.3.5): , Widget,
,
Window. attach ( )
. attach ( ) Window
attach ( ) . ,
- ,
.
,
.

Widget

Window

Widget
620 1 6

, Window ,
Widget . 1 4.4.
-
Window
Widget. Widget ,
Window .
.
-. own pw
, protected.
Widget (Button,
Menu ..) GUI . h.

1 6.4.2. Button
Button - Widget, .
, , - .
class Button : puic Widqet
puic:
Button ( Point , int ww, int hh , const strinq& s ,
Callback )
:Widqet { xy , ww , hh , s , c } { }

void attach (Window& win) ;


};
. attach ( ) ()
FLT. (
, , 1 7 1 8). .
Widget .
,
( )
. ,
,
. ,

. , ,
,
, .

1 6.4.. In Out
,
Widget.
struct In_box : Widqet {
In_box ( Point , int w, int h , const strinq& s )
1 6.4. Button r Widget 621

:Widqet { xy , w , h , s , 0 } { }
int qet_int ( ) ;
strinq qet_strinq ( } ;

void attach (Window& win) ;


};

struct Out_ox : Widqet (


Out_ox (Point , int w , int h , const strinq& s )
:Widqet { xy , w, h , s , 0 } {
void put (int) ;
void put (const strinq&) ;

void attach (Window& win) ;


};
In_ , ,
get_string ( )
- get_int ( ) .
, , get string ( )
, .
strinq s = some_inox . qet_strinq ( ) ;
if (s == " " ) {
11

Out_box ,
. In_
put ( ) , .
In_box Out_box 1 6. 5 .
get_floating_point ( ) , get_
complex ( ) . ,
s trings tream -
, (. 1 1 .4) .

1 6.4.4. Menu
.
struct enu : Widqet {
enum Kind { horizontal , vertical } ;
enu (Point , int w, int h , Kind kk , const strinq& lael ) ;
Vector ref<utton> selection ;
ind k ;
int offset;
int attach (Button& ) ; 11
int attach (Button* ) ; 11 ,
11 new
622 1 6

void show ( ) //

for (Button& : selection) b . show ( ) ;

void hide ( ) ; //
void move (int dx , int dy) ; //

void attach (Window& win) ; // win


};

, Menu .
, Point .

. 1 6. 5 1 6. 7 .
( ) - Widget,
Menu ttach ( ) . ,
enu attach ( )
,

. Menu
Vector_ref ( 1 3. 1 0 .4).
(pop-up menu), (
1 6. 7).

1 6.5.

,
, , .
1 6.5. 623


( ; . 1 3. 6),
. ,
(. ) next next ;
Next point.
current (, ) , ,
.
current (, ),
,
( current (, ))
(, ), (, )
.
. ,
Quit .
,
: ,
. , ,
.
.

. .
struct Lines_window : Window {
Lines_window (Point , int w , int h ,
const strinq& title ) ;
Open_J?olyline lines ;
private :
Button next_button ; // (nex t_ x , nex t_y) l i nes
624 1 6

Button quit_button ;
In next_x ;
In_box next_y;
Out_ox xy_out ;

void next ( ) ;
void qui t ( ) ;
};
Open_polyline.
- Button, In_box
Out_ox, -,
. -
, .
Lines_window .
Lines_window : : Lines_window ( Point , int w, int h, const strinq& title)
:Window { xy , w , h , title } ,
next_button{ Point{ x_ax ( ) -150 , 0 } , 70 , 20 , "Next point" ,
[ ] (Addres s , Addre ss pw) {
reference_to<Lines_window> (pw) . next ( ) ;
} 1

quit_button{ Point{ x_max ( ) -70 , 0 } , 70 , 20 , "Quit" ,


[ ] (Address , Addre ss pw) {
reference_to<Lines_window> (pw) . quit ( ) ;
} 1

next_x{ Point{x_max ( ) -310 , 0 ) , 50 , 20 , "next : " } ,


next_y{ Point{x_max ( ) -2 10 , 0 } , 50 , 20 , "next : " } ,
xy_out{ Point { l00 , 0 } , 100 , 20 , "current ( , ) : " }

attach (next_button) ;
attach (quit_button) ;
attach (next_x) ;
attach (next_y) ;
attach (xy_out) ;
attach ( lines) ;

, ,
.
Quit .
FLTK, .
void Lines_window : : quit ( )
{
hide ( ) ; 11 FLTK

Next point:
, Open_polyline, r
.
1 6.5. 625

void Lines_window : : next ( )

int = next_x . get_int ( ) ;


int = next_y . get_int ( ) ;
lines . add (Point { x , y } ) ;

11
ostringstream s s ;
ss << ( 1 << << , 1 << <<
1 1 1
) ;
1

xy_out . put ( ss . str ( ) ) ;

redraw ( ) ;

. get_int ( )
In_box: os trings tream
I Out_box; -
s tr ( ) ostringstream.
, redraw ( ) , I :
,
redraw ( ) Window.
?
main ( ) .

#include "GUI . h"

int main ( )
try {
Lines_window win{ Point { l00 , 10 0 ) , 600 , 400 , "lines" } ;
return gui_main ( ) ;

catch (exception& ) {
cerr << "1JDD : " << . what ( ) << ' \n ' ;
return 1 ;

catch ( . . . ) {
cerr << "- JU\n" ;
return 2 ;

, , ! main ( )
win gui_main ( ) .
, if swi tch, - ,
6 7, -
gui_main ( )

run ( ) FLTK. . .
run ()
while (wai t () ) ;
626 16

,
. .
. .
?

1 6.6.
:
:
, - . ,
, .
"", ,
- . , wai t ( )
.
wai t ( ) ,
,
. FLTK
wai t ( )
,
.
"" .




----

" "



1 6. 7. 627

" "
,
. ,
. ,
,
.
( - 26).
.
. ,
.
, ,

. ,
,
, , ,
. ,

.
,
?
, , ,
1 6. 5 . next ( ) Lines_window.
Next point,
In_ox (next_x next_y), - lines
Out_ox (xy_out). , ,
, : ,
.. ,
.

1 6. 7.
,
" ",
. .
- lines.
color_menu .
struct Lines_window : Window {
Lines_window (Point , int w , int h , const strinq& title) ;

Open_J?olyline lines ;
enu color_enu ;

11 :
628 16

static void c_red (Addre ss , Address ) ;


11 :
static void c_lue (Address , Address) ;
11 :
static void c_ack (Address , Address) ;

11 :
void redyressed ( ) change ( Color : : red) ;
void uyressed ( ) change ( Color : : ue) ;
void ackyressed ( ) change ( Color : : ack) ;

void change (Color ) lines . set_color (c) ;

11 . . . , . . .
};


"" - .
.
.
,
-.
.
color_menu, .
Lines_window : : Lines_window ( Point , int w , int h ,
const string&title}
:Window ( xy , w , h , title } ,
// . . . . . .
color_menu( Point ( x_AX ( ) -70 , 40 } , 70 , 20 ,
enu : : vertical , "color" }

11 . . . . . .
color_menu . attach (new Button ( Point ( 0 , 0 } , 0 , 0 ,
"red" , c_red} ) ;
color_menu . attach (new Button( Point ( 0 , 0 } , 0 , 0 ,
"lue" , _u } ) ;
color_menu . attach (new Button ( Point ( 0 , 0 } , 0 , 0 ,
"lk" , _k } ) ;
attach ( color_menu) ;

, (
ttach ( ) ) / .
Menu : : attach ( ) ,
. .
.
1 6.7. 629

, ,
. . . ,
, ,
. color menu.
, ,
, , color menu .
, .

color menu .
color menu .
630 1 6

, color menu . .
. u,
. color menu .

.. . color menu
,
. Lines window
.
struct Lines window : Window
Lines_window (Point , int w , int h , const string& title) ;
1 6. 7. 631

private :
11 :
Open_polyline lines ;

11 :
Button next_button ; // (nex t_x , nex t_y) l ines
Button quit_button ; //
In next_x;
In_box next_y ;
Out_box xy_out ;
Menu color menu ;
Button menu_button ;

void chanqe (Color ) { lines . set_color (c) ;

void hide menu ( ) { color menu . hide ( ) ; menu_button . show ( ) ;

11 , :
void red_pressed ( ) { chanqe ( Color : : red) ; hide_menu ( ) ; }
void ue_pressed ( ) { chanqe ( Color : : u) ; hide_menu ( ) ; }
void ack_pressed ( ) { chanqe (Color : : ack) ; hide_menu ( ) ; }
void enu_pressed ( ) { menu_button . hide ( ) ; color_menu . show ( ) ; }
void next ( ) ;
void qui t ( ) ;

11 :
static void c_red (Address , Address) ;
static void c_ue (Address , Address) ;
static void c_ack (Address , Address) ;
static void c_menu (Address , Address) ;
static void c_next (Addres s , Address) ;
static void c_quit (Addres s , Address) ;
};

. , ,
. . . ,
, ,
. , . ,
.
.
Lines_window : : Lines_window ( Point , int w, int h ,
const strinq& title)
: Window{ xy , w, h , title } ,
next_button { Point{ x_max ( ) -150 , 0 } , 70 , 20 ,
"Next point" , c_next} ,
quit_button { Point { x_max ( ) -70 , 0 } , 70 , 20 ,
"Quit" , c_quit} ,
next_x{ Point {x_max ( ) -310 , 0 } , 50 , 20 , "next : " } ,
next_y{ Point {x_max ( ) -2 10 , 0 } , 50 , 20 , "next : " } ,
xy_out{ Point { lOO , O } , 100 , 20 , "current ( ,) : " } ,
632 16

color_menu{ Point{ x_max ( ) -70 , 30 } , 70 , 20 ,


enu : : vertical , "color" } ,
menu_button { Point{x_max ( ) -80 , 30 } , , 20 ,
" color menu" , c_menu}

attach (next_button) ;
attach (quit_button) ;
attach (next_x) ;
attach (nextJ) ;
attach (xy_out) ;
xy_out . put ( "no point" ) ;
color_menu . attach (new Button { Point { 0 , 0 } , 0 , 0 , "red" , c_red) ) ;
color_menu . attach ( new Button { Point { 0 , 0 } , 0 , 0 , "lu" , _u) ) ;
color_menu . attach (new Button { Point { 0 , 0 } , 0 , 0 , "lk" , _k) ) ;
attach (color_menu) ;
color_menu . hide ( ) ;
attach (menu_button) ;
attach (lines) ;

,
-. .

. ()
,
.

1 6.8. GUl-

, : ,
. ,

. main ( ) .
int main ( )
{
Lines_window{ Point { l00 , 10 0 } , 600 , 400 , " lines " } ;
return qui_ain ( ) ;

? , ,
; ,
- .
?
.
(, ,
).
1 6.8. GUl- 633

. ,

.
.
.
.
r
.
, ,
- ,
.
.
(). -
. .
.
, ? (. 1 6.5).
int main ( )
{
Line_window win{ Point { l00 , 100 } , 600 , 400 , "lines " } ;
return qui_main ( ) ;

w" win Lines_window.


, .
, , ,
. .
, .
,
. ,
. ?
.

.
( ) .

. .

,
-
"" ( .
++).
634 1 6

, ,
Shape Widget -
, -
. ,
, .
11
void load_disaster_menu (enu& m)
{
Point oriq { 0 , 0 } ;
Button l { oriq , 0 , 0 , 11flood11 , c_flood} ;
Button b2 { oriq , 0 , 0 , "fire" , c_fire } ;
11 . . .
m . attach (l ) ;
m . attach (b2 ) ;
11 . . .

int main ( )

11 . . .
enu disasters { Point{ l00 , 100 } , 60 , 20 , Menu : : horizontal ,
"disasters " } ;
load_disaster_menu (disasters ) ;
win . attach (disasters) ;
11 . . .

.
load_di sas ter_menu, - .
1 8.6.4 (
n.
8 . 5 . 8 . ,
load_di sas ter_menu ( )
, di sasters
() . , ,
.
,
new.
11
void load_disaster_menu (enu& m)
{
Point oriq { 0 , 0 ) ;
m . attach (new Button { oriq , 0 , 0 , 11flood11 , c_flood} ) ;
m . attach (new Button { oriq , 0 , 0 , "fire " , c_fire } ) ;
11 . . .
, (, ,
) .

1 .
FLTK ( .)
2. Graph_l ib.
1 6. 5 .
3. .
, 1 6.7, .
4. .
. .

r
1 . ?
2. ?
3. ?
4. ?
5. -
++?
6. ?
7. ?
8. ?
9. "FLTK"?
1 0. GUI ?
1 1 . .
?
1 2 . .
1 3. ?
1 4. ?
1 5. ?
1 6. ?
1 7. ?
1 8.
.
636 16

1 9.
,
-?

11

/ -
-


1 . y_window, Simple_window,
, , next qui t.
2. ( _window) 44
.
,
( ).
. Image Button;
. ..
" "
s td_lib_facilities . h:
#include<random>
inline int rand_int ( int min, int max)

static default_random_engine ran ;


return uniform_int_distribution<> {min , max } (ran) ;

[min,max).
4. "". "". "
" "". (
) .
, .
5. ,
Next.
,
.
6. " ", .. .
. .
- ,
1 6.8. GUl-nporpa 637


(, ).
. : clock ( ) , sleep ( ) .
7. ,
, . Start Stop.
8. .
.
(, ) .
9. 7 ,
, .
1 0. ,
(. sin ( ) log ( ) )
,

- .

. ,
(, GUI
).

, .
, , , ,
,
/ ,
.
, GUI
-, ,
,
..
, , .
,
. ,
; /
.
111


lf vector.'
- /.uJU- (& .!)



++, STL.

STL . ,

,
STL,
.
- ,
.


STL: vector .
642 1 7

1 7 . 1 . 1 7 .6.
1 7.2. 1 7.7.
1 7.3. , 1 7.8. : void*
1 7.3. 1 . sizeof
1 7.4. 1 7.9.
1 7.9. 1 .
1 7.4. 1 .
1 7.9.2. , -
1 7.4.2.
1 7.4.3. 1 7.9.3. :
1 7.4.4. 1 7.9.4.
1 7.4.5. 1 7.9.5 .
1 7.4.6. 1 7 . 10. this
1 7 5
. . 1 7.10. 1 .
1 7 .5 . 1 . r
1 7.5.2.

1 7 . 1 .

++ vector.
.
, push
back ( ) , ,
s i ze ( ) , ,
.
- , , ( ,
)
. s tring
, , list
map, 20.

. , -
, - . .
vector<doue> v . push_back ( 2 . 3 )
2 . 3 doue
v (v . size ( ) ).
, push back ( ) ; ,
, - .
,
vector, ,
1 7 . 1 . 643

. ,
,
++.
, vector,
.
,
vector,
. map,
. ++
( 20 2 1 ) . .
,
.
,
++,
.
- sort ( ) .
vector
.
vector.
. ,
vector,
vector, ++.

.
,
. .
1 7. ? ,


?
(). ,
( ) .
1 8. ?
? ,
.
1 9.
?
?
++ .
,
,
, ( )
644 1 7

.
.
, . ?
vector s tring :
. , , vector string,
,
. ,
, .
. .. ,
vector , ? ,
,
.
, - (
, ..), .
,
.
,
.
"
", , .
" ",
(
) . ,
.
++, .
, (
)
. ++, ,
.
I ,
, ,
-
,
, .

1 7.2.
vector
.
vector<doule> aqe ( 4 ) ; 11
aqe [0 ] =0 . 33 ; // d
aqe [ 1 ] =2 2 . 0 ;
1 7 .2. 645

aqe [ 2 ] 2 7 . 2 ;
q [ ] "54 . 2 ;

, rr acca vector
doule . 33, 22 . , 2 7 . 2 54 . 2 .
, 1 , 2 3.
++ .
,
, ++.
rracca vector .
, aqe .
() size- 1 . , aqe
aqe . size ( ) - 1 . aqe
.
age:
4
age[O]: age[ 1 ] : age[2]: g[]:
0.33 22 . 27. 2 54.2
" " ?
? ,
rr acc vector. , rrac
ca -
. ,
?
vector, :
rr acc .
, . ?
, .
.
clase vector {
int size , aqeO , aqel , aqe2 , q ;
11 . . .
};
, ,
.
age:
slze: age[O] : age[ 1 ] : age[2] : age[3] :
4 0.33 22 .0 27. 2 54. 2
646 1 7

,
push_back ( ) ,
: ,
. ,
, .
. vector,
push_back ( ) , ,
vector . ,
,
. -
. ++ , ,
(pointer). * ,
doule* doule.
vector.
// doue
class vector {
int sz ; 11
doue* elem; 11 ( doul e)
pulic :
vector ( int ) ; /! : s
11 doul e ,
11 elem s s z
i n t size ( ) const { return sz ; )
//
};
vector,
"" . "" (
"") - ""
++.

1 7 ,
. .


. .
(address) ,
. .
, 1 ..
-

2 220_ ,

1''-- -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
, -. , :
int var = 17 ;
1 7.. . 647

.
int, var
1 7 . ,
. . , . , ,
in t,
int int*.
int* ptr = &var ; // ptr va r

,
&. , var ,
4096 ( 2 12), ptr
4096.

1 2 ptr 2 1 2 2 20- 1
@
1 1 1 1_-_-_-_-_-_-_ -_-_ -_-_-
_-_-_-_-_-_ -_ -_-_-_ ?:_-_-_------ -0
,
,
.
, .
, :
int = 17 ;

int* pi = & ; // i n t

doue = 2 . 71828 ;
doule* pd = & ; // do ue

, ,
, *, :
cout << "pi==" << pi << " ; pi==" << *pi << 11\n" ;
cout << "pd== " << pd << " ; pd==" << *pd << 11\n" ;

*pi 1 7 , *pd -
doue, 2 . 7 1 8 2 8 . pi pd ,
. ,
(),
, ;
( .2. 1 . 1 ) .

:
*pi = 27 ; 11 Ofi: 2 7 i n t ,
11 pi
*pd = . 1415 9 ; 11 : 3 . 1 4 1 5 9
11 dou e , pd
648 1 7

*pd = *pi ; 11 : i n t ( *p i )
1 1 doue ( *pd)

, ,
,
. " int?" . -

, .
, int
( ) .
.
int i = pi ; // : in t * i n t
pi = 7 ; // : i n t i n t *

char (.. char* ) int


(.. int* ) . :
= pi ; 11 : i n t * cha r *
pi ; 1 1 : cha r * i n t *

pi?
. char int:
.
char ehl = ' ' ;
ehar eh2 = ' ' ;
ehar h = 1 1 ;
char eh4 = ' d ' i
int* pi = &h ; 11 h , ,
11 cha r . : cha r *
11 in t *
11 , ,
*pi 12345 ; 11 ,
*pi 678 90 ; 11 i n t

,
, , , .
h : pi:

,
12345 , &h.
, . . ch2 ch4 .
( )
pi l *i=7890
67890 . ,
,
.
1 7.. . 649

,
int ,
reinterpret_cast ( -
1 7.8).
.
.

. ,
, vector. ,
, (.
25). ,
,
. -
,
. ,
1 8 1 9 ,
, vector.

1 7 . . 1 . sizeof
in t? ?
sizeof.

void sizes ( char ch , int I , int* pi )


{
cout << " char "
<< sizeof ( char) << 1 1
<< si zeof ( ch) << ' \n ' ;
cout << " int "
sizeof ( int) << 1 1
<< sizeof ( i ) << ' \n ' ;
cout << " int* "
<< sizeof ( int* ) <<
<< sizeof (p) << ' \n ' ;

, sizeof ,
: sizeof
, - .
sizeof ,
sizeof ( char) ,
1 . , char ,
sizeof .


. ,
.
bool, doue .
650 1 7 11


++. sizeof ( int)
. 8-
, int 32 .
int 1 6 ,
int 64 .
vector? .
vector<int> v ( l000 ) ; // 1 0 0 0
cout << " vector<int> ( lO O O ) "
<< si zeof (v) << 1 \n ' ;

:
vector<int> ( l0 0 0 ) = 20


(. 1 9.2. 1 ) . , sizeof
.

1 7 .4.
vector,
1 7. 2 . I)J,e vector ?
elem . ?
, ++,
1 ( )
(
). , ,

( ) .
;
.
. .


1 7 .4. 651

++ (
(heap)) new:
doue* = new doule [ 4 ] ; // 4 doue
11


doue
.
.
.
:

new . .
new (),
. .
, new, *:
char* q = new doule [ 4 ] ; // : doule *
11 cha r *

new doue,
doue char, (
) doue
char.

1 7 .4. 1 .
new (allocation)
(free store).
new .

.
.
.
652 17

new ,
:
int* pi new int ; // i n t
int* qi = //
new int [ 4 ] ;
11 i n t ( )
doule* pd = new doule ; //
11 doue
doule* qd = new doule [n] ; //
/ / doue

,
. , ,
,
. n 2, .

pi: __:-----!

qi: >---- !

-

pd:
qd:
.
.
p i = pd; // : dou e * in t *
pd = pi ; // : i n t * doul e *

? ,
int doue, .
[ ] . ,. ,
. , qi [ 2 ]
, int qi [ ] , qd [ 2 ]
, doue
qd [ O ] . int doule,
, , qi ,
qd,
.
.
:
(type errors).
1 7.4. 65 3

1 7 4 2
. . .

*.
[ ] :
doule* = new doule [ 4 ] ; //
11 d
doule = * ; //
//
doule = [2 ] ; //
//

vector.
. . [ 2 ] ;
[] . [ ] , *
-

[ ] * .
* = 7.7; 11
11
[2 ] = 9 . 9 ; 11
11

. .
.
.
doule = * ; // ,
* = 8 . 9 ; // ,

[ ] .
( ,
), .
doule = [ ] ; //
//
[3] = 4 . 4 ; //
11
doule = [ ] ; // { ] - , *

. . -
.
[] : [ 1 ) : [2] : [] :
8.9 9.9 4. 4

vector.
654 17

1 7 .. .
, , ,
,
. .
doule* pd = new doule [ 3 ] ;
pd [2 ] 2.2;
pd [ 4 ] 4.4;
pd [-3) -3 . 3 ;
, pd?
pd [ 4 ] ?
pd, "" "" .
; .
, .
pd [ -3 ] .
doue ,
pd.

pd :

pd [-3] : pd [ -2] : pd[O] : pd [1] : p d[2] : pd[3] : pd [4] :


-3.3 2.2 4.4

, ,
pd [ -3 ] pd [ 4 ] . .
,
doule, pd. ,
, . .
. ""
" - ", "
". ;
. , .

, ,
, .
, ,
,
. ,
, -
""
. , .
, .
: ,
1 7.4. 655

, ,
.
(" ") .
,
. ,
vector,
new, , vector

.

. ,
doue* doue*
, .
, . .
doue* = new doue ; // doue
doule* q = new doule [ lOOO ] ; // do u e

q [ 700] = 7 . 7 ; 11
q = ; 11 q ,
doule d = q [ 700] ; 11 !

. q [ 7 0 0 ]
,
.


q
q
:_-_-_-:_-_-_-_-_-_-_-_-_-:_-_-_-_-_-
! -
, , "
?" , ,
. , -
vector . ,
++, ,
" ",
.

, . . ,

.
656 1 7

1 7 4 4
. . .

. , - ,
; .
, , ,
. .
doule* ; // :
//
doule* pl new doule ; //
// dou e
doule* 2 new doule (S . 5 ) ; //
// dou e 5 . 5
doule* 3 = new doue [S] ; //
// dou e

,
. .
* = 7.0;
7 . .
, .
, .
, :
.
,
,
-
. , ,
, - , ,
.
- .
.
.
, new ,
.
, -
, 2 :
*2 5 . 5.
( ) , .
, [ ] !
,

,
new, :
doue* 4 new doule [ S ] ( 0 , 1 , 2 , 3 , 4 } ;
doule* 5 = new doule [ ] ( 0 , 1 , 2 , 3 , 4 } ;
1 7.4. 657

4 doue,
. . 1 . . 2 . , 3 . 4 . . 5;
,
.
,
, ,
. ,
(
). , ,
.
. ,
. .
.
.
.
, :
* pxl new ; // ,
//
* 2 new [ 1 7 ] ; // 1 7 ,
11

, ,
:
* pyl = new ; // :
* 2 = new { 13 } ; // : ( 1 3 }
* 3 = new [ 1 7 ] ; // :
* 4 = new [ 1 7 ] ( 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 } ;

new ,
(
). .

1 7 .4.5.
,
,
nullptr.
doue* = nullptr ; //


(null pointer).
(.. , -)
nullptr:
if ( != nullptr) //
658 1 7

,
(, )
, delete (.
1 7.4.6). ,
. nullptr,
if , .
if () // ;
1 1 p O ! =n u l lp t r

, ,
" ",
.
.
- , .
, ; :
, ,
? ,
?
nullptr ++ 1 1 ,
() NULL
nullptr. /
,
nullptr.

1 7 4 6
. . .

new .
,
, .
.

. .
doule* calc ( int res_size , int ) //
{
doue* = new doue [m.ax] ;
doue* res = new doule [res_size] ;
11
11 res
return res ;

doule* r = calc (l00 , 1000) ;


calc ( ) "
" , doule,
1 7 .4. 659

. , calc ( 100 , 1000)


,
doue.
, , delete.
, delete
, new:
doule* calc ( int res_size , int )
// , res ,
//

doule* = new doue [m.ax] ;


doule* res = new doule [res_size] ;
//
// res
delete [ ] ; // ,
return res ;

doule* r = calc ( l00 , 1000 ) ;


11 r
delete [ ] r ; // ,

,
:
.
delete :
delete , new
;
delete [ ] ,
new .
.
- .
.

int* = new int ( S ) ;


delete ; // : ,
// new
11 . . .
delete ; // : ,
11

delete .
,
,
delete
.
1 7


, ,
: (
) .
.
.
(
),
:
int* = ;
delete ; // :
delete ; // - -

?
, ,
? .
(automatic garbage collection)
(garbage collection) . , -
.
,
. (.
www . stroustrup . com/C++ . html). ,
"", ,
.
? ,
"",
.
, ( . 25).
, -
,
. , , .

. .

(Unix, Windows - ),
. ,
,
, , ,
. ,
, ,
, .
1 7 .5. 1 661

1 7 5
. .

, .

.
11 dou e
l vector {
int z ; 11
doul111 * 111 1 111111 ; 11
pulic :
vector {int ) //
: z { } , 11 s z
111 l111111 { n111w doul111 [ ] } 1 1 elem
{
for ( int iO ; i< ; ++i ) 11
111 l 111111 [ i ] O ; 11

int iz111 ( ) cont { r111 turn z ; } //


11 . . .
};
, sz.
, vector
, size ( ) .
new, ,
new, elem.
,
( . ). vector
, .
, vector
.
new. . 1 7.4,
delete. .
void f ( int n)
{
vector v (n} ; 11 doue
11 . . .

f ( ) v,
, . clean_
up ( ) - vector :
void f2 ( int n )
{
vector v (n) ; 11 ,
11 i n t
// . . . v . . .
662 1 7

v . clean_up ( ) ; // clean () elem

.
, , ,
delete.
clean_up ( ) : .
.
, , ,
, . -
(destructor) .
,
, -
. ,
. , , ,
, .
.
class vector {
int sz ; 11
doule* l; //
pulic :
vector ( int s ) 11
: sz { s } , 11 s z
ele{ new doule [ s ] } 11 elem
{
for ( int i=O ; i<s ; ++i} //
ele [i ] =O ; //

vector ( ) 11
{ delete [ ] l; } //
};

:
void f ( int n )
{
doule* = new doule [n] ; //
11 d
vector v (n) ; // (
11 d }
11 . . v
.

delete [ ] ; // ,
// d
// vector , v

delete [ ]
! vector,
new,
delete [ ] .
1 7.5. 663

vector. , vector
, . .

. .
. ,
- . , .. ,
ios tream? ,
. .. .
, "" - ,
.

1 7 .5.1 .
,
, :
struct Custoer {
string name ;
vector<string> addresses ;
11 . . .
};

void s_fct ( )

Customer fred;
11 fred
11 fred

some fct ( ) fred


, : ,
name addresses.
, .
: "
Customer. ".

.
( )
( ,
). . : "
. " (
, delete ..).
664 17

1 7 5 2
. . .

,

++. .
-
, .

.

, .
"-"
vector, .
1 9. 5 .
.
.
Shape* fct ( )
{
Text tt { Point { 200 , 200 } , "Annemarie" } ;
11 . . .
Shape* = new Text { Point { l00 , 100 } , "Nicholas " } ;
return ;

void f ( )
{
Shape* q = fct ( ) ;
11 . . .
delete q;

- .
, . .
, .
fct ( ) tt Text (. 3. 1 1 ), ,
. Text string,
. - s tring
, vector.
tt :
Text, 1 7. 5 . 1 .
Text, fct ( ) ? f ( )
, q Text:
. Shape.
delete q Text?
1 7 .6. 665

1 4. 2 . l , Shape
. Shape .
, .
delete q. delete q,
. , , .
delete q -Shape ( ) Shape.
-Shape ( ) ,
(. 1 4.3. 1 )
. Shape, -
-t ( ) . Shape : : -Shape ( ) ,
Text : : -t ( ) Text,
string, .
: ,
.
.
1 . , , ,
.
2. , , ,
new.
3.
new,
, , ,
.
,
delete.
. .


.
, ,
, .
, .

1 7 6
. .

vector .
.
- get ( ) set ( ) .
11 doue
class vector {
666 17

int sz ; //
doule* elem; //
pulic :
vector ( int s ) : sz { s } , elem{ new doule [ s ] } //
{ / * . . . */ }
vector ( ) { delete [ ] elem; } 11
int size ( ) const { return sz ; 11

doule qet ( int n ) const { return elem [n) ; 11


void set ( int n , doule v) { elem [n ] =v ; } 11
);

get ( ) set ( ) ,
[ ] elem.
, doue,
.
vector v ( S ) ;
for ( int i=O ; i<v . size ( ) ; ++i )
v . set ( i , l . l * i ) ;
cout << "v [ " << i << " ] == " << v . qet ( i ) << \n ' ;

:
v [ O ] ==O
v [ l ] ==l . l
v [2 ] ==2 . 2
v [ ] == .
v [ 4 ] =4 . 4

vector . .
get ( ) set ( ) ,
( ).
, , ,
, .

.

1 7. 7.
,
. ,
vector ,
char:
vector* f ( int s )
{
vector* new vector ( s ) ; 11
11 *
1 7. 7. 667

return ;

void ff ( )
{
vector* q = f ( 4 ) ;
11 *q
delete q; 11

, , vector
delete, vector.
vector* = new vector ( s ) ; 11
delete ; //

vector
new :
vector;
vector,
;
vector .

vector, delete
:
vector; ,
, ( ),
, ;
, vector.
,
(. . 8.5.8). () vector,
:
vector<vector<doule>>* = new vector<vector<doule>> ( lO ) ;
delete ;

delete vector<vector
<doue>>. , .
vector<doue>. ,
.
delete ( ,
, , vector) , ,
(destroy) ,
(deallocate).
, , "" new

delete. (. . ,
668 1 7

Vector_ref 1 3. 1 .4) ,
new . delete -

.
,
, ? ,
. ().
.
vector v ( 4 ) ;
int = v . size ( ) ;
doue d = v . qet ( ) ;

-> ()
:
vector* = new vector ( 4 ) ;
int = p->size ( ) ;
doue d = p->qet ( ) ;

. () . -> ()
- -.
, int doue, , ->
. "" ""
(member access operators).

1 7. . : void*
,
. .
, (, ,
* [ ] ) .
,
.
, .
, ,
,
(, ,
,
++). , ,
,
.
.
.
, .
1 7.8. : void 669

,
,
( ) .
void* " ,
". ,
. -
,
. ,
( 1 6.3. 1 ),
(, new) .
void , , ,
void " ".
void v; // : void
void f ( ) ; 11 f (} ;
11 f () void

void* ,
:
void* pvl = new int ; // !\ : i n t * voi d *
void* pv2 new doule [ lO ] ; / / : doul e * voi d *

,
void* , .
void f (void* pv)
{
void* pv2 = pv ; // ( void*
11 )
doue* pd = pv ; // :
11 void* dou e *
*pv = 7 ; 11 : void *
11 ( , pv,
// )
pv [ 2 ] = 9 ; // : voi d *
int* pi = static cast<int*> (pv) ; // :
_
11 . . .

static_cast
, , void* doue* ( .5.7).
static_cast -
( ) ,
.
( ). , static_cast,
(explicit type conversion)
(cast).
670 1 7

++ ,
static_cast.
reinterpret_cast
, , int doue*.
const cast const.
.
Reqister* in = reinterpret_cast<Reqister*> ( Oxff) ;

void f ( const Buffer* )


{
Buffer* = const_cast<Buffer*> (p) ;
11 . . .

- ,
reinterpret_cast. .
(. OxFF)
Register (,
). , .
.

in : OxFF

OxFF :

const cas t
const const Buffer* . ,
, .
s tatic_cast
const,

static_cast. ,
. : ,
? ,
? .
, .
, , static_
cast.
. reinterpret_cast.
.
1 7.9. 671

1 7. 9.
(reference)
.
.
- ,
, .
, ,
new & .
, ,
* [ ] .
,
, .

.
(
. );
(
, ).
.
.
int = 10 ;
int* = & ; // &
* = 7 ; /!
11 *
int 2 = * ; 11
int* 2 = &2 ; 11
11 i n t
2 = ; 1 1 2
= &2 ; //

.
int = 10 ;
int& r = ; // & ,
r = 7; 11
// r ( * )
int 2 = r; // r
11 ( * )
int& r2 = 2 ; 11 i n t
r2 = r ; /!
11 2
r = &2 ; // ll :
11 ( i n t * i n t & )
672 1 7

; ,
, -
. ,
. 1 7.9.3.
, .
-,
.

1 7 . 9 . 1 .
,
, :
int incr_v ( int ) { return x+l ; 11
11
void incr_p ( int* ) { ++* ; } 11
11 (
11 )
void incr_r ( int& r) { ++r ; } 11

? ,
( , ) :
int = 2;
= incr_v (x) ; 11 incr_ v () ;
11

, in t.
" (.
1 8. 3.4), .

? ,
, .
.

, . .
int = 7 ;
incr_p ( &x) ; 11 &
incr_r (x) ;

& incr_ ( &)


, .
incr_r (x) . . "".
.
,
, .
, . . nullptr. .
1 7.9. 673

incr_p ( O ) ; // : i ncr_p ()
int* = ;
incr_p (p) ; // : i n cr_p ()

, . incr_ ( )
:
void incr_p ( int* )
{
if (p=nullptr)
rrr ( " incr_p ( ) " ) ;
++* ; //
// ,

, incr_ ( )
, . 5 , ,
.
, (, incr_r ( ) ) ,

, .
" " ( )
,
. :
, p==nullptr .
, :
.

.
, "
" ( nullptr),
( nullptr!).

.
(. 8 . 5 . 6.)

1 7 . 9 .2. ,
1 4. 3 , ,
Circle, Shape.
:
Circle* Shape* .
Shape Circle.
.
void rotate ( Shape*s , int n) ; // *s
674 17

Shape* = new Circle { Point { l00 , 100 } , 40 } ;


Circle c { Point { 200 , 200 } , 50 } ;
rotate (p , 35) ;
rotate ( &c , 45) ;

.
void rotate (Shape&s , int n) ; 11 s

Shape& r = ;
rotate ( r , 55) ;
rotate ( *p , 65 ) ;
rotate ( c , 75} ;
-
(. 1 4.3, 1 4.4).

1 7.9.. :

. , ,
.
- . ,
.

norse_gods

Frela Odln Thor

(douy-linked list).

. ,
, . (singly-linked list).
, .
:
struct Link {
strinq value ;
Link* prev ;
Link* succ ;
Link ( const strinq& v, Link* = nullptr , Link* s = nullptr)
: value { v } , prev{p} , succ { s } { }
};
. Link,
. succ,
- prev. ,
1 7.9. 675

, .
:
Link* norse_qods = new Link { "Thor" , 0 , 0 } ;
norse_qods = new Link { "Odin" , 0 , norse_qods } ;
norse_qods->succ->prev = norse_qods ;
norse_qods = new Link { "Freia" , 0 , norse_qods } ;
norse_qods->succ->prev = norse_qods ;

, Link .
: - , , ,
, -
. , , ,
succ prev .
,
.
Link* insert (Link* , Link* n) // ()

n->succ = ; //
p->prev->succ = n ; 11
n->prev = p->prev; //
//
p->prev n ; 11
return n ;

,
Link
. , .
, ,
Link, ,
.
. . .
insert ( ) .
, , n. p->prev nullptr.
. ,
insert.
Link* insert (Link* , Link* n) // ;

{
if (n==nullptr) return ;
i f (p==nullptr) return n ;
n- >succ = ; //
if (p->prev) p->prev->succ = n ;
n->prev p->prev ; //
11

p->prev = n ; //
676 1 7

return n ;

:
Link* norse_qods = new Link { "hor" } ;
norse_qods = insert { norse_qods , new Link { "Odin" } ) ;
norse_qods = insert (norse_qods , new Link { " Freia" } ) ;
,
prev succ, .
, -

. ,
, ,
nullptr, ()
-

insert ( ) .
,
(. 1 5. 3 . 1 , .9. 2),

.

1 7. 9 .4.
list,
20.4. ,
, Link,
, " " ,
.
Link,
, ?
, .
.
insert: .
add: .
erase: .
find: .
advance: - .
:
Link* add (Link* , Link* n) // ;
{
11 , insert (. . 1 1 )
1 7. 9. 677

Link* erase (Link* ) 11 * ;


{ 11
if (p==nullptr) return nullptr ;
if (p->succ) p->succ->prev = p->prev;
if (p->prev) p->prev->succ = p->succ ;
return p->succ ;

Link* find(Link* , const strinq& s ) // s ;


// n ul lp t r ,

while () {
if (p->value s ) return ;
= p->succ ;

return nullptr ;

Link* advance (Link* , int n) //

// n u l lp t r ,
// ; -
-

{
if (p==nullptr) return nullptr ;
if (O<n) 1
while (n-- )
if (p->succ nullptr) return nullptr ;
p->succ ;
"'

else if (n<O) {
while (n++)
if (p->prev nullptr) return nullptr ;
p->prev;

return ;

n+ + .
, ,
.

1 7 . 9 . 5 .
.
Link* norse_qods = new Link { " hor" } ;
norse_qods insert (norse_qods , new Link{ "Odin" } ) ;
norse_qods insert (norse_qods , new Link { " Zeus" ) ) ;
norse_qods = insert (norse_qods , new Link { "Freia" } ) ;
678 1 7

Link* qreek_qods = new Link{ 11Hera11 } ;


qreek_qods = insert (qreek_qods , new Link { 11Athena11 } ) ;
qreek_qods insert (qreek_qods , new Link { 11ars11 } ) ;
qreek_qods = insert (qreek_qods , new Link { 11Poseidon11 } ) ;

" ". : - ,
, - , ( -
). :
Link* = find (qreek_qods , 11ars 11 ) ;
if () p->value = 11Ares11 ;

, find ( )
nullptr. , , , ( ,
greek_gods),
- , .
.
Link* = find (norse_qods , 11Zeus 11 ) ;
if () {
erase (p) ;
insert (qreek_qods , p} ;

? (,
).
erase ( ) norse_gods? ,
, ,
.
Link* = find (norse_qods , "Zeus 11 ) ;
if () {
if (p=-norse_qods) norse_qods = p->succ ;
erase (p) ;
qreek_qods = insert (qreek_qods , p ) ;

:
, .
- , .
.
void print_all ( Link* )
{
cout << 11 { 11 ;
while () {
cout << p->value ;
if (p--p->succ) cout << 11 11 '.

cout << 11 } 11 ;
1 7 .1 . this 679

print all (nore_gods) ;


cout<<" \n" ;
print_all (greek_god ) ;
cout<<"\n" ;

:
Freia, Odin , Thor }
{ Zeus , Poseidon , r , Athena , Hera

1 7 . 1 . thi s
, ,
, Link*
, .
. Link (
), ? ,
, -
? .
class Link
puic :
string value ;
Link ( const tring& v, Link* = nullptr , Link* s nullptr)
: value { v} , prev{ p } , succ { s } { }

Link* inert (Link* n) ; 11


Link* add (Link* n) ; 11
Link* erase ( ) ; 11
Link* find (const string& ) ; //
const Link* find ( const string& s ) const ; //
11 (. 1 8 . 5 . 1 )
Link* advance (int n) const; //
/!
Link* next ( ) const { return u ; }
Link* previous ( ) const { return prev ; }
private :
Link* prev ;
Link* succ ;
);

. ,
Link,
-. ( ) next ( )
previous ( ) , . -
succ prev
. , ()
; " ".
680 1 7

Link : : insert ( ) ,
.
Link* Link : : insert (Link* n) // ;
{
Link* = this ; //
if (n==nullptr) return ; //
if (p==nullptr) return n ; //
n->succ = ; //
if (p->prev) p->prev->succ = n;
n->prev p->prev; //
11

p->prev n; //
return n ;

,
Link : : insert ( ) ?
. - this -
. .
this .
Link* Link : : insert (Link* n) // this;
{
if (n==nullptr) return this ;
if ( this==nullptr) return n ;
n->succ = this ; 11 this
if ( this->prev) this->prev->succ = n ;
n->prev = this->prev; // this
11
this->prev = n ; // th i s
return n ;

,
this , .
Link* Link : : insert ( Link* n) // thi s ;
{
if (n==nullptr) return this ;
if ( this==nullptr) return n ;
n->succ = this ; // this
if (prev) prev->succ = n ;
n->prev = prev ; // this
11
prev = n ; // this
return n ;

,
thi s . ,
1 7 1 . this
. 681

, .
.
, this
: , -.
- .
, this
-. .
struct S {
11 . . .
void mutate ( S * )
{
this = ; // 011 : this
11 . . .

) ;

1 7 1 0 1
. . .

, ,
.
Link* norse_qods = new Link { " Thor" ) ;
norse_qods = norse_qods->insert (new Link { "Odin" ) ) ;
norse_qods norse_qods->insert (new Link { " Zeus " } ) ;
norse_qods norse_qods->insert (new Link { "Freia" } ) ;

Link* qreek_qods = new Link { "Hera" } ;


qreek_qods qreek_qods->insert (new Link{ "Athena" } ) ;
qreek_qods qreek_qods->insert (new Link { "ars " } ) ;
qreek_qods qreek_qods->insert (new Link { " Poseidon" } ) ;

. ,
"". . .
Link* = qreek_qods->find ( "ars " ) ;
if () p->value = "Ares " ;

.
Link* 2 = norse_qods->find ( " Zeus " ) ;
if (2) {
if (p2==norse_qods ) norse_qods = p2->next ( ) ;
p2->erase ( ) ;
qreek_qods = qreek_qods->insert (p2 ) ;

.
void print_all (Link* )
{
682 1 7

cout << " { " ;


while () {
cout << p->value ;
if (p=p->next ( ) ) cout << " ";

cout << " ) " ;

print_all (norse_qods) ;
cout<< " \n" ;

print all (qreek_qods ) ;


cout<< " \n" ;
, :
Freia , Odin , hor }
Zeus , Poseidon , Ares , Athena , Hera )
: , in8ert ( )
-, ,
? , ,
9 . 7 . 5.
. .
. ,
.
. Li8t,
. list
20.4.


.

vector.
1 . , -
int, new.
2. cout int.
3. , ( delete [ ] ).
4. print_arraylO (o8tream& 08 , int* ) .
08 ( ).
5. ,
int; 1 00, 1 0 1 , 1 02 .. ;
.
1 7 .1 . this 683

6. ,
int: 1 00, 1 0 1 , 1 02
.. : .
7. print_array (os tream& os , int* , int n ) ,
os ( n ).
8. ,
int: 1 00, 1 0 1 , 1 02 .. ;
.
9. ? ( , .)
1 . 5, 6 8, vector, ,
print_vector ( ) print_array ( ) .
.
print_array ( ) .
1 . int,
7 pl.
2. pl int,
.
3. .
int: l , 2, 4, 8 .. :
2.
4. 2 ,
.
5. int*
2 .
6. pl 2 .
7. 2 .
8. pl 2, ,
.
9. . .
1 0. ,
int: 1 , 2. 4, 8 .. :
pl.
1 1 . ,
int, 2.
1 2. , pl,
, 2 .
1 3. 1 0- 1 2, vector, .
684 1 7

r
1 . ?
2 . .
.
. ? ? -
?
4. ?
5. ? ++ ?
6. , -
? ?
7. 7
8. ?
9. ?
1 . 7
1 1 . ? ?
1 2. ( )?
1 3 . ? ?
14. ?
1 5. ?
16. ? ?
1 7 . ?
1 8 . ?
1 9. this ?

delete
delete [ ]
new
nullptr
this : ->
void* : *
: [ ]
: &


1 7 . 1 0. this 685

1 .
? : .
2 . int, doue l?
, sizeof,
.
3. void to_lower ( char* s ) ,
- s . ,
"Hell o , World ! " "hello , world ! " .
. -
, ,
, ,
.
4. char* strdup ( const char* ) , -
.
.
5. char* findx ( const char* s , const char* ) ,
- s .
6. , , ,
new, .
11 (memory exhaustion). , .
:
,

. .
, ?
7. . cin
, .
, ( ! ).
std : : string .
8. . 7 ,
std : : string, ( string ,
).
9. : ( )
( )? (
)
? , .
1 0 . . 7 .
; . ,
686 1 7

( )? .
, ?
1 1 . 1 7. 1 0. 1 , " ".
1 2. find ( ) ?
1 3 . Link 1 7. 1 0. 1 ,
struct God. string: ,
, . . God{ "" ,
" " , " " , "" } God{ " " , "" , "
'l' " , " r" } . print_
all ( ) . .
- add ordered ( ) . ,
new,
. Link God,
: ()
-
.
1 4. 1 7. 1 0 . 1
: , prev
Link? ?
?
.

.
,
vector? , , -
vector , ,
. ,
,
. ,
. ,
, ,
++ ( ,
). ,
, . .
. .
, ,
,
: ,
"- ".

,
.


.
,
.
,
: ,
,
, .
, ,
.
688 1

1 8. 1 . 1 8.6.
1 8.2. 1 8.6. 1 .
1 8..
1 8.3. 1 . 1 8.6.2.
1 8.3.2. 1 8.6.3.
1 8.3.3. , 1 8.6.4.
1 8.7. :
1 8.3.4. 1 8.7. 1 .
1 8.4. string
1 8.4. 1 . 1 8.7.2.

1 8.4.2.
1 8.7.3.

1 8.5.
1 8.5 . 1 .

1 8. 1 .
,
. ,
. ,
, -
. ,
.
,
-
,
.
, ,
.
.
,
;
;

(, , ..).
,
++ ( ; . 22.2. 5 27).
1 7 vector,
,
1 8.2. 689

"" ,
.
.
, . ,
?
?
? ?
. ?

, vector string,
, . .
. ,
,
++ .
, vector

++.
(string, vector, list, map .)

. .

1 8.2.
vector, 1 7:
class vector {
int sz ; 11
doue* elem; 11
puic :
vector (int s ) 11
: sz { s } ,
elem{new doule [ s ] } //
{ / * . . . */ }
vector ( ) //
{ delete [ ] elem; 11
11 . . .
1;
,
, ?
. :
vector vl = { 1 . 2 , 7 . 89 , 12 . 34 } ;
. ,
. ,
:
690 1 8 r

vector v2 (2) ; //
v2 [ 0 ] 1.2;
v2 [ 1 ] = 7 . 8 9 ;
v2 [ 2 ] = 12 . 34 ;

vl, w" v2
(
). push_ack ( )
.
vector v ; //
v2 . push_back ( l . 2 ) ;
v2 . push_back ( 7 . 89 ) ;
v2 . push_back ( 12 . 34) ;

.
.
? { }
initializer_list<T>
, . .
.
class vector (
int sz ; 11
doue* elem; //
puic :
vector (int s) // (s - )
: sz ( s } , elem( new doule [ s z ] } //
(
for ( int i = ; i<sz ; ++i )
ele [i] = . ; 11

11
vector (initializer_list<doule> lst)
: sz ( lst . size () } ,
elem( new doule [ s z ] } //
(
// s td: : copy () ; ( . 5 . 2)
( lst . eqin ( ) , l st . end ( ) , l) ;
}
11
};
(.5.2).
,
( - initiali zer_list)
,
( - ,
elem) .

1 0.3. 691

vector vl = { 1 , 2 , 3 } ; // 1 . 0 , 2 . 0 , 3 . 0
vector v2 ( 3 ) ; // ( 0 . 0)
.
. - . ,
, :
vector v1 { 3 } ; // 3 . 0
vector v2 ( 3 ) ; // .

, .
. { }
ini tializer_list.
- -
= { } ,

vector vll = { 1 , 2 , 3 } ; // 1 . 0 , 2 . 0 , 3 . 0
vector v12 { 1 , 2 , 3 } ; // 1 . 0 , 2 . 0 , 3 . 0
, .
, ini tializer_list<doule>
. : ini tia
lizer list ,
_

r-" (. .6.4).

1 8..
.
class vector {
int s z ; 11
doue* elem; 11
pulic :
vector ( int s ) 11
: sz { s } ,
elem{ new doule [s] } //
{ / * . . . */ }
vector ( ) //
{ delete [ ] elem; 11
11 . . .
};
.
void f (int n)
{
vector v (3 ) ; //
v . set ( 2 , 2 . 2 ) ; // v [2 ] 2 . 2
vector v2 = v; // ?
11 . . .
692 18

v2 v (.. =
); , v2 . size ( ) =v .
size ( ) v2 [ i ] ==v [ i ] i [ O : v . size ( ) ). ,
f ( ) .
() vector ,
vector. -
vector . ,
, .
. ? ,
. ,
, .
"
-". .
, Point, -
. , ,
. ,
v . sz==v2 . sz v . elem=v2 . elem,
:

. . 2.2

, v2 v;
v. .
v . set ( l , 99) ; // v [ l } 9 9
v2 . set ( 0 , 88 ) ; 11 v2 [ 0 } 88
cout << v . get ( O ) << ' ' << v2 . get ( l ) ;
88 9 9 . ,
. v v2,
,
v [ ] v2 [ 1 ] . ,
"", "" " ",
, , vector. .
f ( ) , .
v v2;
v
delete [ ] elem;
v21
, v v2, elem ,
.
(. 1 7.4.6).
1 .. 693

1 ..1 .
, ? :
,
.
.
, , . ,
, ( constructor)
. ,
(. .
vector : vector ( const vector& ) ; ).
,
vector . ,
( ) ,
. . .
const, (.
8.5.6). , vector.
class vector {
int sz ;
doue* ele111 ;
puic :
vector (const vector&) 11
11 . . .
1;
(sz)
( elem)
.
vector : : vector {const vector& arg)
11 ,
: sz { arg . sz } , ele111 { new doule [arg . sz ] }

copy (arg , arg+sz , ele111) ; 11 s td : : copy () ; . . 5 . 2


.
vector v2 = v;
v2,
vector v.
.
v: 3 1 2.2

v2 : 3 1 2.2
694 1


. , vector
, v,
v2, .
v . set ( l , 99) ; 11 v [ l ] 9 9
v2 . set ( 0 , 88 ) ; 1 1 v2 [ 0 ] 8 8
cout << v . ge t ( O ) << ' ' << v2 . get ( l) ;
.

vector v2 = v;


vector v2 (v) ;

v () v2 ( )
,
, ,
, .

1 8. .2.

, . ,
, -
(. 1 8.3. 1 ) .
.
void f2 ( int n)
(
vector v ( ) ; 11
v . set ( 2 , 2 . 2) ;
vector v2 ( 4 ) ;
v2 = v ; 11 : ?
11 . . .

, v2 v (
vector). vector
.
; , ,
sz elem v2 sz elem v
.
:
7
1 .. 695

,- ---2 .2 -- -
: :::

. .

f2 ( )
, f ( ) 1 8.3 ,
: ,
2
(v v ),
delete [ ] . , ,
v2, .
.
(. 18.3. 1 ) .
.
class vector {
int s z ;
doue* elem;
pulic :
11 :
vector& operator= ( const vector&)
11 . . .
};

vector& vector : : operator= ( const vector& )


11
{
doule* = new doule [ a . sz] ; //
copy (a . elem , a . elem+a . sz , p) ; 11
delete [ ] elem; 11
elem = ; 11
sz = a . sz ;
return *this ; // (. 1 7 . 1 0)

, ,
.
vector.
doule* = new doule [a . sz ] ; //
copy ( a . elem, a . elem+a . sz ,p) ; //
vector:
delete [ ] elem; 11

elem .
elem = ; //
s z = a . sz ;
.
696 1



.----- delete[]

vector ,
.
,
,
, .
, ,
vector .
vector v ( lO) ;
v=v ; 11
,
, .

1 . . . .

. ,
: ( ) ,
(, , ).
(shaHow ) .
,
.
.
I. (deep )
, ,
.
, vector. string . . ,

,
.
.
int* = new int{ 7 7 } ;
int* q = ; //
* = 8 8 ; 11 i n t ,
11 q

.
1 8.3. 697

: q:
( )

.
int* = new int{ 77 } ;
int* q = new int{ *p } ; // i n t ,
11 ,
* = 88 ; // i n t ,
11
.

, .
vector .
,
elem. vector, vector
. ,
. ,
(, ). .
(pointer semantics) u (reference
semantics). . ,
(. s tring vector) . ,
(value semantics), . . . -
. .
,
. . .
".
".

1 . .4 .
,
.
. . .
vector fill (istream& is)
{
vector res ;
698 1 8

for (doue ; is>>x ; ) res . push_back (x) ;


return res ;

void use ( )
{
vector vec = fill ( cin) ;
// . . vec . . .
.

res
use ( ) . res fill ( )
vec .
? !
res .
res fill ( ) .
?
:

res: i 1 00000 :::,..-----


"" res
vec. , , vec
res , .
res
res vec . res
fill ( ) vec. res
( ) - :

1 00 doue
fill ( )
.
++?
:
class vector (
int sz ;
doue* elem;
1 .. 699

puic :
vector (vector&& ) ; 11
vector& operator= (vector&&) ; 11
11 . . .
};
&& rvalue". ,
. ,
, . . (vector&&) ,
(const vector&&) .
, ".
,
. .
vector : : vector (vector&& )
: sz { a . sz ) , l.{ . l.} // elem sz
{
a . sz = ; //
a . elem = nullptr ;

vector& vector : : operator= (vector&& ) //

delete [ ] elem; 11
elem = . l.; // elem sz
sz = a . sz ;
. l. = nullptr ; 11
a . sz = ;
return *this ; 11 (. 1 7 . 1 0)

,
,
. .
vector fill (istream& is)
{
vector res ;
for (doue ; is>>x ; ) res . push_back (x) ;
return res ;


. ,
(res) , ,
.
,

. -
- , .
700 1 8

vector* fi112 (istream& is)

vector* res = new vector ;


for (doule ; is>>x ; ) res->push_back (x) ;
return res ;

void use2 ( )
{
vector* vec = fill ( cin) ;
// . . . vec . . .
delete vec ;

vector.
1 7.4.6, , ,
,
.

1 8.4.
, ,
,
-
. .
.
.
( ).
( ).
(
).
(
).
.

,
, , :
strinq s { " cat . jpq" } ; / / s " t . jpg"
Iaqe ii { Point { 200 , 300 } , // Poi n t ( 2 0 0 , 30 0 ) ,
"cat . jpq" } ; //
// ca t . jpg


. string
,
1 8.4. 701

vector .
( . 9.4.3).
,
. , ,
.
, , ,
.
.
, ?
,
.
, .
vector.
. int, string vector<int> ,
.
vector<doue> vi (lO) ; 11 1 0 dou e ,
// .
vector<strinq> vs (lO) ; // 1 0 s t ring,
11 " "
vector<vector<int>> vvi ( 1 0 ) ; // 1 0 ,
//
// vecto r { }

, .
:
? :
. ,
int doue, ( doue
. ). string
11 11 vector .
, { } . , doue { }
. , string { } " " , vector<int> { } vector,
-

int.
. .
- , "- " ,
. .
new, ,
delete delete [ ] .
vector ;
, . ,
, - ( ,
) . , (

).
702 1

, , -
,
. ,
, .
, ,
.
, (
- , ),
( )
.
vector.
, ,

. ,
( - ,
), (
) , (
, . )
. vector.
, ,
(.
1 7. 5.2).

1 8.4. 1 .
, r,
r . .
.
class l {
puic :
complex (doue) ; // doue complex
complex (doue , doule) ;
11 . . .
};
l z l 3 . 18 ; / / : 3. 18 (3 . 1 8 , 0)
l z2 complex { l . 2 , 3 . 4 ) ;
.
,
. , vector, ,
, int, .,
int vector:
class vector
11 . . .
1 8.4. 703

vector (int) ;
11 . . .
};

vector v = 10 ; // 1 0 d ?
v = 20 ; // v 20 doue
void f ( const vector&) ;
f ( lO) ; // ? f
11 1 0 doue
, , . ,
.
explici t
. -
.
class vector
11 . . .
explicit vector ( int) ;
11 . . .
};

vector v = 10 ; // : i n t vector
v = 20 ; // : i n t vector
vector v0 ( 10 ) ; 11

void f ( const vector&) ;


f ( lO) ; // : i n t vector
f (vector (lO) ) ; 11
, -
- vector
explici t. ,
explici t ; , .
,
explici t.

1 8. 4 .2.

. ,
vector ( 2) ; vector

new.
, .
,
. -
.
704 1 8

,
.
, .
, ;
, ,
delete.
.
; ,
new (
), .
? ,
,
. .
struct { //
int val ;

void out (const strinq& s , int nv)


(
cerr << this << "->" << s << 11 " << val
<< 11 ( " << nv << " ) \n" ;
1
( ) ( out ( "X ( ) " , 0 ) ; val=O ; 1 //
X ( int v) ( val=v; out ( "X ( int) " , v) ; 1
11 I1 :
X ( const & ) ( val=x . val ; out ( "X (X&) " , x . val) ; }
& operator= (const & ) //
(
out ( 11X: : operator= ( ) " , a . val) ;
val=a . val ; return * this ;

- ( out ( "-X ( ) 11 , ) ; //
1;

,
.
qlob ( 2 ) ; //
( ) ( return ;

2 ( ) { = ; return ; 1

& ref_to (X& ) { return ; 1

* ake ( int i) ( a (i ) ; return new ( ) ; }


struct { ; ; } ;
int main ( )
(
1 8.4. 705

loc { 4 ) ; 11
loc2 { loc) ; 11
loc = X{ S ) ; 11
loc2 = copy ( loc) ; 11
loc2 = 2 ( 1) ;
l { ) ;
& r = ref_to (loc) ; 11
delete k ( 7 ) ;
delete k ( ) ;
vector<X> v ( 4 ) ; 11
loc4 ;
* = new { 9 ) ; 11
delete ;
* = new X [ S] ; 11
delete [ ] ;

&
: -
, .
, ,
.


" ". ( ) 2 ( ) .
() . ;
- .
, , -
. ,
,
.
"", .
, , ,
.
( 1 8.3.4).
" "?
, .
. ,
. .
,
, .
, .
. , . ,
706 1 Belfrop

.
,
,
. ,
.
,
,
; " -
" (leak detectors). , , ,
, .

1 8.5.
(. 1 7 .6)
- set ( ) get ( ) .
. : v [ i ] . i
- operator [ ] .
() .
class vector {
int s z ; 1 1
doue* elem; //
pulic :
11 . . .
doule operator [ ] ( int n )
{ return elem [n ] ; } //
};
, , , .
(operator [ ] ( ) ) ,
, .
vector v ( lO) ;
int = v [ 2 ] ; //
v [] = ; // : v [} l va l ue

v [ i ] v . opera
tor [ ] ( i ) , i- v. i
vector v [ ]
, ,
.


vector,
,
v [ ] = ; .
1 8.5. 707

operator [ ]
:
class vector {
int sz ; 11
doue* elem.; //
puic :
11 . . .
doule* operator [ ] ( int n )
{ return &elem [n ] ; ) //
);

:
vector v ( l O ) ;
for ( int i=O ; i<v . size ( ) ; ++i )
11 , -
*v [ i ] = i ;
cout << *v [ i ] ;

v [ i ] v . opera tor [ ] ( i )
i- v. ,
* ,
. , set ( )
get ( ) . ,
.
class vector 1
11 . . .
doule& operator [ ] ( int n )
{ return elem [n] ; } //
};
.
vector v ( lO) ;
for ( int i=O ; i<v . size ( } ; ++i ) { // !
v [i ] = i ; // v { i }
11 i -
cout << v [i ] ;

:
v [ i ] v . opera tor [ ] ( i )
i- v.

1 8.5. 1 .
operator [ ] ( ) , , :
:
708 1 8

void f ( const vector& cv)


{
doue d = cv [ l ] ; // ,
cv [ l ] = 2 . 0 ; // ( )

, - vector : : operator [ ] ( )
vector.
, - ,
. ,
const (. 9.7.4).
.
class vector {
// . . .
doule& operator [ ] ( int n ) ; //
doule operator [ ] ( int n) con s t ; //
};
, doue&
const, doue.
const doue&,
, doue ,
(. 8. 5.6), .
.
void ff ( const vector& cv , vector& v)
{
doule d = cv [ l ] ; // ( [ } )
cv [ 1 ] = 2 . ; // ( [ } )
doule d = v [ l ] ; // ( [ } )
v [l] = 2 . 0 ; // ( [ } )

vector
, operator [ ] ( ) const
.

1 8.6.
(array)
, .
-
. .
(.
).
(
).
1 8.6. 709

( ).
( , , -
).
, ,
vector . s td : : vector
. -

( ),
,
, ,
vector.
? ?
,
-

; ,
.
.
.
const int = 100 ;
int qai [ax] ; // ( 1 0 0 i n t ) ; " "
void f ( int n)

char lac [ 20 ] ; // ; " "


//
int lai [ 60 ] ;
doue lad [ n ] ; // :
11 . . .

:
. ,
,
.
vector .
, ,

( [ ] ):
void f2 ( )
{
char lac [ 2 0 ] ; // ; " "
//
lac [ 7 ] '' ;
*lac '' ; 11 l a c [ O} = ' b '
lac [ - 2 ] ' ' ; // ? ?
lac [ 2 0 0 ] ' ' ; 11 ? ?
710 1

, , ,
. [ ] ,
,
. f2 ( ) , lac [-2]
lac [ 2 0 0 ] (
). .
.
, "
" .
- , lac
, lac [200 ] -
? , , , , -
. ,

, (, )
.

1 8.6. 1 .
. .
doue ad [ lO ] ;
doue* = &ad [S ] ; // a d [ 5 ]

doule, ad [ 5 ] .

ad :

.
* = 7 ;
[2 ] = ;
[ -3 ] = 9 ;

ad : 7 6
1 8.6. 711

,
, . ,
,
.
( ,
; . 1 7 . 4.3). ,
( ) .
,

.
+= 2 ; //

ad : 6


-= 5; //

ad : 9 7 6

+, -. +=
-=

(pointer arithmetic). ,
, , -
.
+= 1000 ; // : ,
11 1 0
doule d = * ; // :
11 ( )
* = 12 . 34 ; // : ,
11

, ,
, .
.
71 2 1 8


++
--
. ,
ad :
for (doule* = &ad [ O ] ; p<&ad [ lO ] ; ++) cout << * << ' \n ' ;
:
for (doule* = &ad [ 9 ] ; p>=&ad [ O ] ; --) cout << * << ' \n ' ;


. , ("")
. &ad [ 9 ] , &ad [ l O ] ? >=. >?
( ),
. ,
vector,
.
,
.
,
, .
.
++ ?
,
:
doue* pl = &ad [ O ] ;
doue* 2 = 1+7 ;
doue* = &1 [ 7 ] ;
if (2 ! = ) cout << ":110 ! \n" ;

.
.
,
, . ,

, .

1 8.6.2.
:
char ch [ lOO J ;

ch, . . sizeof ( ch ) , 1 00.


:
1 8.6. 71 3

char* = ch ;

&ch [ ] , si zeof ()
- 4 ( ,
1 001).
. ,
strlen ( ) , ,
.
int strlen ( const char* ) //
11 s t rlen ()

int count = ;
while ( *) { ++coun t ; ++ ; }
return coun t ;

strlen ( ch) ,
strlen ( &ch [ ] ) . , ,
, .
,
,
. .
int strlen ( const char [ ] ) //
11 s trlen ()

int count = ;
while ( [count] ) { ++coun t ; }
return coun t ;

char lots ( 100000 ] ;

void f ( )
{
int nchar strlen ( lots) ;
11 . . .

( ) ,
1 00 . ,
strlen ( ) , .
char [ ] char*p.
strlen ( lots) s trlen ( &lots [ ] ) .
-

, .
? . ,
, ( .
8.5.3-8. 5.6), .
71 4 1 8

, ,
, ,
.
char ac [ lO ] ;
= new char [ 20 ] ; // : -
11
& [ ] = new char [ 2 0 ] ; // :
//

- , .

.
int [ 1 0 0 ] ;
int [ 1 0 0 ] ;
11 . . .
= ; 11
int z [ l O O ] = ; 11

, . .
. :
for ( int i=O ; i<lOO ; ++i )
x [ i ] =y [i ] ; 11 1 0 0 in t
me.cpy ( x , y , lOO*sizeof ( int) ) ; 11 l OO * s i zeof (in t )
copy (y , y+lOO , x) ; // 1 0 0 in t

,
. ,
++, ( - 27. 1 .2).
, ( , ;
27.5) .
,
vector .
vector, ,
:
vector<int> x ( lOO ) ;
vector<int> ( 10 0 ) ;
11 . . .
= ; 11 1 0 0 i n t

1 8.6..
char
, :
char [ ] = "eorn" ; //
1 8.6. 71 5

. ,

! lo r
.
.

: l l ln 1 1
, ,
. ,
, (C-style string)
-.
.
char* = "Howdy " ; //
//

10 1... 1 1
:

11
char, , - ' '
- .
- . :
. ,
:
int strlen ( const char* ) // s t rlen ()

int n = ;
while (p [n] ) ++n ;
return n ;

s trlen ( ) ,
,
<string . h> ( 2 7 . 5 . 1 1 .3). ,
s trlen ( ) .
; .
+ l char.

,
, .
.
int ai [ ] = { 1 , 2, 3, 4 , 5, 6 } ; // i n t
int ai2 [ 10 0 ] = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ) ; // 9 0
11
71 6 1 8

doue ad [ lO O ] = { } ; //
char chars [ ] = { ' ' , '' , '' } ; // !

, ai
( ), chars (
), - " "
. ,
( ).
,
( ai2 ad),
,
.

1 8.6.4.
, .
, . ,
, ,
, ,
, .
. .
.
.
.
.
, .
,
, ,
;
. ( ),
, , .
, . .
.
int* = nullptr ;
* = 7 ; // !
, ,
- .

.
, , ,
:
1 8.6. 71 7

int* = fct that can return a nullptr ( ) ;


_ _ _ _ _
if ( == nullptr) {
11 -

else {
11
* = 7 ;

void fct that can receive a nullptr ( int* )


_ _

if ( = = nullptr) {
11 -

else {
11
* = 7 ;

, ,
, (. 1 7.9. 1 ) ,
- (. 5.6 1 9. 5).
.
int* ;
* = 9 ; // !

, ,
.
.
int a [ lO ] ;
int* = &a [ l O ] ;
* = 1 1 ; / / !
a [ l O ] = 12 ; // !



. vector.
(
),
.
.
int* = new int ( 7 ) ;
11 . . .
delete ;
11 . . .
* = 13 ; !! !
71 8 1

delete . ,
* .

.
"" new, " "
delete: new delete
. Vector_ref (
.4).
.
int* f ( )
{
int = 7 ;
11 . . .
return & ;

11

int* = f ( ) ;
11 . . .
* = 15 ; // !

f ( ) . ,
* .
. , .

. . ,
( . 1 7.5. 1 ) .
,
, .
.
vector& ff ( )
{
vector ( 7 ) ; // 7
11 . . .
return ;
11

11

vector& = ff ( ) ;
11 . . .
[ 4 ] = 15 ; // !


.
.
.
1 8.7. : 71 9

,
. -
, , new delete.
, " " -
. , RAII (WResource Acqui
sition ls Initialization" w ": .
-

1 9. 5),
.

1 8. 7. :
!
. (palindrome) - ,
. , r petep
malaualam , ida homesick - .
, .
, ,
.
, , - -
.
.
.
. ,
- , -
,
.

1 8. 7 . 1 . s tring
,
string ,
.
l is_yalindrome ( const string& s )
1
int first = ; //
int last = s . length ( ) -1 ; //
while ( first < last) { //
if (s [ first] ! = s [ last] ) return fal se ;
++first ; //
--last ; //

return true ;
720 1 8

true, ,
.
, , ,
,

. , ,
, ,
.
is_palindrome ( ) .
int main ( )
{
for ( s trinq s ; cin>>s ; ) {
cout << s << 11 i s 11 ;
if ( ! i s_yalindrome ( s ) ) cout << 11 not11 ;
cout << 11 palindrome\n 11 ;

, , string,
, s tring .
,
, . is _

palindrome ( ) , ,
getline ( ) (. 1 1 . 5).
, ah ha as dJ.fd sa
.

1 . 7 .2.
string ( vector)
?
bool i s_yalindrome ( const char s [ ] , int n)
// s

int first = ; 11
int last = n-1 ; //
while ( first < last) //
if ( s [first] ! =s [ last] ) return false ;
++first ; //
--last ; //

return true ;

is_palindrome ( )
. (
) :
1 8.7. : 721

istream& read_word ( i stream& is , char* buffer , int )


// -1 buffer

is . width (max) ; 11 >>


// -1
is >> buffer ; // ,
11 ,
return i s ;

istream
.v >>. ,
, , -
- (..
). , width ( )
?
string vector, ,
.
, (-
) .
read word ( ) , .v :
int main ( )

constexpr int = 128 ;


for ( char s [max] ; read word ( cin , s , max) ; )
_
cout << s << 1 1 i s 11 ;
if ( ! isyalindroe ( s , strlen ( s ) ) ) cout << 11 not 11 ;
cout << 11 palindroe\n11 ;

strlen ( s )
read_word ( ) , cout<<s
, .
string ,
, ,
(. . 1 ).

1 8. 7
. .


.
bool isyalindroe ( const char* firs t , const char* last)
// fi rs t
11 l a s t

while ( first < last) { //


722 1

if ( *firs t ! =*last) return false ;


++firs t ; / /
--last; //

return true ;

,
. ,
, - -
. ,
,
. , .
is_palindrome () , ,
:
int main ( )

const int = 128 ;


for ( char s [max] ; read_word ( cin , s , max) ; ) {
cout << s << 11 is11 ;
if ( ! is_Palindrome { &s [ O ] , &s [ strlen ( s ) -1] ) )
cout << 11 not11 ;
cout << 11 palindrome\n11 ;

is_palindrome ( ) :
bool is_Palindrome (const char* first , const char* last)
// fi rs t
// l a s t

if (first<last) {
if ( *first ! =*last) return false ;
return is_Palindrome (first+l , last-1 ) ;

return true ;

,
; ,
,
, .
1 8.7. : 723

-../
:
, - .
, .

l . ga int,
l , 2, 4, 8. 1 6 ..
2. f ( )
.

int int,
.
3. f ( ) .
la int,
.
ga la.
la.
int
,
, ,
.
,
, .
,
.
, .
4. main ( ) .
f ( ) ga.
, ,
(.. 1 ,
2* 1 , 3*2* 1 , 4*3*2* 1 ..).
f ( ) .

1 . vector<int> gv;
1 , 2, 4, 8, 1 6 ..
2. f ( ) , vector<int>.
3. f ( ) .
724 1

vector<int> lv -
, . r .
qv lv.
lv.
vector<int> lv2;
. ,
.
lv2 .
4. main ( ) ,.
f ( ) r qv.

vector<int> vv
( 1 . 2* 1 , 3*2* 1 , 432 1 ..).
f ( ) r vv .


1 . " !"?
2. ?
3. -
, ?
4. ?
5. ?
6. ,
?
7. ? ?
8. vector ?
9. .
1 0. explici t?
, ?
1 1 . r ?
1 2. ?
1 3 . ?
1 4 . ?
1 5.
?
1 6. , -?
1 7. ?
1 8.7. : 725

explici t


l . char* s trdup ( cons t char* ) , -


,
. .
,
*
2 . char*findx ( const char* s , const char*x) ,
- - s .
. ,
* .
3. int strcmp (const char*s l , const char* s2 ) ,
- .
sl s2,
, - , sl
s2 - .
. ,
* .
4 . , strdup ( ) , findx ( ) strcmp ( )
. -? !
, char* ,
, , (
- - ;
).
-" .
, .
,
-
.
-.
5. string cat_dot ( const string&sl , const string&s2) ,
.
, t_dt ( "" , "" ) . .
6. cat_dot ( ) ,
,
( ).
726 1

7. t_dot ( ) ,
- -
, .
.
. , ,
new,
delete. , . 5 6.
8. 1 8. 7 ,
: . " home " ,
" emoh" ,
, home - .
9. ,
1 7.4. . ,
, .
: ?
, .
- ?
1 0. 1 8. 7 . 2
. ,
: 1 ) ,
: 2)
. .
1 1 . , (skip list) ,
. !
1 2 . "Hu nt the Wumpus" (" "
""). ( ) ,
(Gregory ). -
,
. - .
, :
. ,
. ,
.
.
, . .
.
" " , .
" " , .
" " ,
.
1 .7. : 727

.
. ,
, " 1 2;
1 , 1 3 4: ?"
ml (" 1 3") sl-4-3 ("
1 3, 4 3").
. .
,
, , ,
.
,
,
. ,
(, randint ( ) std_lib_facilities . h) ,

. r1:
.


vector
, .
- ,
. ,
,
.
,

.YU1.U "' . .
- ,,. ("



SL: vector .
.
.
.
. .
.
.
.
vector . .
, :
. ,

.
. ,
,
.
.
730 1 9 ,

1 9. 1 . 1 9. .6.
1 9. 2 . 1 9..7. vector
1 9.2. 1 . 1 9.4.
1 9.2.2. reserve capacity 1 9.4. 1 . :
1 9.2.. resize
1 9.2.4. push_back 1 9.4.2.
1 9.2.5.
1 9.2.6. 1 9.5.
1 9.. 1 9.5 . 1 .

1 9.. 1 .
1 9.5.2. - -
1 9..2. -
1 9.5..
1 9 .... 1 9.5.4. unique_ptr
1 9..4. 1 9.5.5.
1 9..5.
1 9.5.6. RAll vector

1 9 . 1 .
1 8 vector ,
.
vector,

.
vector
.
, vector,
.
vector,
( ,
).
. ,
(
vector) . .
vector (
)?
,
?
1 9. 1 . 731

vector
?
, vector ,
:
vector<doule> vd; // doue
for (doule d; cin>>d ; )
vd .push_back (d) ; //

vector<char> vc (lOO) ; // cha r


int n ;
cin >> n ;
vc . resize (n) ; 11 vc

, .
?
-
?
. - vector,
:
;
.

. .
, ,
.
,
: . ;
.
. , :
. .
( vector ),
: . ,
. . , ,
- .
: ,
, ,
, . ,
vector<Numer>.
,
. ,
, .. .
push_back ( ) , resize ( )
? , ,
732 19 .

:

,
. ,
,
, .
11 , push_ba ck :
vector<doue>* = new vector<doule> ( lO ) ;
int n = ; 11
for (doule d; cin >> d; ) {
if {n==p->size ( ) ) {
vector<doule>* q = new vector<doule> (p->size ( ) *2) ;
copy (p->begin ( ) , p->end ( ) , q->egin ( ) ) ;
delete ;
= q;

( *) [n] = d;
++n ;

. , ?
? ,
.
, .

(; . 1 8. 6). ,
, vector,
; , , vector
,
. ,
. .
, :
vector<doule> vd;
for (doue d; cin>>d; ) vd. push_back (d) ;

? ,
;
.
.
-
. (
, )
. , ,
, , .
1 9. 1 . 733

,
?
? -
. ,
. .
. (
)
(
).
,
. , ,
. ( ), , ,
, . ,
.. .
. ,
,
, .
?
,
. .
; vector.

. , ,
. "
-
. .
.
vector s tring (string
). list,
map (map ) .
,
.
. ,

.

.
.
.
.
734 1 9 .

1 9 . 2.
vector
? . .
, vector:
vector<doule> v (n) ; // v . s i ze () ==n

.
v . resize ( l O ) ; // v 1 0
v. push_back ( 7 ) ; // 7 v;
11 v . s i ze ()
v = v2 ; // ; v
// v2 v . s i ze ()== v2 . si ze ()

vector .
, erase ( ) insert ( ) (
.4. 7), ,
.

1 9 .2. 1 .
1 9. 1
:
.
. ,
, , , .
.
push_back ( ) .
, . .
. vector
, " ".
". ". .
class vector (
int sz ; //
doule* elem; //
int space ; // "
// "! ""
11 (I1 )
pulic :
11 . . .
};

. .
1 9.2. 735


sz: : \ s z:
( )

elem:
-!---- - -_-_-_-_-_-_-_-_] 1. J
space:
( ) -

, ,
sz ( ) ,
, space .
. ,
elem+sz elem+space.
, space sz. ..
" " .

sz: : \ sz:

elem:
space:
------- q-____- - - -:... '
- - - - - - -
,'

.
. ,
space=sz . , push_back ( ) ,
.
( vector
) ,
- nullptr:
vector : : vector ( ) : sz { O } , elem{ nullptr } , space { O } { }
.

, '

sz:


J
elem :
_ _ _ _ _

space : - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - '
:
' ,

.

(. . 1 6).
736 n 19 ,

vector .
( ),

. , std :
: vector .

1 9 .2.2. reserve capaci ty



(.. )
vector : : reserve ( ) . .
void vector : : reserve (int newalloc)
{
if (newalloc<=space) return ; //
doule* =
new doule [newalloc] ; //
for ( int i=O ; i<sz ; ++i )
p [ i ] = elem [ i ] ; //
delete [ ] elem; //
elem = ;
space = newalloc ;

,
. ,
- push back ( ) resize ( ) .

,
vector, , .
-, .
int vector : : capacity ( ) const { return space ; }

, vector v v .
ci ty ( ) -v . size ( ) ,
v push_back ( )
.

1 9.2.. resize
reserve ( ) , resize ( )
vector .
.
.
,
.
1 9 .2. 737

.
.
, .
void vector : : resize (int newsize)
// , news i z e
// .

reserve (newsize) ;
for ( int i=sz ; i<newsize ; ++i)
elem [i] = ; //
sz = newsize ;

reserve ( ) .

( ).
, ,
, . , .


( ),
, resize ( )
? newsize newsize = -77? =

1 9.2.. push_back
push back ( )
_

, reserve ( ) .
void vector : : push_back (doule d)
11 ;
11 d

if ( space=O)
reserve ( B ) ; // 8
else if ( sz==space)
reserve (2*space ) ; //
elem [sz] = d; // d
++sz ; 11

, ,
.
,
vector .
738 1 9 . n

1 9 2 5
. . .

.
, ,
. 1 8.3.2 ,

: vl=v2 vl
v2 . .

V1 :

sz :
- - - - - - - - - - -
'
2 '

- - - - - - - - - - J
V2:

sz :
- - - - - - - - - - - - - - - - - - "
'

.___.
s ___,__
1_,__
_, _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _;

, ,
? !
vector , ,
, .

V1 :

5 6 7
V2:

sz:
- - - - - - - - - - - - - - - - - - -
'

._ __,_
5 6 7
....__.___
'

_ _.__
_, _ - - - - - - - - - - - - - - - - - .l

:
;
;
;
sz, elem space .
:
vector& vector : : operator= ( const vector& )
11 ,
11
1 9.2. 739

doue* =
new doule [a . sz ] ; //
for (int i = ; i<a . sz ; ++i)
p [ i ] = a . elem [ i ] ; 11
delete [ ] elem; //
space = sz = a . sz ; //
elem = ; //
return *this ; 11


. * this
1 7. 1 .
, , ,
,
. ,
? ,
?
.
, .
vector& vector : : operator= ( const vector& )
{
if ( this==&a) return * this ; 11 ,

if ( a . sz<=space) // ,
//
for (int i = ; i<a . s z ; ++i )
elem [ i] = a . elem [i ] ; //
sz = a . s z ;
return * this ;

doue* = new doule [a . sz ] ; //


for (int i = ; i<a . sz ; ++i)
p [ i ] = a . elem[i] ; 11
delete [ ] elem; 11
space = sz = a . sz ; 11
elem = ; 11
11
return *this ; 11

(
, v=v); .
,
. this,
, ,
740 1 9 ,

, - (.. operator= ( ) ) .

, , .
a . sz<=space .
,
.

1 9 2 6
. . .

, vector doule.
11 dou e
class vector {
/*
:
O<=n<sz elem [ n } -
s z <=spa ce;
sz <spa ce, elem [sz-1 }
(spa ce-sz) doue
*/
int sz ; //
doue* elem; // ( 0)
int space ; //
11
pulic :
vector ( ) sz { O } , elem{ nullptr } , space { O } { }
explicit vector (int s) : s z { s } , elem{ new doule [ s ] } , space { s }
{
for ( int i=O ; i<sz ; ++i ) 11
elem [ i ] =O ; 11

11 :
vector (const vector&) ;
vector& operator= ( const vector&) ;

// :
vector (const vector&&) ;
vector& operator= ( const vector&&) ;

vector ( ) { delete [ ] elem; } 11


doule& operator [ ] (int n) ( return elem [n] ; } //
const doule& operator [ ] ( int n) const { return elem [n] ; }

int size ( ) const { return sz ;


int capacity ( ) const { return space ;

void resize (int newsize ) ; //


void push_back (doule d) ;
void reserve (int newalloc) ;
};
1 9.. 741

,
( 1 8.4): . ,
. . (
[ ] ). (size ( ) capaci ty ( ) ).
(resize ( ) , push_back ( ) reserve ( ) ).

1 9 . .
. doue:
, :
vector<doue>
vector<int>
vector<onth>
vector<Window*> // Window
vector< vector<Record>> // Record
vector<char>

.
.
. -
, ,
. ,
, ,
. vector sort ( ) ( 2 1 . 1 . 5.4) .
, . , .
.
. , 2 1 22
,
, 24 ,
.
. (template) - ,

. .
.

19 1
. . .

, vector.
vector doue . -
, "", doule, int, s tring,
vector<Record> Window*. ++
. . template
<typename >. " ", :
742 1 9 ,

11
teplate<typename > class vector {
// " "
1 1 ( , )
int sz ; 11
* elem; //
int space ; // +
puic :
vector ( ) : sz { O } , elem{nullptr } , space { O } { }
explicit vector ( int s ) : sz { s ) , elem{ new T [ s ] } , space { s )
{
for ( int i=O ; i<sz ; ++i ) //
elem [ i ] = O ; 11

11 :
vector ( const vector&) ;
vector& operator= ( const vector&) ;

11 :
vector ( const vector&& ) ;
vector& operator= ( const vector&&) ;
vector ( ) { delete ( ] elem; } //

& operator [ ] ( int n) { return elem [n] ; ) //


const & operator [ ] ( int n) const { return elem [n] ; }
int size ( ) const { return sz ;
int capacity ( ) const { return space ;
void resize ( int newsize ) ; 11
void push_back ( const & d) ;
void reserve ( int newalloc) ;
};

vector vector,
doue (. 1 9.2.6),
, doue
. vector :
vector<doue> vd; // - d
vector<int> vi ; 11 - in t
vector<doue*> vpd; 11 - doul e *
vector<vector<int>> vvi ; 11 - vector<T>, - in t

,
( ),
. , -
vector<char>.
:
1 9.. 743

class vector_char {
int sz ; 11
char* elem; //
int space ; // +
puic :
vector_char ( ) : sz { O } , elem{ nullptr} , space { O } { }

explicit vector_char ( int s ) : sz { s ) , elem{ new char [ s ] } , space { s }


{
for (int i=O ; i<sz ; ++i ) 11
elem [i ] =O ; 11

11 :
vector_char ( const vector_char&) ;
vector_char& operator= ( const vector_char&) ;

11 :
vector_char ( const vector_char&&) ;
vector_char& operator= ( const vector_char&&) ;
vector_char ( ) { delete [ ] elem; } //

char& operator [ ] { int n) { return elem[n] ; } //


const char& operator [ ] ( int n) const { return elem [n] ; }

int size ( ) const { return sz ;


int capacity ( ) const { return space ;

void resize (int newsize ) ; 11


void push_back ( const char& d) ;
void reserve ( int newalloc) ;
};

vector<doule> vector,
doule, 1 9.2.6 (
, vector<doule>) .
(type generator).
()
(specialization)
(template instantiation). , vector
<char> vector<Poly_line*> vector.
, vector,
.
.
,
. (
)
, .
744 1 9 .

, -
, :
void fct (vector<strinq>& v)
{
int n = v . size ( ) ;
v . push_back ( "Norah " ) ;
11 . . .

-
. , v . push_
back ( "Norah " ) ,

void vector<strinq> : : push_back ( const strinq& d) { / * . . . */ }


template<typename >
void vector<T> : : push_back ( const & d) { / * . . . */ } ;
, v . push_back ( "Norah " )
. ,
,
.
template<typename >
template<class >. ,

typename, . " ", ,
, int,
". , class
"", .
, class .

1 9 2
. . .

-
++. ,
" ++ " -
-

". . ,
.

. ,
, .
, "
". .
1 9.. 745

.
-

. ,

.
. .
(
). 20 2 1 ,
.
, (class template).
(parameterized
type) (parameterized class).
. (func
tion template).
(parameterized function). (algorithm).
wr
" (algorithm-
oriented programming);
. .

.
.
,
.
.
,
(parametric polymorphism) - -

(ad hoc polymorphism, .
- ; .
1 4.3- 1 4.4). ,
(polymorphism). ,

. -
w ",
, .
, Shape, 1 6- 1 9,
( Text, Circle Polygon)
, Shape.
vector. (.
vector<int>. vector<doue> vector<Shape* >)
, vector.
746 1 9 .

-
( )
( ).
,
, -

1 .
v . push_back (x) ; // v
s . draw ( ) ; / / s

v . push_back () v
r push back ( ) , s . draw ( )
( ,
s; . 1 4.3. 1 ) draw ( ) .
- ,
,
,
.
.
,
.
- 11
.
.
.
.

void draw_all (vector<Shape*>& v)


{
for (int i=O ; i<v . size ( ) ; ++i) v [ i ] ->draw ( ) ;

(draw ( ) )

Shape - , , - .
Shape* vector,
, ,
() .
. r
?
.

, . . . -
1 9.. 747

.
(.
;
24 25).
.
(. ++:
20 2 1 ).

1 9 . . .
.
. . .
. . .
.
w"
( ) ().
;
.
. .
. , "
" . ,
. .
.
. . .
- .
.
.
.
.
:
. .

.
vector: -
, .
.
.
.
,
++. 20 2 1 ,
.
.
748 1 9 .

++ 1 4
. . ++ 1 1
template<typename > //
class vector {
11 . . .
};
.
. . ,
"" , . .

(concept). -
- , . ,
, .
, ( )
. .
.
Element. ++ 14 :
template<typename > //
requires Element<T> ( ) // , El emen t
class vector {
11 . . .
};
,
. .. ,
(constexpr) . true, (
) , (
Element) , fal se. . ,
:
template<Element > // , Elemen t<T> () t rue
class vector {
11 . . .
};
, ++ 1 4
.
:
template<typename l> // El emen t <El em> ()
class vector {
// . . .
};

.

,
1 9.3. 749

.
.
Element<E> ( ) : .
Container<C> ( ) : Element
lbegin ( ) :end ( ) ).
Forward_i tera tor<For> ( ) : For
lb:e) ( ,
).
Input_iterator<In> ( ) : In
I:) ( ) .
Output_iterator<Out> ( ) :
Out.
Random_access_iterator<Ran> ( ) : Ran
!.)
[ ] .
Allocator<A> ( ) :
( ).
Equal_comparae<T> ( ) :
== .
Equal_comparae<T , U> ( ) : u
= .
Predica te<P , > ( ) :
.
Binary_predicate<P , > ( ) :
.
Binary_predicate<P , T , U> ( ) :
u .
Less_comparale<L , > ( ) : L
, ,
< .
Less _comparae<L , , U> ( ) : L
u , ,
< .
Binary_operation<B , T , U> ( ) :
.
Binary_operation< , T , U> ( ) :
u.
Numer<N> ( ) : N , +, -. * / .
750 1 9 .

(
) . ,
20 2 1 ,
.
, . (
Value_type<T>) , .
Value_type<T> - : : value_type; . vector
list ( 20.5).

1 9 . .4.
-
, ,
:
.
.
vector<Shape> vs ;
vector<Circle> vc ;
vs = vc ; // : vec tor<Shape>
void f (vector<Shape>&) ;
f (vc) ; 11 : vec tor<Shape>

? " , - , -
Circle Shape!" , .
Circle* Shape* , Circle&
Shape&, Shape,
, ,
Circle Shape,
(. 1 4.2.4). , ..
,
"" ("slicing"), (. 3.9.2).
- .
vector<Shape*> vps ;
vector<Circle*> vpc ;
vps = vpc ; // : vec tor<Shape *>
void f (vector<Shape*>&) ;
f (vpc) ; // : vec tor<Shape *>

. ? ,
f ( ) .
void f (vector<Shape*>& v)
{
v . push_back (new Rectanqle{ Point { O , O } , Point{ l00 , 10 0 ) } ) ;
1 9.. 751

, Rectangle* vec
tor<Shape*>. , vector<Shape*>
- vector<Cir
cle*>. . ,
. ,
Rectangle* vpc? -
. .
-
. .
. "D " , "C<D>
- <>" -

. -
. (.
25.4.4.)

19 . .5.
, -
. "
". ? .
,
. -
, ++

.

: ,
.
template<typename , int N> struct array {
elem [N] ; // - ,
// ,
11

& operator [ ] (int n) ; // :


const & operator [ ] ( int n) cons t ;
* data ( ) { return elem; } / / *
const * data ( ) const { return elem; }

int size ( ) const { return N ; }


};
array (. 20. 7) :
array<int , 256> qb ; // 2 5 6
array<doule , > ad = { . , 1 . 1 , 2 . 2 , 3 . 3 , 4 . 4 , 5 . 5 } ;
const int m.ax = 1024 ;
752 1 9 ,

void some_fct ( int n)

array<char , ax> loc ;


array<char , n> oops ; // :
11 . . .
array<char , .ax> loc2 = loc ; //
11 " .
loc = loc2 ; 11
// " .

. array -
. vector: .
vector? - w".
array ,
( , gb)
( , loc). .
. (
, N).
,
(, ),
. ,
.
/ .
(. 25). array
vector
( ).
- "
vector?". W
?" 1 8.6, :
,
: array. vector.
:
doue* = ad ; // :
doue* q = ad . data ( ) ; 11 ok :

template<typename > void printout ( const & ) //


{
for (int i = ; i<c . si ze ( ) ; ++i) cout << c [ i ] << ' \n ' ;

printout ( ) array. vector.


printout (ad) ; // a rray
vector<int> vi ;
11 . . .
printout (vi ) ; // vec tor
1 9.. 753

,
r . , array,
vector ( size ( )
).
20 2 1 .

1 9 . .6.
,
:
array<char , 1024> buf; // buf - cha r , N == 1 02 4
array<doule , 10> 2 ; // - d , N == 1 0


. .

teplate<typename , int N> void fill (array<T ,N>& , const & val)
{
for { int i = ; i<N; ++i ) b [ i ] = val ;

void f ( )
{
fill (buf , ' x ' ) ; 11 fi l l () - cha r , N == 1 02 4 ,
// b u f
fill (b2 , 0 . 0 ) ; // fi l l ()
- d , N = = 1 0 ,
//

fill (buf , ' ' )


fill<char , 1024> (buf , ' ' ) , fill (2 , )
-

fill<doue , 1 0> (2 , 0 ) , , .
. .

1 9..7. vector
vector "vector
doue" "vector ",
push_back ( ) , resize ( ) reserve ( ) .
, 1 9.2.2 1 9.2.3
,
doue, ,
.
vector<X>,
?
754 1 9 .

,
?
?
" ,
" " -
, ". ,
,
,
.
,
.
vector, .
,
,
.
template<typename >
void vector<T> : : resize (int newsize , def = () ) ;
, ,
( ) , .
.
vector<doule> vl ;
vl . resize ( lOO) ; 11 1 00 doul e () , . . .
vl . resize (200 , 0 . 0) ; // 2 0 0 . .
-

// JJ
vl . resize (OO , 1 . 0 ) ; // 300 1 . 0
struct No_default {
No_default ( int) ; // No defa u l t
11 . . .
};
vector<No_default> v2 ( 10 ) ; 11 : 1 0
// No_ defa ul t ()
vector<No_default> v ;
v . resize ( lOO , No_defaul t ( 2 ) ) ; // 1 0 0
11 No_ defa u l t (2)
v3 . resize (200) ; // : 200
11 No_defa u l t ()

, , .
:
, -
.
, .
vector ,
vector .
1 9.. 755

-,
. ,
allocator,
.
.
template<typename > class allocator {
pulic :
11
* allocate ( int n ) ; 11
11
void deallocate (T* , // ,
int n ) ; 11

void construct (T* ,


const & v) ; 11
11 v
void destroy (* ) ; //
1;
,
h ++ Programming Language (<memory> . 1 . 1 )
++.
, .
,
.
.

.
, -
.
, allocator - ,
vector<T> : : reserve ( ) . ,
vector . .
template<typename , typename = allocator<T>> class vector
alloc ; // ,
11 . . .
1;
, new,
vector .
vector
, , vector
.
vector ,
756 1 9 .

, ,
,
. ,
, - ,
, , vector<T> : : reserve ( ) .
template<typena.e , typena.e >
void vector<T , A> : : reserve ( int newalloc)
{
if (newalloc<=space) return ; //
* = alloc . allocate (newalloc ) ; 11
// :
for ( int i=O ; i<sz ; ++i ) alloc . construct ( &p [ i ] , elem [ i ] ) ;
11 :
for ( int i=O ; i<sz ; ++i ) alloc . destroy ( &elem [ i ] ) ;
alloc . deallocate (elem, space ) ; //
elem = ;
space = newalloc ;

,
, .
, , strinq,
,
.
reserve ( ) , vector<T , > : : push_back ( )
.
template<typena.e , typena.e >
void vector<T , A> : : push_back ( const & val )
{
if ( space==O ) reserve ( B ) ; 11 8
else if ( sz==space)
reserve ( 2 * space ) ; //
alloc . construct ( &elem [ s z ] , va l ) ; //
11 val
++sz ; 11

vector<T , > : : resize ( ) .


template<typena.e , typename >
void vector<T , A> : : resize ( int newsize , val = ( ) )
{
reserve (newsize) ;
11 :
for ( int i=sz ; i<newsize ; ++i ) alloc . construct ( &elem [ i ] , val) ;
11 :
for ( int i = newsize ; i<sz ; ++i ) alloc . destroy ( &elem [ i ] ) ;
sz = newsize ;
1 9.. 757

, ,
.
.
- w "
. -.
w ".
-
.
. , .

1 9.4.
vector
( ?).
.
operator [ ]
template<typename , typename >
& vector<T , A> : : operator [ ] ( int n )
{
return elem [ n ] ;

:
vector<int> v ( lO O ) ;
v [-200 ] = v [ 2 0 0 ] ; // !
int i ;
cin>>i ;
v [i] = 999; 1 1

, ,
vector.
! .
vector, .
- t ( ) .

struct out_of_range / * . . . */ } ; //
//

template<typenae , typename = allocator<T>> class vector


11 . . .
& at ( int n) ; 11
const & at ( int n) const ; //

& operator [ J ( int n) ; 11


const & operator [ ] ( int n) cons t ; //
11 . . .
};
758 19 .

teplate<typename , typename > & vector<T , A> : : at ( int n )


{
if (n<O 1 1 sz<=n) throw out_of_range ( ) ;
return elem [n] ;

teplate<typename , typename >


& vector<T , A> : : operator [ ] ( int n) 11
{
return elem [n] ;

.
void print_some (vector<int>& v)
{
int i = -1 ;
while ( cin>>i && i ! =- 1 )
try {
cout << "v [ " << i << " ] ==" << v . at ( i ) << 11 \n" ;

catch ( out_of_range)
cout << " : " << i << 11\n" ;

t ( ) ,
,
out of range .
_ _


[ ] , , ,
at ( ) , .

1 9 4 1 :
. . .

,
opera tor [ ] ( ) ? ,
, vector at ( )
operator [ ] ( ) .
. .
1 . .

, ++ .
2. .

,

1 9.4. 759

,
, . .
3. . .
4. . ,
vector, .
. ,
.

1 9 .4. 1 . 1 .
, . ,
,
, .
, ,
, .
, . .
. , ,

. .
.
. , ,
vector ,
++, ,
, ( )
.
.

1 9 .4.1 .2.
. ,

, .

. -
. ,
vector
.

1 9 .4.1 ..
, ,
.
,
.
760 1 9 . n

(. 25.2. 1 ).
.

1 9 .4.1 .4.
ISO ++ ,
,
.
.
, vector ,
,
vector
. .
. ,
,
.

1 9 4 2
. . .

vector,
vector
( [ ] ),
at ( ) , .
std : : out_of_range?
4 1 9.4. 1 : vector

[ ] , ,
. ,
, Vector,
[ ] .
.
struct Range_error : out_of_range //
//
int index ;
Range_error ( int i ) : out_of_range { "Range error " } , index { i } { }
};

template<typena.me > struct Vector : puic s td : : vector<T> {


using size_type = typename std : : vector<T> : : si ze_type ;
using vector<T> : : vector ; //
// vec t or<T> ( 2 0 . 5)

& operator [ ] ( size_type i ) 11 a t (i ) ;


{
if ( i<O 1 1 this->size ( ) <=i ) throw Range_error ( i ) ;
return std : : vector<T> : : operator [ ] ( i ) ;
1 9 .4. 761

const & operator [ ] ( si ze_type i ) const

if (i<O 1 1 this->size ( ) <=i) throw Range_error ( i ) ;


return std : : vector<T> : : operator [ ] ( i ) ;

1;
Range_error,
. std : : vector
Vector -
. using size_type
std : : vector; . 20.5. using
Vector .
Vector .

vector, .v
( , );
, ,
(
).
s td_lib_facili ties . h
().
vector - Vector.
11 ,
11
#define vector Vector

, , vector,
Vector. , .
.
(. 27.8 . 1 7 .2).
,
string.
, ,

[ ] vector.
vector string ,
. .
.
.
, ,
string 2.
762 1 9 .

1 9 .5.
, vector ,
:
, ,
(. 5). ,
, ,
vector . - "
try,
" -
.

, , , -
- .

,
,
,
.
,
.
, - ,
()
. -
, , new,
delete, :
void suspicious ( int s , int )
{
int* = new int [ s ] ; //
11 . . .
delete [ ] ; / /

1 7.4.6,
, .
, -
.
suspicious ( ) , new
,
.
1 9 .5. 763

. ,
, (owner) u (handle)
, .

1 9 5 1
. . .

, , ,
:
int* = new int [ s ] ; //

, new
delete. suspicious ( )
delete [ ] . ,
, .
, ,
? ,
,
. ,
.
, ,
delete:
void suspicious ( int s , int )
(
int* = new int [ s ] ; //
11
if () = q; //
11 . . .
delete [ ] ; 11

if () , ,
, .
, delete.
void suspicious ( int s , int )
{
int* = new int [ s ] ; //
11
if () return ;
11 . . .
delete [ ] ; 11

, delete.
.
void suspicious ( int s , int )
764 1 9 .

int* = new int [ s ] ; //


vector<int> v ;
11
if ( ) [ ] = v . at (x) ;
11 . . .
delete [ ] ; //

.
, ,
, . -
, .
void suspicious ( int s , int ) // ! !
{
int* = new int [ s ] ; /!
vector<int> v ;
11 . . .
try {
if () [ ] v . at (x) ;
11 . . .
} catch ( . . . ) { 11
delete [ ] ; 11
throw ; //
}
11
delete [ ] ; 11


, (
delete [ ] p ; ). , ; -
. , .
void suspicious ( vector<int>& v, int s)
{
int* = new int [ s ] ;
vector<int>vl ;
// . . .
int* q = new int [ s ] ;
vector<doue> v2 ;
11 . . .
delete [ ] ;
delete [ ] q ;

, , new
, bad_alloc.
try . tch ,
. .

try,
. ,
1 9.5. 765

,
, .


try ,
.

1 9 .5.2. -
, try . . . catch,
. :
void f (vector<int>& v, int s )
{
vector<int> p ( s ) ;
vector<int> q ( s ) ;
11 . . .

. , . (
)
.
, .
; :
, ,
.
, - (
iostream).
: "
" ("Resource Acquisition ls Initialization" - RAII).
.
f ( ) , q:
q ,
, return
.
: ,

. , :
. ,
, . ,
, .
,
,
vector, new delete.
766 1 9 .

1 9 .5.3.
,
( ) ? .
vector<int>* make_vec ( ) //
{
vector<int>* =
new vector<int>; //
// . . . ;
/ / . . .

return ;

: ,
,
. , ,
make_vec ( ) vector. ,
, -
, make_vec ( ) (. 1 7.4.6).
,
try.
vector<int>* make_vec ( ) //
{
vector<int>* =
new vector<int>; //
try
// . . . ;
// . . .

return ;

catch ( . . . ) {
delete ; //
throw ; // ,
// ,
// ma ke_ vec ()

make_vec ( )
:
, , -
( ,
vector) , .
(, vector : : at ( ) );
make_vec ( ) throw ; .
,
.
1 9.5. 767

. try . . . catch ,
, make vec ( )
, .
(basic guarantee). ,
.
.
.
.
. , .
. (.. .
)
, ,
(strong guarantee).
- : ,
, ,
, .
(no-throw guarantee).

, .
. ,
++
:
. ,
throw new dynamic_cast
( .5. 7).

. RAII
.
.
. (
) . .
. -
.

1 9 5 4 unique_ptr
. . .

, , make_vec ( ) .
.
,
.
.
. .
768 1 9 .

. .
try . . . catch - . :
- RAII ; .
, . vector<int>
, .
<memory>
unique__ptr.
vector<int>* make_vec ( ) //

unique_P tr< vector<int>> p { new vector<int> } ; //


11
// . . . ;
// . . .
return p . release ( ) ; 11 ,

unique__ptr .
. ,
new. -> * unique__ptr
. (. p->at ( 2 ) ( *) .
t ( 2 ) ) , unique__ptr
. unique__ptr , :
unique__ptr delete .
. ,

make_vec ( ) , vector<int>
. . release ( )
( vector<int>). ,
nullptr, (
return) .
unique__ptr make_vec ( ) .
, make_vec ( ) ,
. , unique__ptr
try ;
- make_vec ( ) -
RAII .
make_vec ( ) . unique__ptr. -
: . -
delete.
unique__ptr:
unique_P tr<vector<int>> make_vec ( ) //
{
unique_P tr<vector<int>>
p { new vector<int> } ; 11
1 9.5. 769

11 . ;
. .

// . . .
return ;

unique_ptr ,
: unique_ptr ,
unique_ptr, .
, ,
unique_ptr :
void no_qood ( )
{
unique_J> tr<X> { new } ;
unique_J> tr<X> q { } ; // :
11 . . .
11 , q

"" ,
, shared_ptr ( .6. 5).
. ,

. .
unique_ptr ,
.

1 9 5 5
. . .



.
, :
?
?
( "
" unique_ptr shared_ptr)
", ", , , .
, vector ,
:
.
vector<int> ake_vec ( ) //
{
vector<int> res ;
// . . . ;
return res ; //
11
770 1 9 ,

() make_vec ( ) ,
.
- . , fstream
.
.
.

,
.

1 9 .5.6. RAll vector


, unique
ptr, .
, , ?
, ,
?
reserve ( ) 1 9.3.7.
teplate<typename , typename >
void vector<T , A> : : reserve ( int newalloc)
{
if (newalloc<=space) return ; 11
* = alloc . allocate (newalloc) ; 11

for ( int i=O ; i<sz ; ++i)


alloc . construct ( &p [ i ] , el em [ i ] ) ; 11

for ( int i=O ; i<sz ; ++i )


alloc . destroy ( &elem [ i ] ) ; 11

alloc . deallocate (elem, space) ; 11


elem = ;
space = newalloc ;

,
alloc . construct ( &p [ i ] , elem [ i ] )
. , - ,
1 9. 5 . 1 . ,
uniqueytr? - ,
- ; ,
vector_ase ,
. .
,
.
1 9.5. 771

,'
sz: : sz- 1 : \

elem : --+--- ]I 1.J


space :
---_-_-_-_-_-_-_-_

,'
( )

,
:
template<typename , typename >
struct vector s {
alloc ; //
* elem; //
int sz ; //
int space ; //

vector_ase ( const & , int n)


: alloc { a } , elem { a . allocate ( n) } , sz { n } , space { n } { }
vector_ase ( ) { alloc . deallocate (elem, space } ; }
};

, vector_base ,
. vector
, . ,
vector - vector_base.
template<typename , typename = allocator<T>>
class vector : private vector_ase<T , A> {
puic :
11 . . .
};

reserve ( ) ,
.
template<typename , typename >
void vector<T , A> : : reserve ( int newalloc)
{
if (newalloc<=thi s->space) return ; //
vector_ase<T , A>
( this->alloc , newalloc) ; //
unini tialized copy (b . elem,
_
& . elem [ this->s z ] , this->elem) ; //
for ( int i=O ; i<this->sz ; ++i )
this->alloc . destroy (
&this->elem [ i ] ) ; 11
swap<vector ase<T , A>> ( * this , b ) ; 11
_
772 1 9 .


uninitialized_copy,

, . reserve ( )
vector_
base, .
- ,
. swap ( )
( <algori thm>) .
. swap<vector_
base<T , A>> ( *this , b) , swap ( * this , b) ,
*thi s (vector vector_base
), ,
swap . this->
vector_base<T ,>
vector_base<T ,>, vector<T , > : : reserve ( ) .


reserve,
unique_Ptr.
. vector_base.
, ,
.


1 . template<typenam.e > struct S { val ; } ; .
2. ,
.
3. S<int>, S<char>. S<doule>. S<string>
S<vector<int>>; .
4. .
5. get ( ) , -
vl .
6. get ( ) .
7. val .
8 . . 4, get ( ) .
9. set ( ) ,
val .
1 9.5. 773

1 0 . set ( ) S<T> : : operator= (const &) .


: , 1 9.2.5.
1 1 . get () .
1 2 . template<typename > read_val ( T&v) ,
cin v.
1 3 . read_val ( ) ,
, . 3,
S<vector<int>>.
1 4. : (>> <<) vector<T>.
, { val , val , val } .
read val ( ) S<vector<int>>.
.


1 . ?
2. ?
3. ,
?
4. ?
5. ?
6. vector
?
7. vector ?
8. ?
9. ?
1 0. ?
1 1 . ?
1 2 . ?
1 3 . --
?
1 4. array vector?
1 5. array ?
1 6. resize ( ) reserve ( ) ?
1 7. ? .
18. ?
1 9. RAII? ?
20. unique_Ptr?
774 1 9 r.

#define unique_ytr
at ( )
push back ( )
_
RAll
resize ( )
shared_ytr
this
throw

( )
,
, . .
,
, .
1 . f ( ) ,
vector<T> ; ,
f (vl , v2 ) vl [ i ] +=v2 [ i ]
vl.
2 . . r
vector<T> vt vector<U> vu
vt [ i ] *vu [ i ] .
3 . Pair.
. ,
7.8.
4. Link 1 7.9. , r
. . 1 3
1 7 Link<God>.
5. Int. int.
. +, -. *
/ .
(. << >>
-).
6. Numer<T>,
. Numer
% , ,
% Numer<doule> Numer<int>.
7. . 2 Numer.
1 9.5. 775

8. (. 1 9 .3.7),
malloc ( ) free ( ) ( . 1 1 .4). vector ,
1 9.4,
. : " new"
" " ++.
9. vector : : operator= ( ) ( 1 9. 2 . 5),
( 1 9.3. 7)
.
1 0. unique_Ptr,
, , > * , release ( ) .
-

,
.
1 1 . counted_ptr<T>,
, " " (
int),
.
, . count&d_Ptr

. countedytr ,
.
countedytr
.
counted_Ptr ,
. "
", , ,
,
. counted_Ptr,
,
..
1 2 . File_handle,
string ( ) ,
.
1 3 . Tracer,
.
. , ,
RAII (,
Tracer , -
, , ,
new, ..
,
Tracer ).
776 19 .

1 4.
" (. 1 8).

, .
1 5 . ,
.
. "
".
1 6. ,
. . - vector
<vector<vector<int>>>. .
, sizeof (vector<int>) ==

sizeof ( int* ) , ..
, ,
space.


.
-
, ..
. , ,
, vector,
. .
, .
. ,
, : push_back ( ) , resize ( ) reserve ( )
.

l , -
#-' #- g l '
,.ut-
- JJ'r AJ<; -ll CJ4f d)


STL - ++ ,
.
STL
-

++ .

.
,
STL.
() ( )

.
,
, .

.
778 20

2 0. 1 . 20.6. :
20. 1 . 1 .
20. 1 .2. 20.6. 1 .
20.2. SL 20.6.2.
20.3. 20.7. vector, l i s t s tring
20.3. 1 . 20.7. 1 . insert erase
20.4. 20.8. vector
20.4.1 . SL
20.4.2. 20.9.
20.5 . SL
vector 20.10.
20.5.1 20.10. 1 .
20.5.2 auto

20. 1 .
,
,
, . ,
,
. ,
,
vector.
. ?
,
, .
,
. .

. 1 1 1 , vector<doue>.
,
? ,
, .
,
.
doule* qet_from_j ack ( int count) ; //
// doue ,
//
// *count
vector<doule> qet from_jill ( ) ; 11
_
20.1 . 779

void fct ( )
{
int j ack_count = ;
doue* j ack_data = qet from j ack ( &j ack count) ;
vector<doule>* j ill_data = qet_from_j ill ( ) ;
11 . . .
delete [ ] jack data ;
_
delete jill data ;
_

,
.
, .
, .

20.1 . 1 .
, ,
.
,
.
,
, w".
, .
, .
? ?
? ? . 65?
?
? . .
.
,
. :
. . . .
. . . .

11 . . .
doule h = 1 ; -

doule* j ack_high ; //
doule* jill_high ; //

for ( int i=O ; i<jack_count ; ++i)


if (h<j ack_data [ i ] ) {
j ack high = & j ack_data [i ] ; //
_
h = j ack data [ i ] ; 11 . " "
_

h = -1 ;
for ( int i=O ; i< j ill_data->size ( ) ; ++i )
780 20

if (h< ( * jill_data) [ i ] ) {
j ill_high = & ( * j ill_data) [ i ] ; //
h = ( * j ill_data) [ i ] ; // " "

cout << " : " << * j ill_high


<< " ; : << * j ack_high ;
"

11 . . .

,
: ( * j ill_data) [ i ] . get_from_j ill ( )
vector<doue> * . ,
,
* j ill_data, .
* j ill_data [ i ] - , ;
* ( j ill_data [ i ] ) , [ ] ,
* * j ill_data, ..
( * j ill_data) [ i ] .


,
. ?

20. 1 .2.

, ,
.
.
, , .
. :
- jack_count j ill_
data->size ( ) , -
j ack_data [ i ] ( * j ill_data) [ i ] . ,
:
vector<doue>& v = * j ill_data ;
for ( int i=O ; i<v . size ( ) ; ++i )
if (h<v [ i ] ) {
j ill_high = &v [ i ] ;
h = v [i ] ;

. ,
,
, ? (. . 3),
20. 1 . 781

.
, ,
.
doule* hiqh ( doule* firs t , doule* last)
11
11 { fi rs t , l a s t)

doule h = -1 ;
doule* hiqh ;
for (doule* first ; p ! =last; ++)
if (h<*p)

hiqh = ;
h = * ;

return hiqh ;

:
doule* jack hiqh = hiqh ( j ack da.ta , j ack_da.ta+j ack_count) ;
_ _
vector<doule>& v = * j ill da.ta ;
_
doule* j ill hiqh = hiqh ( &v [ O ] , &v [ O ] +v . size ( ) ) ;
_

.
( high ( ) ).
, * j ack_high * j ill_high:
cout << "& : " << * j ill hiqh
_
<< " ; : << * j ack_hiqh ;
"

, high ( ) .
,
,
.



. ,
, high ( )
. ,
, ,
.
.

high ( ) ,
.
782 20

. , vector
, .v
,
(. 20.4 2 1 .6. 1 ).
vector
doule,
, vector<doue*> char [ 1 0 ] .
,
.

.
, ,
, ""
:
, , ,
. .
11 . . .
vector<doule>& v = * j ill_data ;
doue* middle = &v [ O ] +v . size ( ) /2 ;
doue* hiqhl = hiqh ( &v [ O ] , middle ) ; 11
doule* hiqh2 = hiqh (middle ,
&v [ O ] +v . si ze ( ) ) ; 11
11 . . .

highl
, high2 -
. .v .
&v[O] middle &v[O] + v.size( )

,...
! ..._ ! ---------. J ,
--..--'---
l j
--- _ _

'---
-------'-
- -------' _ _ _ _

\
hig h 1
\ high2

high ( ) .
. .
,
:
doule* find hiqhest (vector<doule>& v)
_
{
doule h = -1 ;
doule* hiqh = ;
for ( int i=O ; i<v . size ( ) ; ++i)
if (h<v [i ] )
20.2. STL 783

high = &v [ i ] ;
h = v [i] ;

return high ;

,
"" high ( ) -
.

find_highes t ( )
. , " ".
. ,
, . :
. .

20.2. STL
++.
, ,
STL. "
" ("standard template library").
STL ISO ++. (
, vector, list map) (,
sort, find accumulate). , -
, - , vector,
STL. .
. ostream (. 1 0)
- ( . 1 1 .3), STL.
STL, ,
, ,
.
:
.
if, . , -
.
, , , . ,
, .
, "-
". , , ,
"",
,
. ,
"".
784 20

h , :
, , ,
, - .. ; ,
, .. ,
, ,
, ,
. 9, 1 1 1 4.
,
.
.
.
.
, 8800.
1 7 .
.
.
, , "Petersen".
.
.
-
.
.
.
"Stroustrup" .
.
, ,
. , -
, , , .. ,
, ( ) ,
, .
( ). .
.
20.2. SL 785

. .
. .
.
("
") .

.
, -
.
,
.
,
, . ,
.
. ;
.
,
, , .

, vector, list .

;
.

(, 42- );
(, ,
age 7);
(. ,
temperature 32 1 00).

;
;
( ).
(,
1 . 7).
, ,
,
786 20

. ,
.
,
, ,
, .
.
int
doue.
vector<int>
vector<string>.
doue
vector<doue>.
,
, -
.
, ,
, ,
.
vector
.
string
string .


.
.
,
;
;
;
;
.
,
.
:
;
+ .
, .
20.. 787

.
.

;
;
.


, , find, search, sort, sum, . . .
STL .
.
, ,
. STL
(Alex Stepanov) ,
,
. - , .

,
, -
,
.
. ,
; ,
, ,
- .
. ,
STL,
.

20..
STL
. STL
. -
. ,
.
.
(iterator) - .
. .
788 20

end :

begin end
-J
;
-

. STL ,
"" ("half-open") ; . .
begin,
. end .
. ()
: [begin,end). .
, , ,
. ,
, .
? .
() (
) .
= ! =

, ,
* ("").

++.
, q .
-


p=q true , , q,

! =q ! (p== q)
* ,
*p=val val ,
val=*p val ,

++
,

. . (.
1 7.4). .
: .

,
20.. 789

[begin,end) end.
.
, .
.


void copy ( int*fl , int*el , int*f2 ) ,
int,
[fl ,el) [f2, f2+ (el-fl ) ) .

( ).


() .
( , ).
.
.
.
: " STL
, ".
. ,
.
sort, find, search, , " . , my_very_own_algorithm, your_code, " .

vector, list, map, array, . " , my_container, your_container , . "

,
;
. .

;
.
*, ++, = ! .
=.

STL
, (. 2 1 ) . ,

STL. , STL

790 20

(. 1 9.3.2).
, .

20.. 1 .
, " "
STL.
teplate<typename Iterator>
Iterator hiqh ( I terator firs t , I terator last)
11 ,
11 [firs t , l a s t )
{
Iterator hiqh = first ;
for ( I terator = firs t ; p ! =last ; ++)
if ( *hiqh<*p) hiqh = ;
return hiqh ;

, h,
.
,
-1 .
! ,
: -1
, .
, " ", - 1 ,
(. 4.3. 1 , 7 .6. 1 , 1 0. 1 1 . 1 .). ,
r
; . " "
r - ! - .
high ( )
, <. ,
high ( )
vector<string> (. . 7).
high ( )
, . ,
.
.

doule* qet_fro j ack ( int* count) ; //


_
11 do u e ,
/ /
11 * coun t
vector<doule>* qet_fro_j ill ( ) ; //
20.4. 791

void fct ( )
{
int j ack_count = ;
doue* j ack_data = qet from j ack ( &j ack count) ;
vector<doue>* j ill_data = qet_from j ill ( ) ;
_

doue* jack_hiqh = hiqh ( j ack_data , j ack_data+jack count) ;


_
vector<doue>& v = * j ill_data ;
doue* j ill_hiqh = hiqh ( &v [ O ] , &v [ O ] +v . size ( ) ) ;

cout << " : " << * j ill_high


<< " ; : << * j ack_hiqh ;
"

11 . . .
delete [ ] j ack_data ;
delete j ill_data ;

high ( ) I terator
doue* .
. ,
, .
high ( )
. .
STL
, ,
,
.


. ,

.

20.4.

.

begin : end : Q_


- ---J
'

1 _ _ _ _ _ _ _

, .
792 20

, , v . begin ( ) ,
v . size ( ) , ,
v. end () .
.
STL . ,
,
.

( )
. STL .
,
STL, .u (liked list).
.
- ,
. ,
( ), .
u (singly-linked list), ,
, , - . (douy
linked list). ,
++ list. I
.

egi n :
J.L.----'

:
template<typenae Elem> struct Link
Link* prev ; //
Link* succ; //
Elem val ; //
};

template<typenae Elem> struct list {


Link<Elem>* first ;
Link<Elem>* las t ; // ,
};

I Link .
20.4. 793

prev
succ

val


. , ,
.
- ,
, ,
,
.
. , .
, .
- ,
.

20.4. 1 .
?
, (.
..), .
()
.
,
: .
STL .
.
teplate<typenae Elem> class list {
//
pulic :
class iterator ; // - : i t era tor

iterator beqin ( ) ; // ,
iterator end ( ) ; // , ,
//

iterator insert ( i terator , // v


const El&111& v) ; // ,
11
iterator erase ( i terator ) ; 11 ,
11
794 20

void push back (const Elem& v) ; 11 v


_
void push_front ( const Elem& v) ; 11 v
void pop_front ( ) ; 11
void pop back ( ) ; 11
_
Elem& front ( ) ; 11
Elem& back ( ) ; 11
11 . . .
};

, vector
, list -
. ;
. "" list -
.
.
++. .
list
STL.
. . ""
.
.
20. 1 20.3. 1 .
( 2 1 . 1 -2 1 .3).
list ?
,
: 1 s t [ 1 ]

.
1 0 0 0 . ,
( advance ( ) ; .
20.6.2). list
. .
( ).
.
. ,
i terator.
list<T> : : iterator, vector<T> : : iterator, map< , V> : :
i tera tor ..

2 0.4.2.
* ++, ==

! .
=.

-- . .
20.4. 795

template<typenae Elem> class list<Elem> : : iterator


Link<Elem>* curr ; // I1Q1
puic :
iterator (Link* ) : curr { p } { }

11 :
iterator& operator++ ( ) { curr = curr->succ ; return * this ; }

11 :
iterator& operator-- ( ) { curr = curr->prev; return * this ; }

11 ( ) :
Elem& operator* ( ) { return curr->val ;

bool operator== ( const iterator& ) const


{ return curr == b . curr ; }
bool operator ! = ( const i terator& ) const
{ return curr ! = b . curr ; }
};

, , , :
. " .
, ,
.
. ()
list<Elem> : : i terator ,
,
. ,
++, --. * = ! = .
high ( ) .
template<typenae Iter> // Inp u t_ i tera tor<I ter> ()
11 (. 1 9 . 3 . 3)
Iter hiqh ( I ter first , Iter last)
11 ,
11 (fi rs t , l a s t) ,
(
Iter hiqh = firs t ;
for ( I ter = first; p ! =last ; ++)
if ( *hiqh<*p) hiqh = ;
return hiqh ;

list.
void f ( )
{
list<int> l s t ; for ( int ; cin >> ; ) lst . push_front (x) ;

list<int> : : iterator = hiqh ( lst . beqin ( ) , lst . end ( ) ) ;


cout << " " << * << ' \n ' ;
796 20

"" I ter list<int> : : i te


rator, ++, * ! ,
=

, . high ( )
- ( )
.
, push front ( )
.
push_back () ,
vector.


vector push_
fron t ( ) . ? push fron t ( )
vector push_back ( ) .

"
?" , " l s t . begin ( ) ==l st . end ( ) ?"
* (
) , , .. lst . end ( ) .
! , , ,
.
:
, , begin ( ) end ( ) , -
, .

begin : end :

- - -
.
'
'
- - - - - - - ...

, end ( )
, , :
.
, - -
.
:
list<int> : : i terator = high ( l s t . begin ( ) , ls t . end ( ) ) ;
if ( == l s t . end ( ) ) // ?
cout << "n n " ;
else
cout << 111 " << * << endl ;

STL,
end ( ) .
20.5. n vector 797

,
.
, (
, . 1 2- 1 4).

20.5. vector
, , 20.3 20.4, ,
vector ( s td : : l i s t) -
iterator, - begin ( ) end ( ) .
vector 1 9.
SL-
. 20.3?
(
), .
teplate<typename > // El emen t<T> () ( 1 9 . 3 . 3)
class vector {
pulic :
using size_type = unsigned long;
using value_type = ;
using iterator = * ;
using const_iterator = const * ;

11 . . .

iterator begin ( ) ;
const_iterator begin ( ) const ;
iterator end ( ) ;
const iterator end ( ) const ;
_
size type size ( ) ;
_

11 . . .
1;
using ; ,
vector i terator - , .. ,
: * . -
v vector :
vector<int> : : iterator = find ( v . begin ( ) , v . end ( ) , 32 ) ;

for (vector<int> : : size_type i = ; i<v . size ( ) ; ++i )


cout << v [ i ] << 1 \n ' ;

, , ,
, i terator
798 20

size_type. , ,
i terator size_type, ,
size_type unsigned long (
-

). iterator , (
-

++).
list
, :
teplate<typename > // El emen t<T> () ( 1 9 . 3 . 3)
class list {
puic :
class Link ;
usinq size_type unsiqned lonq ;
usinq value type ;
_
class iterator ; // . 2 0 . 4 . 2
class const_iterator ; // i tera tor,
//

11 . . .

iterator beqin ( ) ;
const_iterator eqin ( ) con s t ;
iterator end ( ) ;
const i terator end ( ) const ;
_

size_type size ( ) ;

11 . . .
};

. , :
list vector.
, iterator size_type,
,
( - 2 1 ).
: : i terator
I terator<C>.
:
template<typename >
usinq I terator = typename C : : iterator ; // I t e ra tor<C>
11 typename C : : i tera tor

I terator<C> ,
: : i terator
typename, , iterator .

template<typename >
usinq Value_type = typename C : : value_type ;
20.5. vector 799

Value_type<C>.
,
std lib facili ties . h.
using ++ 1 1
, ++ typedef ( . 1 6).

20.5. 1 .
size ( ) ,
:
void printl ( const vector<doule>& v)
{
for ( int i = ; i<v . size ( ) ; ++i )
cout << v [ i ] << ' \n ' ;

, list
. .
, for
( 4.6. 1 ) :
void print2 ( const vector<doule>& v , const list<doule>& lst)
(
for (doule : v)
cout << << ' \n ' ;
for (doule : lst)
cout << << ' \n ' ;

,
"" vector list. ? , for
begin ( ) end ( ) ,

vector. for -
.
begin ( ) end ( ) vector list, "
" for .

20.5.2 auto
,
. :
template<typename > 11 El emen t <T> ()
void user (vector<T>& v, list<T>& lst)
(
for (vector<T> : : iterator = v . egin ( ) ; p ! =v . end ( ) ; ++)
cout << * << 1 \n ' ;
800 20

list<T> : : iterator q = find ( lst . egin ( ) , lst . end ( ) , { 42 } ) ;

. . ,
i terator list, size_type
vector. ,
? , .
. ,
:
auto, ,
:
template<typename > // Elemen t < T> ()
void user (vector<T>& v, list<T>& l s t)

for (auto = v . egin ( ) ; p ! =v . end ( ) ; ++)


cout << * << ' \n ' ;
auto q = find ( lst . egin ( ) , l st . end ( ) , ( 42 } ) ;

vector<T> : : iterator, q - list<T> : : iterator.


auto .
:
auto = 123 ; 11 i n t
auto = ' ' ; 11 cha r
auto& r = ; 11 r i n t &
auto = r ; 11 - i n t ( )

, const char ,
auto
:
auto s l " San Antonio" ;// s l con s t cha r *
// ( ? ! )
string s 2 = " Fredericksburg" ; // s 2 s t ring

, ,
, auto.

auto
for :
template<typename > 11 Con ta iner<T>
void print ( const & cont)
(
for ( const auto& : cont) cout << << ' \n ' ;

auto,
cont - . const.
20.6. : 801

, &
() , ,
.

20.6. :

.
, . -
,
. ,
.
? ,
. ,
,
.

. ,
.
, .
.
.
.
.
.
vector<char>.
, ,
. .
his is he start of very long document .
here are lots of . . .

t
:
his is the start of very long document .
here are lots of

,
vector<char>, ,
h, .
. , , 80 . (
).
40 . .
802 20

.

, .
list<Line>.
Line - vector<char>.

1 This 18 the stert of very long document

2 There are lot8 of

: _ _ _ _ t _ _ _ _ :
. .
1
_ - - - - - - - - J

t
. ,
- .
"This is new line . " "document . .
his i s the start o f very long document .
This is new line .
There are lots of . . .

, , -
.

1 his 18 the start of very long document.

here are lot8 of

.
; _ _ _ _ t _ _ _ _ :
. . Thls is new line.
- - - - - - - - - .J


, ,
, , .
.
, vector
<list<Line> : : i terator>. ,
.
20.2,
. 20.3.
,
,
. , , -
20.6. : 803

20 . 1
20 .2
Storlng and processing data
20 .

STL ldeals

'
,_ - - - - - - - - - -

Sequences and lterators

. .
vector" - .
, list vector,
! (.
20.7.) - ,
() . .
STL
(, , )
,
vector.

20.6. 1 .
. ?
.
1 . (. ' \n ' )
.
2.
"" (. ).
3. ,
(, 50 ). .
, , .
.
Docu
men t. :
using Line = vector<char> ; // -

struct Document
list<Line> line ; // -
Document ( ) line . push_back (Line { } ) ; }
};
804 20

Document :
Document .
:
i stream& operator>> ( istream& is , Docuent& d)
{
for ( char ch ; i s . ge t ( ch) ; ) {
d . line . back ( ) . push_back ( ch) ; 11
if ( ch== ' \n ' )
d . line . push_back (Line { } ) ; 11

if ( d . line . back ( ) . size ( ) ) 11


d . line . push_back ( Line { } ) ; 11
return i s ;

vector list - back ( ) ,


.
, , -
back ( ) !
Documen t
. ,
, ( ' \n ' ).
.
(
).

20.6.2.
vector<char>,
. ?
, list<Line> :
: i terator. ,
, ?
, Document.
class Text_iterator { 11
11
list<Line> : : iterator ln ;
Line : : iterator ;
puic :
11 1 1 -
Text_i terator (list<Line> : : iterator 11 , Line : : iterator )
: ln { ll } , { } { }

char& operator* ( ) { return * ;


Text_iterator& operator++ ( } ;

bool operator== ( const Text iterator& other} const


20.6. : 805

{ return ln==other . ln && pos==other . pos ; }

bool operator ! = ( const Text_i terator& other) const


{ return ! ( *this==other ) ; )
};

Text iterator& Text_i terator : : operator++ ( )

++pos ; //
if (pos== ( *ln) . end ( ) )
++ln ; //
= ( *ln) . begin ( ) ;

return *this ;

Text_iterator ,
Document begin ( ) end ( ) .
struct Document {
list<Line> line ;

Text iterator begin ( ) //


_
{ return Text_iterator ( line . egin ( ) ,
( *line . begin ( ) ) . egin ( ) ) ; }
Text iterator end ( ) //
_
{ return ( line . end ( ) , ( *line . end ( ) ) . end) ) ; }
};

( * line . begin ( ) ) . begin ( )


.
line . begin ( ) :
line . begin ( ) ->begin ( ) .
->.
:
void print (Document& d)
1
for (auto : d) cout << * ;

print (y doc) ;
_

, 1
. . .
. n.
void erase line (Document& d, int n)
_
{
if (n<O 1 1 d . line . size ( ) -l<=n) return ;
auto d . line . begin ( ) ;

806 20

advance (p ,n) ;
d . line . erase (p) ;

advance ( , n) n ;
advance ( ) - .
.
template<typenae Iter> // Forwa rd i tera tor<I ter>
void advance (Iter& , int n)
{
while ( O<n) { ++ ; --n ; }

, advance ( )
. vector v
*advance ( v . begin ( ) , n ) v [n] .
" , advance ( )
n-1 ,
n- . . list
. ,
.
. , ,
list,
advance ( ) .
, , , vector,
advance ( )
++.
, advance ( )
. : ,
, -
, ,
, .


advance ( ) . ,
, .

, -
. (, milkshake Gavin),
, (,
secret\nhomestead, .. , secret,
, homestead),
(, [] \w*ne, .. ,
, ne; .
20.6. : 807

23) . . , : ,
Document.
- ! - .
.
,
.
, ; ,
.
STL ,
, .
,
. , ,
; , ,
.
Text iterator find_txt (Text_iterator first,
Text iterator las t , const string& s)

if ( s . size ( ) ==O ) return las t ; //


char first_char = s [O ] ;
while ( true) {
auto = find ( firs t , las t , first_char) ;
if (p==last 1 1 match (p , last , s ) ) return ;
first = ++ ; // )!


, STL. match ( )
; .
. find ( ) ,
, ,
( 2 1 .2). find_txt ( )
:
auto = find txt (my doc . egin ( ) , my_doc . end ( ) ,
_ _
" secret\nhomestead" ) ;
if (p==my_doc . end ( ) )
cout << "not found" ;
else (
11

" " . ,
, "
" . ,
, - .
808 20

,
,
STL (, list vector)
(), STL, ,
,
. , ,
Documen t STL; Text_i tera tor,
Documen t
.

20.7. vector, list string


list, -
vector? ,
list, -
vector? ,
string?
.
.
char [ ] ( )
vector<char>
string
list<char>
?

; , . .
, ++
* .
. Document,
vector<char> list<char> string
- .
,
, . ,
,
: ,
?
Elem [ ] . begin ( ) .
.

end ( ) -.
.
,
.
20. 7. vector. list string 809

.
. ( ! ) (<<)
== =

, .
vector<Elem>. ,
insert ( ) erase ( ) . .
, insert ( ) erase ( ) , ,
(
).
.
.
vector (.
push back ( ) ) . (
).
! <. <=. > >=.
=. =,

string. ,
.
(+ +=).
. .
! <. <=. > >=.
=. =.

list<Elem>. .
. insert ( ) erase ( )
.
(
). .
! < . <=. > >=.
=. =.

( 1 7. 2 1 8.6),
.
. -
(. 27. 1 .2 27.5).
vector .
, .


?
char, vector<char>. list<char> s tring
"Hello"
.
. " Hello" ( ,
" Hello" ) ,
"Howdy " . ,
. .
810 20

i
int, vector<int>
list<int> { 1 , 2 , 3 , 4 , 5 } .

20.7. 1 . insert erase


vector
. ,

. ,
, ( insert ( ) erase ( ) ),
;

, .
.

, push_back ( ) . ,
.
,
;
, -
, .
20.6,
: ,
(, insert ( ) , erase ( ) push back ( ) ) ,
; .
,
(
). list (
map; . 2 1 .6) vector.
,
, list.
insert ( ) erase ( ) vector list.
, ,
.
vector<int> : : iterator = v . begin ( ) ; // ,
++ ; ++ ; ++ ; //
// 4 -
vector<int> : : iterator q = ;
++q; //
11 5 -
20.7. vector. list string 811

v . insert (p , 9 9 ) ; //

: q:
O

q .
. v
,
, q, ,
3, 4 .
= v . erase (p) ; 11 ,
11

: q:
O
v: 6
r------r---""---.---.- - - - -
- - - ---- - - - - - - -- -
- - 1
'
2 5
-- - - - - - - - - - - - - - - - - - - - - - - .J
'

, erase ( ) insert ( )
. , q .
, ,

, ,
, .
q
, .
list.
list<int> : : iterator = v . egin ( ) ; 11 ,
++ ; ++ ; ++ ; 11
11 4 -
list<int> : : iterator q = ;
++q ; 11
11 5 -
81 2 20

= v . insert (p , 99 ) ; 11

, q -
, 4 .
= v . erase (p) ; 1 1 ,
//

: q:

, . ,
vector, list, , q
.
list<char>
, ; list<char>
1 2 , vector<char> -
.
.
vector
string? ,
, string ,
vector, . :
string , .
vector ,
, push_ack ( ) . string - e.
string ,
-.
vector, insert ( ) erase ( ) .
.
, .
.
. .
vector, string,
,
, .
20.. n vector SL 81 3

20.. vector STL


begin ( ) , end ( )
20. 5 vector insert ( ) erase ( ) ,
std : : vector.
teplate<typename , typename = allocator<T>>
11 Elemen t <T> ( ) Al l oca tor<A> ( ) ( 1 9 . 3 . 3)
l vector {
int z ; 11
* elem; 11
int ; 11
11
alloc ; 11
pulic :
11 . . . 1 9 2 0 . 5
uinq iterator = * ; 11 * -

iterator inert ( i terator , cont & val ) ;


iterator erae ( i terator ) ;
};


. .
, ,
(. 1 8).
, , insert () erase () . -
, (,
vector) .
insert ( ) erase ( ) , ,
-
.
push_back ( ) ,
.
vector<T , > : : erase ( )
, .
vector 1 9.. 7 ,
.
teplate<typename , typename >
// El emen t <T> () A l l oca tor<A> () ( 1 9 . 3 . 3)
vector<T , A> : : iterator vector<T , A> : : erase ( iterator )
(
if (p==end ( ) ) return ;
for ( auto = p+l ; pos ! =end ( ) ; ++ )
* (pos-1) = * ; //
//
alloc . detroy ( &* ( end ( ) - 1) ) ; 11 111I0
11
81 4 2 0

--sz ;
return ;

, .


sz : " - :: - .

1 1 1 1 l_-_-.-
h_-IJ
__ -

elem:
1 14 14 1 "
space: '--' '--' '--' ' - - ' ' - - ' \ delete ,"
' - - - ',
- -

erase () , ,
.
vector? p=end ( ) ?
?
. ?
vector<T , > : : insert ( )
.
template<typenas , typename >
11 Elemen t <T> () Alloca tor<A> () ( 1 9 . 3 . 3)
vector<T , A> : : iterator
vector<T , A> : : insert ( iterator , const & val)

int index = p-beqin ( ) ;


if ( size ( ) =capacity ( ) ) //
reserve ( size ( ) ==0 ? 8 : 2 * size ( ) ) ; //

11
11 :
alloc . construct (elem+sz , *back ( ) ) ;
++s z ;
iterator = eqin ( ) +index ; // val
for ( auto pos = end ( ) -1 ; pos ! =pp ; --pos)
*pos = * (pos-1) ; 11
11
* (eqin () +index) = val ; 11 " " val
return ;

..
- ;
, elem+space.
,
, .
2D.9. SL 81 5

reserve ( ) ,
. ,
. ,
.
, . ,
-
.

, . -
, .
, ,
. ,
vector, ,
.
, .
, "" .
, ,
insert ( ) erase ( ) :
list ( map; . 2 1 .6).
insert ( ) erase ( ) ,

,
(. . 20). () ,
.

20. 9. STL
:
,
, ( .
1 8.6.2) .. :
.
,
, array,
, . array

++. , ,
. array
.
.
81 6 20

teplate <typenae , int N> 11 El emen t <T> ()


//
struct array (
usinq value_type = ;
usinq iterator = * ;
usinq const_iterator = const * ;
usinq size_type = unsiqned int ; //

elems [N] ;
11 //

iterator beqin ( ) ( return elems ; }


const iterator eqin ( ) const ( return elems ;
iterator end ( ) ( return elems+N ; }
const iterator end ( ) const ( return elems+N;

size_type size ( ) cons t ;

& operator [ ] ( int n) ( return elems [n] ; }


const & operator [ ] ( int n) const ( return elems [n] ; }

const & at ( int n) con s t ; //


& at ( int n} ; / /

* data ( ) ( return elems ; }


const * data ( ) const ( return elems ; }
};

,
. ,
array, .
, <array>.
, array<T , N> N,
( ) =. ! = ,
=.

vector.

high ( ) 20.4.2:
void f ( )
(
array<doule , 6> = ( . , 1 . 1 , 2 . 2 , . , 4 . 4 , 5 . 5 } ;
array<doule , > : : iterator = hiqh ( a . beqin ( ) , a . end ( ) ) ;
cout << 11.n " << * << endl ;

, array.
high ( ) . high ( )
array ,
.
20.1 . 81 7

20.1 .
STL .


vector .

list . ,


deque . ,


.
, ( .
2 1 .6. 1-2 1 .6.3)
mul tiap ,
. ,
(.
2 1 .6. 1 -2 1 .6.3)
unordered_ap -; .
,
- ( . 2 1 .6.4)
unordered_mul timap -,
; mul tiap.
,
- (. 21 .6.4)
set .
(.
2 1 .6.5)
mul tiset ,
.
( .
2 1 .6.5)
unordered set unordered-map, ,
(, )
unordered mul tiset unordered-mul tiap, ,
( .)
array ,
(. 20.9)


.
. . .
81 8 20

1 . Josuttls, Nicholai . h ++ Standard Lirary: Tutorial d Rference.


Addison-Wesley. 20 1 2. ISBN 978-032 1 6232 1 8 .
! 1
2 . Lippman, Stanley " Jose Lajoie, and Barbara . . The ++ Primer.
Addison-Wesley, 20 1 2. ISBN 978-0-32 1 -7 1 4 1 1 -4.
! 2
3. Stroustrup. Bjarne. The ++ Programming Language. Addison-Wesley.
20 1 2. ISBN 978-032 1 7 1 4 1 1 4. !
4. STL
- SGI (Silicon Graphics International)
- www . sgi . com/tech/ stl. ,
- .
? ,
, ? .
,
, .
, .
, - .

, . -
,
, , ,
, . " " -
.
.
, , , vector, list
map. , 2 1 .
STL.
, ,
, .
?
.
. STL -
.

[begin ( ) : end ( ) ) .

1 : .
.
++: , 2- . - . :
".. " , 20 1 4.
2 : . , , . .

++ . , 5- . : "..
-

", 20 1 5.
20.1 D. 81 9

,
.
.
value_type .
iterator const_i terator.
* , ++ (
, ), = ! =
. list --
:
(directional iterator).
vector --, [ ] , + - .
umepwnopau
(random-access iterators) ( . 20. 1 0. 1 ) .
insert ( ) erase ( ) , front ( )
back ( ) , push_back ( ) pop_back ( ) , size ( ) .. ; vector map
( [ ] ) .
( ! =, <. <=, >
=,

>=) . <. <=, > >=


: ,
, .
- .
.
The ++
Progrwnmig Laguage .

, . ".
.

" "
size -.
[n] ,
, vector, string array
string ,
, (+ +=).
string
valarray ,
, .
-

,
, ( )
.
820 20

, vector.
, vector.

2 0. 1 0. 1 .
.
.
,
. , ,

, .


, ++,
( ) *.
istream (. 2 1 . 7 .2).
( * ) . m ,
p->m
, ++,
( ) *.
ostream (. 21 .7.2)
, ++,
( )
* ( *) . m
,
p->m
( ++) (
- -), (
)
* . ( *) . m ,
p->m
( ++) (
- -), (
)
* [ ] .
, ,
+ -.

, . (
vector.)
( *) . m ,
p->m

I , ,

. . -
20.1 . 821

,
- .
.


;'

,
. ,
.


1 . int 1 , 1 , 2, 3, 4,
5, 6, 7, 8, 9 }.
2. vector<int>
.
3. list<int> .
4. , .
,
.
5. 2:
:
5.
6. ( )
teplate<typenama Iterl , typename I ter2>
11 Inp u t_ i tera tor<Iterl > () Ou tp u t_ i tera tor<Iter2> ()
I ter2 copy ( I ter fl , I terl el , I ter2 f2 ) ;

[fl.el) [f2 , f2+


+ (el-fl ) ) ,
.

, f2+ (el-fl ) .
, fl=el,
.
822 20

7. ( )
. - .
8. find ( ) .
. 3,
( ).
find ( ) . ,
27.
( ). ""
, ..
find ( ) .
. , .
.


1 . , , --
? .
2. , ?
3. .
4. ?
5. ?
6. STL?
7. STL? ?
8. ?
9. ?
1 . ,
?
1 1 . ?
1 2. ?
1 3 . STL?
1 4. ?
?
1 5 . ( )?
1 6. insert ( ) ? erase ( ) ?
1 7 . , ?
1 8 . list?
1 9. , STL?
20. string. vector?
20.1 D. 823

2 1 . list, vector?
22. ?
23. begin ( ) end ( ) ?
24. STL?
25. ?
STL?
26.
, ?

auto
begin ( )
end ( )
erase ( )
insert ( )
usin9
value_type

1 . ,
.
2.
20. 1 .2. .
. (. 1 8. 7);
. 2 , .
4. .
20.. 1 , STL.
5. (>> <<) vector.
6. " " Docwnent,
20.6.2.
7.
vector<string>.
8. ,
Docwnent.
9. ,
Document. : ,
- " ,
", , - " -
824 20

". ,
alpha . numeric as12b ,
.
1 0. ,
- .
1 1 . vector<doule>
list<int>, ( ).
, .
.
1 2 . list 20.4. 1 20.4.2
high ( ) .
Link, , .
1 3 . list
Link, .
,
Link (l ist<Elem> : : end ( ) )
; .
.
1 4. slist,
std : : li st. list
slist,
?
1 5. pvector. ,
, ,
, -.
1 6. ovector, pvector, ,
[ ] * , ,
.
1 7. ownership_vector, ,
pvector, ,
, (..
). :
, 1 3.
1 8.
vector ( ).
1 9.
list ( ).
20.
vector list.
26.6. 1 . N
20.1 . 825

[O,N) .
vector<int> (
). :
. . I
.
. ,
list<int>. N .
list , vector?
.
w (John Bentley).

N
, ,
N*M . -
, N*M*K .
STL .
( ) .

, N+M .
. , 12 60
, 720 ,
, STL, 60
1 2 : 90% .
,
,
(. , , . 6) . , STL
, ,
,
.


"<
- 1Uk- r- 4)

,
STL, .
.
-
,
. , .


, . .
. -
,
,
.
. : m.ap, set
unordered m.ap.
_
828 2 1

21 . 1 . 21 .5.4.
inner_yroduct ( )
21 .2. : find ( ) 21 .6.
21 .2.1 . 21 .6. 1 .
21 .6.2.
21 .3. : 21 .6.3. -
find_if ()
21 .4. 21 .6.4. unordered_map
21 .4. 1 . 21 .6.5. set
21 .7.
21 .4.2. 21 .7. 1 .
21 .4.3. - 21 .7.2.
21 .5. 21 .7.3. set

21 .5 . 1 . accwnulate ( )
21 .5.2. 21 .7.4. copy_if
accumulate ( ) 21 .8.
21 .5.3. inner_yroduct ( ) 21 . 9.

2 1 . 1 .
.
- .
, , ,
.


r=find ( , e , v) r v
[,)
r=find_if (b , e , p) r ,
, () true,
[,)
x=count ( , , v) v [,)
-

x=count_if (b , e , p) [,),
-

, () true
sort (b , e) [,)
<
sort ( , e , p) [,)

( , , 2) [,)
[2,2+ (-) ) ;
2
21 .2. : find() 829


unique_copy (b , e , b2 ) [,)
[2,2+ (-) ) ;
merge (b , e , 2 , e2 , r) [2,2)
[,) [r,r+ (-) + (2-2) )
r=equal_range (b , e , v) r [,)
-

v, - v
equal (b , e , 2 ) ,
[,) [2,2+ (-) )
x=acc\lulate ( , , i ) i [,)
-

x=acc\lulate ( , , i , ) acc\lulate, -

x=inner_product ( [,) [2,2+ (-) ); i
- -
, , 2 , i) ,
x=inner_product ( inner_product,
, , 2 , i , , 2 ) + * 2

.. -
==. - < (-
).
<algori thm>.
.5 2 1 . 2-2 1 .5.
.
; -
, . ,
,
,
. ,
, . .
find (b , e , v) , v.

2 1 . 2. : find ( )
,
find ( ) . .
template<typename In, typename >
11 Inp u t_ i tera tor<In> ()
11 Equa l i ty_ compa rale<Va l ue_ type<T>> () ( 1 9 . 3 . 3)
In find ( In first , In las t , const & val)
11 { fi rs t , l a s t ) , va l
{
while (first ! =last && *first ! = val ) ++first;
return first;
830 21

find ( ) . ,
find ( ) , , , -
(, 20.6.2).
find ( ) ,
.
, find ( )
, . val
[first, last). , -
find ( ) , .
, val ,
l ast. ,
, - ,
STL , .
find ( ) .
void f (vector<int>& v, int )
{
auto = find (v. beqin ( ) , v . end ( ) , ) ;
if (p ! =v . end ( ) ) {
11 v

else {
11 v ,

}
11

, ,
( - ) .
, ,
.
, .
,
auto. , "" auto,
, :
auto ch = ' ' ; 11 ch cha r
auto d = 2 . 1 ; 11 d d
auto .
find ( ) ,
( vector<int> : : i terator).
, find ( ) . ,
,
. , ,
find ( ) .
21 .2. : find() 831

teplate<typename In , typename >


11 Inp u t_i tera tor<In> ()
11 Equa l i ty_ compa rae<Va l ue_ type<T>> () ( 1 9 . 3 . 3)
In find ( In first , In las t , const & val)
11 [ fi rs t , la s t ) , va l
(
while (first ! =last && *first ! = val ) ++first ;
return first ;

, ? .
,
. ,
, .
.
teplate<typename In , typename >
11 Inp u t_ i tera tor<In> ()
11 Equa l i ty_ compa ral e<Va l ue_ type<T>> () ( 1 9 . 3 . 3)
In find ( In first , In las t , const & val )
11 [fi rs t , la s t ) , va l
(
for (In = first ; p ! =last; ++)
if ( * == val ) return ;
return last;

,
.
,
() ,
. ? ,
() find ( ) ,
, ,
, , ,
. .


,
? ?
.
.
(Donald nuth) : " .
. ".
.
, , .
832 21

2 1 .2.1 .
find ( ) . ,
.
.
find ()
STL.
find ( ) .
( .
20.4) .
void f (vector<int>& v , int ) / / i n t
{
auto = find (v . beqin ( ) , v . end ( ) , ) ;
if ( != v . end ( ) ) { / * */
11 . . .

.
find ( ) . vector<int>
: : iterator; .. ++ ( ++first) -
(
). * ( *first)
. ( firs t ! =last)
. ( *firs t ! =
vl) - .
list.
void f ( list<strinq>& v, strinq ) //
{
list<strinq> : : iterator = find ( v . beqin ( ) , v . end ( ) , ) ;
if ( p ! =v . end ( ) ) { / * */ }
11 . . .

.
find ( ) . list
<string> : : i terator.
,
( vector<int>).
; . ++ (
++first) ,
. * ( * first) .
( first ! =last)
Link*. ( *firs t ! =val)
! = string.
21 .3. : find_if() 833

, find ( ) :
,
find ( )
. , find ( )
Document, 20.6:
void f (Docuent& v , char ) // Documen t
(
Text_iterator find (v . eqin ( ) , v . end ( ) , ) ;
if (p ! =v . end ( ) ) { / * */ }
11 . . .


STL ,
, .

2 1 .. : find if ( ) _

- .
. .
find,
. ,
, 42.
. ,
" 1 7 Cherry Tree Lane " .
,
, find if ( ) _ .

teplate<typename In , typename Pred>


// Inp u t_i tera tor<In> ()
// Predi ca t e <Pred, Va l ue_ type<In>> ()
In find_if ( In first , In las t , Pred pred)

while (first ! =last && ! pred ( *first) ) ++first ;


return first ;

( ) ,
find ( ) , , ! pred ( *first) ,
*first ! =val; , ,
pred ( ) ,
.
(predicate) - , true
fal se. , find_if ( ) ,
, u pred ( * first) .
, -
834 21

, " ){?", "


42?" " ?". ,
.
bool odd (int ) { return %2 ; } // % -

void f (vector<int>& v)
{
auto = find_if ( v . egin ( ) , v . end ( ) , odd) ;
if (p ! =v . end ( ) ) { / * */ }
11 . . .

find if ( ) odd ( )
_

, .
,
, .
, 42.
bool larger_than_42 (doue ) { return >42 ; }

void f ( list<doule>& v)
{
auto = find_if ( v . egin ( ) , v . end ( ) , larger_than_42) ;
if (p ! =v . end ( ) ) { / * , 42 */ }
11 . . .

.
, 4 1 ?
. , 1 9?
. !
v,
-
find if ( ) . (
v_val)
.
doule v_val ; // , l a rger than v ()
_ _
11
bool larger_than_v (doule ) { return x>v_val ;

void f ( list<doule>& v , int )


{
v_val = 31 ; // v_ va l 3 1
auto = find_if ( v . begin ( ) , v . end ( } , larger_than_v) ;
if (p ! =v . end ( ) ) { / * , 31 */ }

v val = ; // v_ va l
auto q = find_if ( v . begin ( ) , v . end ( ) , larger_than_v) ;
21 .4. 835

if ( q ! =v . end ( ) ) { / * , */ }

11 . . .

! , .
, ,
,
. : !


?
,
. ,
.

2 1 .4.
, find_if ( ) ,
,
. , :
void f ( list<doule>& v , int )
{
auto = find_if ( v . beqin ( } , v . end ( ) , Larqer_than ( l) ) ;
if (p ! =v . end ( ) ) { /* , 3 1 */ }

auto q = find_if ( v . eqin ( ) , v . end ( ) , Larqer_than (x) ) ;


if (q! =v . end ( ) ) { / * , */
11 . . .

, Larger_than .
, pred ( * first) .
, 31 ,
.
,
, .. , . ,
,
. .
class Larqer_than
int v;
pulic :
Larqer_than (int vv) : v (vv) { } //
bool operator ( ) ( int ) const { return x>v ; } //
1;
836 21

, ,
. , .
Larger_than ( 3 1 ) , ()
Larger_than, 31 v:
find_if ( v . eqin ( ) , v . end ( ) , Larqer_than ( l) )
Larger_than ( 3 1 ) find_if ( )
pred. v find_if ( )
pred ( *first)
, .. -
operator ( ) , *first.
, . . *first,
31.
,
( ) , .
( ) (func-
tion call operator) (application operator) .
, ( ) pred ( * first)
Larger_than : : operator ( ) , , [ ]
v [ i ] vector : : operator [ ] .

2 1 .4. 1 .
, , ""
. ,
,
.
.
class F ( //
S s; 11
puic :
F ( const S& ss) : s { ss ) { / * */ )
operator ( ) (const S& ss) const
{
11 - ss
11 (
// void, bool 5)

11 :
const S& state ( ) const { return s ; }
11 :
void reset ( const S& ss) { s = s ; }
1;
21 .. 837

F s.
-.
" - " " ".
F, .
. F
, state ( ) ,
- reset ( ) .
.
, ,
.
F ,
, , .

STL.
, ,
(. 2 1 .3), (
2 1 .4.2),
( 2 1 . 5), , (
2 1 .8), .
- .
. , .
,

. ,
,
:
,
! ,
(, -
) ,
(, <)
(,
). -
- .

,
, , .

, ""
Larger_than .
1 0-50 , . ,
, .
838 21 n

2 1 .4.2.
.
, int doue.

. .
. -
.
struct Record {
string n. ; // -
char addr [24] ; //
//
11 . . .
};

vector<Record> vr ;

vr , - .
,
.
:
11 . . .
sort (vr . egin ( } , vr . end ( ) , Cmp_by_nae ( } ) ; //
11 . . .
sort (vr . begin ( ) , vr . end ( ) , Cmp_y_addr ( ) ) ; //
11 . . .

Cmp_by_name - .
Record name.
,
sort ,
. Cmp_y_name ( )
Cmp_by_name sort ( ) .
Record. , ,
. - Cmp_by_name Cmp_by_addr.
// Record:

struct Cmp_by_na.e {
bool operator ( ) ( const Record' , const Record& ) const
{ return . n < b . name ;
};

struct Cmp_by_addr {
bool operator ( } ( const Record& , const Record& ) const
{ return strncmp (a . addr , b . addr , 2 4) < ; } // ! ! !
};
21 .4. 839

__name .
operator ( ) name, <
string. Cmp_by_addr
. ,
- , 24 (
). ,
,
. - :
, ,
STL.
strncmp ( ) ,
,
, .
, ( . . , . 1 1 .3).

21 .4.. -
( )

. , ,
,
.
- ( 1 5.3.3). , -
-
( ( ) )
,

. , .
11 . . .
sort (vr . beqin ( ) , vr . end ( ) , 11
[ ] ( const Record& , const Record& )
{ return a . name < b . name ; }
) ;
11
sort (vr . beqin ( ) , vr . end ( ) , 11
[ ] ( const Record& , const Record& )
{ return strncmp ( a . addr , b . addr , 24) < ; }
) ;
11

"

?" , , Cmp_y_name Cmp_y_addr
?
, , find_if ( ) 2 1 .4.
,
:
840 21

void f ( list<doue>& v, int )


{
auto = find_if (v . eqin ( ) , v . end ( ) , Larqer than ( l) ) ;
if (p ! =v . end ( ) ) { / * > 3 1 */ )
auto q = find_if (v . beqin ( ) , v . end ( ) , Larqer_than (x) ) ;
if (q! =v . end ( ) ) ( / * > */ }
11

void f ( list<doule>& v , int )


{
auto = find_if (v . beqin ( ) , v . end ( ) ,
[ ] (doule ) { return > l ; } ) ;
if (p ! =v . end ( ) ) { / * > 31 * / }
auto q = find_if (v . eqin ( ) , v . end ( ) ,
( & ] (doule ) { return > ; } ) ;
if (q! =v . end ( ) ) { / * > */ }
11

-
.

2 1 .5.
STL
: , ,
..
. ,

STL. .


x=accumulate (b , e , i) ; ,
{a,b,c,d} i+a+b+c+d.
i
x=inner_product
( , , 2 , i ) ; ,
{a,b,c, d} {e,f,g,h} i+a*e+b*f+c*g+d*h.
i
r=partial_sum (b , e , r) ,
; ,
{a,b,c,d} {, +, ++,
a+b+c+d}
21 .5. 841


r=adjacent_difference
( , , 2 , r) ; ,
{a,b,c,d} {a,b-a,c-b,d-c}

<numeric>.
,
.

2 1 .5. 1 . accumulate ( )

accumulate ( ) . .
.
template<typename In , typename >
11 Inpu t_i tera tor<In > () Number<T> ()
accumulate ( In first , In las t , init)
{
while (first ! =last) {
init = init + *first;
++first;

return init ;

init.
[first,last) .
ini t, .
(accumulator). .
int [] = { 1 , 2 , , 4 , 5 } ;
cout << accwnulate ( a , a+sizeof (a) /sizeof ( int) , 0 ) ;

1 5. .. + 1 +2+3+4+5 (
). , accumulate ( )
.
void f (vector<doue>& vd, int* , int n )
{
doue swn = accwnulate (vd . eqin ( ) , vd . end ( ) , . ) ;
int swn2 = accwnulate ( p , p+n , 0 ) ;

() .
accumulate ( ) .
.
.
842 21

void f ( int* , int n)


{
11 i n t :
int l = accwaulate (p , p+n , ) ; 11 i n t
long l = accwaulate (p, p+n , long ( O) ) ; 11 l ong
doue 2 = accwnulate (p , p+n , . ) ; 11 d

long
. int. doue
( ) int, ,
, . 24
.

,
.
void f (vector<doue>& vd , int* , int n)
{
doule l = ;
l = accwnulate (vd . egin ( ) , vd. end ( ) , sl) ;
int s2 = accwnulate (vd. begin ( ) , vd . end ( ) , s2) ; 11
float s = ;
accwnulate (vd . egin ( ) , vd . end ( ) , s) ; 11


accumulate ( ) -
. -
s2,
; .
s accumula te ( ) ( ; .
8.5.3), ;
.

2 1 .5.2. accumula te ( )
, accumulate ( )
.
, - ,
, - STL
accumulate ( ) ,
.
template<typens.e In , typename , typename BinOp>
11 Inp u t_ i tera tor<In > () , Number<T> ()
11 Bina ry_ opera tor<Bi n Op , Va l ue_ type<In> , T> ()
accwnulate ( In fir t , In las t , ini t , BinOp )
{
21 .5. 843

while (first ! =last)


init = op (ini t , *first) ;
++first ;

return init;

,
. .
array<doue , 4> = { 1 . 1 , 2 . 2 , 3 . 3 , 4 . 4 ) ;
cout << accumulate ( a . egin ( ) , a . end ( ) , 1 . 0 , multiplies<doue> ( ) ) ;
35. 1 384, .. 1 .0* 1 . 1 *2.2*3.3*4.4 ( 1 .0 -

). multiplies<doue> ( ) ,
.
. ;
mul tiplies<doue> doule,
mul tiplies<int> int ..
: plus (), minus
(), divides modulus ( ).
<functional> ( .6.2).
, -
1 . .
sort ( ) (. 2 1 .4.2),
, ,
. . -
, .
struct Record {
doue unit_J>rice ;
int units ; //
11 . . .
1;
accnula te
uni ts Record.
.
doule price (doule v, const Record& r)
{
return v + r . unit_J>rice * r . units ; //
//

void f ( const vector<Record>& vr)


{
doule total = accumulate (vr . begin ( ) , vr . end ( ) , O . O , price) ;
11 . . .
844 21

" " .
- ,
.
:
,
,
( , ).

. .

&
vector<Record>,

. .

2 1 .5.. inner_Product ( )
,
.
11 (inner product)
(,
; . 24. 6). ,
STL.
teplate<typename In , typenae In2 , typena.e >
// Inpu t_ i tera tor<In> Inpu t_ i tera tor<In2>
// Number<T> ( 1 9 . 3 . 3)
inner_product ( In first , In last , In2 first2 , init)
// :
(
while (first ! =last) (
init = init + ( *first) * ( *first2 ) ; //
++firs t ;
++first2 ;

return ini t ;


.
.
. , - Alcoa
2.4808.
,
21 .5. 845

. ,
.
11 -
vector<doue> dow_price = { //
81 . 86 , 34 . 69 , 54 . 45 ,
11 . . .
};

list<doue> dow weight = { //


5 . 85 4 9 , 2 . 4808 , 3 . 8940 ,
11 . . .
};

doule dji_index = inner_product ( // (weigh t , va l ue)


dow_price . begin ( ) , dow_p rice . end ( ) ,
dow_weight . begin ( ) , . } ;

cout << " DJI " << dj i_index << ' \n ' ;

, inner_product ( )
.
: .
,
, .
. inner_product ( )
, ;
.

.
, -
vector, - list.

2 1 .5 . 4. inner_product ( )
inner_product ( ) ,
accumula te ( ) .
inner_product ( ) : -
,
accumula te ( ) , - .
template<typename In, typename In2 , typename ,
typename BinOp , typename BinOp2>
11 Inp u t_i tera tor<In > , Inp u t_ i tera tor<In2>,
11 Number<T>, Bina ry_opera t i on <Bi n Op , T, Va l ue_ type<In> () ,
// Bina ry_opera t ion<Bin 0p2 , T, Va l ue_ type<In2> ()
inner_product ( In firs t , In las t , In2 first2 , ini t , BinOp , Bin0p2 2 )
{
while ( firs t ! =last) {
846 21

init = op (init , op2 ( *first, *first2 ) ) ;


++first;
++first2 ;

return init;

2 1 . 6.3 -
inner_yroduct ( )
.

2 1 .6.
vector , .
map,
(, ) -
; , my_yhone_book
[ "Nicholas " ] .
map
unordered_map (. 2 1 .6.4) . ,
map unordered_map, ,
(associative arrays) . - (hash
taes) - (red-ack trees) .
.
. (associative contalners).

.


map -
(, )
set -
unordered_map -
(, )
unordered set -
multimap - map,

multiset - set,

unordered_multimap - unorde
red_map,
unordered multiset - unordered_
set,
21 .6. 847

<map>. <set>,
<unordered_map> <unordered_set>.

2 1 .6 . 1 . map
:
.
. ,
, . ,
1 ; -
. list
vector,
. .
map , ,
. .
int main ( )

.a.p<strinq , int> words ; // ( , )

for ( s trinq s ; cin>>s ; )


++words [ s ] ; // words

for ( const auto& words )


cout << . first << 11 " << p . second << ' \n ' ;


++words [ s ] . main ( ) ,
words - map, (string, int):
, words string int.
, string words
int. ,
words s ( ,
). words [ s ]
int, s. .
words [ " sultan" ]

" sul tan" ,


words ,
int, .. . words
(" sultan" ,0). , " sultan"
, ++words [ " sul tan " ] " sul tan"
1 . , map , " sul tan"
, ( " sul tan" , ) , ++
. 1 .
848 21

: ++words [ ]

. 1 .
.
for ( s trinq s ; cin>>s ; )
++words [ s ] ; // words

( )
.
. map .
STL. map<string ,
int> pair<string , int>. pair
first, second. :
-

for ( const auto& : words )


cout << . first << " : " << . second << ' \n ' ;


h ++ Progrnming Lagrge.
++ is general purpose programming language designed to make
programming more enjoyae for the serious programmer. Except for minor
details, ++ is superset of the programming language. In addition to
the facilities provided , ++ provides flexile and efficient facilities for
defining new types.
.
: 1
++ :
, : 1
Except : 1
In : 1
: 2
addition : 1
and : 1
: 1
defininq : 1
desiqned : 1
details , : 1
efficient : 1
enjoyae : 1
facili ties : 2
flexie : 1
for :
qeneral : 1
is: 2
lanquaqe : 1
lanquaqe . : 1
ma.ke : 1
minor : 1
21 .6. 849

more : 1
new : 1
of : 1
proqraier . : 1
proqrl.lllllli nq :
provided: 1
provides : 1
purpose : 1
serious : 1
superset : 1
the :
to : 2
types . : 1


. (. . 1 3).

2 1 .6.2. map
?
, . , STL
, -
- - .
, , ,
.
( ; .
20.4). Node .
-.

first

map: second

Node* left
Nod e* right

map<Fruit , int>
, (iwi, 1 00), (Quice,O).
(Plum,8), (Apple,7), (Grape,2345) (Orange.99).
850 21

Node first,
:
left->first < first && first < riqht->first

, :
;
, .
,
. .
. ,
. ,
(Orange, 99) . ,
. (balanced),
( )
.
, ,
. .
,
.
,
, . ,
N , ,
log2 N . , N/2 ,
,
, (
N ). (. 2 1 .6.4.)
, .

- , ,

.
left->first < first && first < riqht->first
21 .6. 851

,
. Apple iw1,
. ,
. ,
map .
,
, . ,
. , , -
, .
.
template<typename , typename Value , typename Cmp = less<ey>>
// Bina ry_opera t i on <Cmp , Va l ue> () ( 1 9 . 3 . 3)
class map {
11 . . .
usinq value_type = pair<Key , Value> ; // (Key, Va l ue)
usinq iterator = sometypel ; //
usinq const iterator sometype2 ; //

iterator beqin ( ) ; /!
iterator end ( ) ; 11

Value& operator [ ] ( const & k) ; 11 k

iterator find (const & k) ; 11 k ?

void erase (iterator ) ; // ,


11 (key, va l ue) :
pair<iterator , bool> insert ( const value_type& ) ;
11 . . .
};

map
<>.
Node*, ,
.
vector
list (. 20.5 .4) .
, pair
< , Value>. STL.
template<typename Tl, typename 2>
struct pair { // s td : :pa i r
usinq first_type = Tl ;
usinq second_type = 2 ;

Tl first ;
2 second;
11
};
852 21

template<typename Tl , typename 2>


pair<Tl , T2> make_pair (Tl , 2 )
{
return { , } ;

pair
make_pair ( ) .
, map
, . ,
. ,
:
(Apple , 7 ) (Grape , 2345) (iwi , 100) (Orange , 99 ) ( Plum, 8 ) (Quince , 0 )

.
insert ( ) ,
, , . ,
, (, ).
bool, true, (, )
insert ( ) .
, , bool
fal se.
, ,
, (
Cmp map). .
map<string , doue , No_case> m;
No_case (.
2 1 .8).
less<Key>. .. "" .

2 1 .6.. map
,
- 2 1 .5.3.
, vector
, .
.
,

- ("',2.4808) .
, ('J\A",34.69).
, ClllA,
,
21 .6. 1 853

('M','1\lcoa lnc."): ,
.
, (,
).
ap<string , doule> dowyrice = { // -
11 ( , ) ;
// . www . dj indexes . com
{ " " , 81 . 86 } ,
{ "" , 34 . 6 9 } ,
{ "" , 54 . 45 } ,
11 . . .
};
, (, ),
.
m&p<string , doule> dow_weiqht = { // ( , )
{ " " , 5 . 854 9 } ,
{ "" , 2 . 4808 } ,
{ "" , . 8 940 } ,
11 . . .
};
(. ) .
ap<strinq , strinq> dow_name = { // ( , )
{ " " , " . " } ,
{ "" , "Alcoa Inc . " } ,
{ "O" , "Altria Group Inc . " } ,
11 . . .
};

.
.
doule alcoayrice '"' dowyrice [ "" ] ; 11
doule boeinqyrice = dow_price [ "" ] ; //

// :
if (dowyrice . find ( " INC" ) ! = dow_price . end ( ) )
cout << " Intel .\n" ;
.
, first, second. -

// -
for ( const auto& : dowyrice) {
const strinq& symol = p . firs t ; //
cout << symol << \t '
<< p . second << ' \t '
<< dow_nae [ symol ] << ' \n ' ;
854 21

,
. , ,
2 1 . 5 .3.
.
,
map<string , doule>.
doue weiqhted_value (
const pair<strinq , doue>& ,
const pair<strinq , doule>&
) / /
{
return a . second * b . second;


inner_product ( ) .
doule dj i_index =
inner_yroduct (
dow_yrice . beqin ( ) , dow_yrice . end ( ) , 11
dow_weiqht . eqin ( ) , //
., //
plus<doule> ( ) , 11
weiqhted_value) ; 11

.
? map,
. . , -
map , . .
dow ;
vector. .
map ,
.
find ( ) ,
, map.


.

.

2 1 .6.4. unordered map _

vector,
find ( ) ,
.
21 .6. 855

N;
. O(N).
map,

.
.
. N . log2 N.
O(log2 N), .. log2 N. .
, O (N).

16 1 28 1 024 1 6 384 l 048 576 l 073 74 1 824


4 7 10 14 20 30
,

. (
map) , (
vector find ( ) ).
.
, .
map. . -
. .
vector. -
(hash value), . , -m -
11 (hash t). ,
-. , -
,
. .
. . -
. ()
. .. 0( 1 ) .
. .
500 -.
-
unordered_map ( )
( -
) .
() .
-.
vector.

1 + f ++ 1
- - - - - - - - - - - - - "

map ( ).
856 21

unordered_map (-).

unordered_map STL
-. map -
, vector - . STL
,
. ,
- . .
vector.
.

map.
(
"").
unordered_map,

(
-).
unordered_map.
string int ,
map. , .
.
- - 2 1 .6. :
unordered_ma.p<strinq , doue> dow_price ;

for ( const auto& : dow_price) {


const strinq& symol = p . first ; //
cout << symol << ' \t '
21 .6. 857

<< p . second << ' \t '


<< dow_name [ syol ] << ' \n ' ;

dow .
,
.
, - ,
.
:
.
++
,
++
(Thchnical Report), .
, , , ,
, - hash_map.


#include <unordered_map>.
, , unordered_map
++.
unordered_map,
(. , , www . oost . org).

21 .6.5. set
set ,
, .
set .
first

set:
Node* left
Node* right

set, (. 2 1 .6.2),
:
set? , ,
, -
. - (
); - .
- w", -
858 21

/ Plum
--
i-; -;-:
, _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ J

,
, . .
struct Fruit {
string n ;
int count;
doule unit_yrice ;
Date last_sale_date ;
11 . . .
};

struct Fruit_order {
bool operator ( ) ( const Fruit& , const Fruit& ) const
{
return . n< . n ;

};

set<Fruit , Fruit_order> inventory ; //


// Frui t Frui t_Order

,
, STL.
set ,
(opera tor [ ] ( ) ). ,
" ".
insert ( ) erase ( ) . , map set
push_back ( ) :
set, .
insert ( ) :
inventory . insert (Fruit { "quince" , 5 } ) ;
inventory . insert (Fruit { "apple" , 200 , 0 . 37 } ) ;

set map
, ,
. set (,
), map ( . 2 1 .6.3),
:
21 .7. 859

for (auto = inventory . egin ( ) ; p ! =inventory . end ( ) ; ++)


cout << * << ' \n ' ;

. ,
<< ,. Frui t.

2 1 .7.
2 1 .2 find ( )
". , r .
, .
. , -
? ( )
find ( ) . STL ( ) .


( , , ) [,)
[,+ (-) )
unique_ ( , , ) [,)
[,+ (-) )
,

[,)
copy_if ( , , , ) [,+ (-) ) ,

2 1 .7. 1 .

( ) :
template<typena.me In , typename OUt>
// Inpu t_ i tera tor<In > () Ou tp u t_ i tera tor<Ou t > ()
OUt copy ( In first , In last , Out res )
{
while (firs t ! =last)
*res = *first ; 11
++res ;
++first ;

return res ;

, ( )
, ,
:
void f (vector<doue>& vd, list<int>& li)
11 i n t
// d
860 21

if (vd . size ( ) < li . size ( ) )


rrr ( " Ji " ) ;
copy (li . eqin ( ) , li . end ( ) , vd. beqin ( ) ) ;
11 . . .

,
.
STL:
,
. ,
.
.
STL
:

. . ,
, .
.

2 1 .7 . 2.
" "
" ".
-.
( ) .
.
.

++.

* .
.
.
ostream_iterator<strinq> oo { cout } ; // *
11 cou t
* = " Hello , " ; // co u t << "He l l o , "
++ ; // "
// "
* = "World! \n" ; 11 co u t << "Wor ld ! \ "

, .
ostream_i terator<T>.
21.7. 861

: ,
.
i s tream_i tera tor<T>
.
istream_iterator<string> i i { cin } ; // * i i
-
11 cin
string sl = *ii ; // cin >> s l
++ii ; // "
!! "
string s2 = *ii ; // cin >> s2

os tream_i tera tor i s tream i tera tor,


_

( ) -. ,
- - :
int main ( )

string from, to ;
cin >> from >> to ; //

ifstream i s { from} ; //
ofstream os { to } ; //

istream_iterator<string> ii { i s } ; //
istream_iterator<string> eos ; //
ostream_iterator<string> //
oo { os , " \n" } ;

vector<string> b { ii , eos } ; //
sort (b . begin ( ) , b . end ( ) ) ; /!
copy (b . begin ( ) , b . end ( ) , ) ; 11

eos - , " ".


istream ( eof),
i s tream i terator i s tream_i terator
_

( eos).
,
vector .
{ , } , , :
" l.) ". ,
{ ii , eos } .
-

>> push_
back ( ) . -
.
vector<string> b (max_size ) ; // ! !
copy ( ii , eos , b . egin ( ) ) ;
862 21

, .
,
, .
.



, , .
" '',
, ,
. ,
,
.

,
. , , ,
, ?
, ,
, .
, unique_
( ) ( ) . unique_copy ( )
. , .
( ) ,
the man bit the doq

it
doq
man

the
the

unique_ ( ) ,
:
it
doq
man
the

?
, ostream_
i terator ( ) , -
:
ostream_iterator<strinq> oo { os , 11\n" } ;
21.7. 863

, -
JI . , ,
. ? .
:
ostream_iterator<string> oo { os , " " } ;

. .
it dog man the

2 1 .7 .. set

: set
vector.
int main ( )
{
string from, to ;
cin >> from >> to ; 11
11

ifstream i s { from} ; 11
ofstream os { to } ; 11

set<string> { istream_i terator<string> { is } ,


istream_iterator<string> { } } ;
11 :
copy (b . begin ( ) , b . end ( ) ,
ostream_iterator<string> { os , " " } ) ;

set, .
. , set
. . -

.

21 . 7.4. copy_if
( ) - .
unique_copy ( ) ,
.
, JI .
template<typename In , typename Out, typename Pred>
11 Inp u t_ i tera tor<In> () , Ou tp u t_ opera tor<Ou t > ()
11 Predica te<Pred, Va l ue type<In>> ()
_
864 21

)
Out copy_if ( In first , In last , Out res , Pred
11 ,

while ( first ! =last) {


if (p ( *first) ) *res++ *first ;
++firs t ;

return res ;

Larger_than 2 1 .4,
, , ,
.
void f ( const vector<int>& v)
11 , 1

vector<int> v2 ( v . size ( ) ) ;
copy_if ( v . eqin ( ) , v . end ( ) , v2 . eqin ( ) , Larqer_than ( ) ) ;
11 . . .

-
ISO 1 998. ,
++,
_if. ,
.

2 1 . .
. ,
, ,
map set, .
STL
sort ( ) , .
sort ( ) <.
.
teplate<typename Ran>
11 Ra ndom_a ccess_ i t era t or<Ran> ()
void sort (Ran first , Ran last) ;

teplate<typename Ran , typenae Cmp>


11 Ra ndom_ a ccess_ i t era t or<Ran> ()
11 Less_ than_compa ra e<Cmp , Va l ue_ type<Ra n >> ()
void sort (Ran firs t , Ran last, Cmp cmp) ;

, .
2 1 .8. 865

struct No_case { // l o werca se (x) < l o werca se (y) ?


bool operator ( ) (const strinq& , const strinq& ) const
{
for ( int i = ; i<x . lenqth ( ) ; ++i )
if (i == y . lenqth ( ) ) return false ; 11 <
char = tolower ( x [ i ] ) ;
char = tolower (y [ i ] ) ;
if (<) return true ; 11 <
if (<) return false ; 11 <

if (x. lenqth ( ) ==y . lenqth ( ) ) return false ; // ==


return true ; // < ( )

1;

void sort_and_yrint (vector<strinq>& vc)

sort (vc . eqin ( ) , vc . end ( ) , No_case ( ) ) ;

for (const auto& s : vc)


cout << s << ' \n ' ;

,

find ( ) ; -
, . .
.
. .
.
, !
. ,

, (
).
,
,
( ).
(
) . ,
.

, find ( ) (
). -
inary_search ( ) equal_range ( ) . -
866 21

""? ,
,
inary_search ( ) find ( ) .
, , inary_search ( ) (
O(log2 N)) 200
, find ( ) (. 2 1 . 6.4).
inary_search .
template<typename Ran , typename >
bool binary_search (Ran firs t , Ran last , const & val ) ;

template<typenae Ran , typenams , typenae Cmp>


bool binary_search (Ran first , Ran las t , const & val , Cmp cmp) ;

,
. .
" ", , . -
. inary_search ( ) ,
.
void f (vector<string>& vs ) 11 vs
{
if (inary_search (vs . begin ( ) , vs . end ( ) , " starfruit" ) )
11 "s t a rfrui t "

11

, inary_search ( ) - ,
, .
,
lower_bound ( ) , upper_bound ( ) equal_range ( ) ( 23.4 .5.4).
, ,
, , ;
;
,
.

2 1 .9.
,
, .
I,),
, .
-

(20.3). ,
.
21 .9. 867

void test (vector<int> & v)


{
sort ( v . begin ( ) , v . end ( ) ) ; // v
// v . begi n () v . end ()

. ,
:
void test (vector<int> & v)
{
// v :
sort (v. beqin ( ) , v . beqin ( ) +v . size ( ) /2 ) ;
// v :
sort ( v . eqin ( ) +v . size ( ) /2 , v . end ( ) ) ;

,
- ,
. :
void test (vector<int> & v)
{
sort (v) ; / / v

sort ( )
, :
template<typename > // Con t a i ner<C> ()
void sort (C& )
(
std : : sort ( c . beqin ( ) , c . end ( ) ) ;

,
std_lib_facil i ties . h.
,

.
template<typename , typename V> 11 Con tainer<C> ()
Iterator<C> find (C& , Val v)
(
return std : : find ( c . beqin ( ) , c . end ( ) , v) ;

, I terator<C> .
868 21



.
1 . s truct I tem { string name ; int iid ; doule
value ; / * . . . * / } ; , vector<Item> vi
, .
2. vi name.
3. vi iid.
4. vi value;
(..
).
5. I tem ( "horse shoe" , 99 , 12 . 34 )
I tem ( " Canon S400 " , 9988 , 499 . 95) .
6. I tem vi,
name .
7. I tem vi ,
iid .
8. list<Item> vector
<Item>.
map.
1 . map<string , int> msi .
2 . (, ),
msi [ 11 lecture" ] =21 .
3 . (, ) cout .
4. (, ) msi .
5. , (. )
cin msi .
6 . (, )
msi .
7 . msi cout.
8. () msi .
9. map<in t , string> mis .
1 0 . msi mis ; ,
msi ( " lecture 11 , 21 ) , mis
(21 , 11 lecture11 ) .
21. 9. 869

1 1 . mis cout.
, vector .
1 . ( 1 6
) vector<doue> vd.
2. vd cout.
. vi vector<int>
, vd: vd
vi .
4. cout (vd [ i ] ,vi [ i ] ) .
5. vd.
6. vd vi.
7. reverse.
( );
vd
vd cout.
8. vd
.
9. vector<doue> vd2
vd, .
1 0 . vd .


1 . STL.
2 . find ( ) ? .
. count_if ( ) ?
4. sort ( , ) ?
5. STL ?
6. STL ?
7. STL " ""?
8. ?
9. ?
1 0 . ?
1 1 . accumula te ( ) ?
1 2 . inneryroduct ( ) ?
1 3 . ?
.
870 21

1 4. JUiacc list ?
1 5. .
1 6. ?
1 7. map?
1 8. vector?
1 9. unordered_map, ()
map?
20. set map?
2 1 . mul timap map?
22. ( ) .
I?
23. ?

accumulate map -
inary_search set
( ) sort ( )
copy_if ( ) unique_ ( )
equal_range ( ) unordered_map
find ( ) upper_bound ( )
find_if ( )
inneryroduct ( )
lower_bound ( ) -

1 .
, .

2. STL -
.
3. count ( ) . .
4. count_if ( ) . .
5. ,
end ( ) . , ?
find ( ) count ( ) ,
. .
.
6. JUiacca Frui t 2 1 .6.5
Frui t set. , 11
2 1 . 9. 871

r?
set<Frui t*>;
.
, set<Fruit* , Fruit_comparison>.
.
7. vector<int> (
). ,
. . ,
?
list<string>. .
?
, ,
STL7
8. .
2 1 .6. 1 , .
, .
: ++, ++ : .
9 . Order ().
, , r vector<Purchase>.
Purchase name, uni tyrice count,
.
Order.
Order. ,
Order, vector
<Order>. .
,
Order, ,
list<Order>. (
) .
, std : : merge ( ) .
1 0.
. Purchase (
) unit_Price*count.
1 1 .
.
1 2.
; , " Joe, " "
Hardware" "
Clothing". :

.
872 21

1 3 . , ""
, ;
, 'I)' ,
. don't do ri ( ..)

(. ships ship). .
. ,
s. ship,
ships. 1)' .
5 (, ).
1 4. (
), "
sblp?". " ?". "
?", " ?",
" s" " ,
".
1 5.
.

STL ISO ++, -


. ,
.
:
, . -
STL,
. , STL
-

, ,

. -
, - STL
, ,
.
IV


JG,.e)A, - "".
"
, , '
- //' ' ..u)Ut-
-- l (.41-, ?)



. .

.
++ .
, .
. -
,
. ,
:
- ;
.
.
876 22

2 2 . 1 . , 22.2.2.

22.1 . 1 . 22.2.. Algol
22.2.4.
22.1 .2. Simula
22.1 .. 22.2.5.
22.2. 22.2.6. -
+ +
22.2 . 1 . 22.2.7.
22.2.8.

22. 1 . ,
" - ", -
(Henry Ford) .
: ", , -
". . .
. : "95% - ",
r ( ,
95%, , ).
.
. .
, .
.
, . ""
. .
,

, , ,
, , , ,
. ,
, ,

.

.
. !
, 1 .
22.1 . . 877

22. 1 . 1 .
? ?

.
.
.
.
.
.
.
.
: " !" ,
. ,
-
,
.
?
.
.
.
.
.
.
.
.
.
(, Linux.
Windows, OS/2, , ).
.
,
.
.
.
878 22

(, -
- ).
, .
.
.
(, ).
(,
).

.

(, ).
.
(Microsoft,
I ..).
, .
,
: , ,
,
. , -
, :
. .

; , ,
,
. , , , (
, )
,
.
.

, , -
. , ,
.

22.1 .2.
h ++ Progrwnming Language
" ++ - ,
,
22. 1 . , 879

" . ?
? ,
?
?
? ,
, , .
. (Doald nuth) : "
Alto , ". Alto -
Xerox Palo Alto Research Center (PARC),
, ,

.

,
. , . -
,
?
, ,
, .
,
, -
. , , -
, ,
, ..
. "".
.
.
,
,
.
, .
.
.
.
.
.
, ,
. -
. , ,
,
( ). (-
880 22

),
, r .
, , (,
). (
)
. .
r
. ,
(. -
1 4. 1 ) . .
,
.
.

.
, ,
.
. ,
. .
. . -
, , .
. .
.
. "
. , - ".

22.1 .2. 1 .
, .
. . ,
"", .

. .
.
.
:
.
.
, .
. () -
.
. .
22. 1 . . 881

,
. . .
,
.
STL,
.

. .
.
- .
, .
.
. .
. .
(. .
)
(
) .
.
(
). , .
-
. .
.
,
, ,
(
). .
.
.
. .
.
, .
.
.

22.1 .2.2.

.
882 22

. ,
.
. ,
,
.
,
,
. :
,
.
,
.

(, , ..).
.
,
: ( , )
, ,

. ,
,
, .

.
26. - . -
.
,
.

22.1 .2..
- - -
, - ,
. -
.
. .
(, Month
Color), (, int).
. ,
sort ( ) ( )

; , ,
22.1. . 883

.
" ,
, ,
Object - before ( ) ,
vector : : sort ( ) ".
sort ( ) ,
, ,
-. ..

. ,
, (
, Circle Shape)
(, vector<T>
, ).
, ,
-

11 . . sort ( )
.
< ( ,
sort ( ) r,
). .
.
.
. ,
, ,
. .
( ),
. (
) . .

. ,
sort ( , , ) ,
, sort ( , ) .
"". ,
sort ()
"" sort ( c , op)
.

22.1 .2.4.

, .
.
884 22

, .
, .
(, ) vector<pair
<string , Value_type>>.
,
map<string , Value_type>. (,
, ) . .
vector<pair<string , Value_type>>
, , string [max] Value_type [max] .

.
int ( ) void* (
- , .
).
,
,
. ,
,
. , .
Phoneook . Phonebook
.
,
(
-
). ,
.
. .
,
.
( 2 5 . 2 . 2).
( )
. .
. Phoneook,
, , s tring [max] Val ue_type [max]
map<string , Value_type>.
, - .
, ,
.
,
. ,
,
(
- ).
22.1 . . 885

22.1 .2.5.
- .
(, . ,
..). ,
.
,
, .
(reuse) - ,
- .
. , ,
,
. ,
22. 1 .3, ,
, .
,
:
,
.

.
, "-
" .
.
1 2

1 2

tJ EJ EJ
.
, ,
,
. ( )
( )
, .
.. , ,
.
? ,
?
886 22

1 2

__[--, J
.

-___ ) _ _
__
' : ;
____________J ------ ------J

,
. ,
, ,
. ,
,
. ,
(. 9.7 25.4.2) -
(. 9.4.3).

22.1 .2.6.
-
. , ,
.
,
,
.
.
.
(
),
.
(
),
.
,
. ,

. STL (. 20 2 1 ,
. 4-6) . (,
- .
), ,
.
( " ' ") "
, .
22. 1 . . 887

,
( ) .
.
.
.
.

(. 1 9.5).

ISS (" It Simple, Stupid" - " , ").
. ISS - -
.
, " simple things simple" ("
") " it simple: as simple as possie, but simpler" ("
. ").
(Albert Einstein) .
,
. : "
?"

22.1 ..
.
. ++
, .
.
.
- .
.
( )
. .
(functional programming).
(logic programming). (rule-based
programming) . (constraints
based programming) -
(aspect-oriented programming). ++
. ,
.
u . -
. .
,
888 22

sqrt ( ) cos ( ) . ++
(. 8). ,
,
.
struct.
(, - -
) . ,
- -
.
. -
,
, .
( 24.3-24.6).
(, ) .

string vector,
,
. ""
.
,
.
- r.
- ,
.
- Shape, 1 4.
,
.
: .
, . -
, "?"
?
?
.
-
, ,
.

high ( ) , 20. find ( ) sort ( ) STL
.

. (. 20 2 1 .)
22. 1 . . 889

.
() .
: -
, -.
,
. " " ,
, ,
. Shape (
Simula; . 22.2.4),
- .
:
void draw_all (vector<Shape*>& v)
{
for (int i = ; i<v . size ( ) ; ++i ) v [ i ] ->draw ( ) ;

" -
".
,
draw ( ) Shape;
. Circle Circle : : draw ( ) ,
Open_polyline Open_polyline : : draw ( ) .
-

vector<Shape*>
: ( ).
. ,

.
void draw_all (vector<Shape*>& v)
{
for_each (v. begin ( ) , v . end ( ) , mem_fun ( &Shape : : draw) ) ;

for_each ( ) ,
,
( .5. 1 ) . ,
( -).
f () . -,
p->f ( ) .
. ,
( Shape : : draw ( ) )
.

mem fun ( ) ( .6.2). ,


for each ( ) mem_fun ( ) , ,
_

- ;
.
890 22

, mem_fun ( ) () ,
. ,
( )
( ).
,
, ++.

? ,
. ,
for_each ( ) "
", for,
. , for_each ( )
, ( ).
.
" ".
( -
). .
.
? ( ) .
teplate<typena.e Iter> void draw_all ( Iter , I ter )
{
for_each (b , e , 111.81_
11 fun ( &Shape : : draw) ) ;

.
,
Shape.
Point { , 100 ) ;
Point 2 { 50 , 50 ) ;
Shape* [ ] = {new Circle {p , 50 } , new Trianqle { p , p2 , Point { 25 , 25 } } } ;
draw_all ( a , a+2 ) ;
,
:
teplate<typena.e Cont> void draw_all (Cont& )
{
for (auto& : ) p->draw ( ) ;

, ++ 1 4 (.
1 9. 3.3).
void draw_all (Container& )
{
for (auto& : ) p->draw ( ) ;
22.2. 891

I , -,
.
-
.
, ,
(multi-paradigm programming).
, : "
", . ,
,
, .
,
.

22.2.

! , , .
()
++.
.
2000 ; ,
. ,
, .
- http : //research . ihost . com/
hopl/HOPL . html . ,
SIGPLAN HOPL (istory of Pro
gramming Languages - ).
, ,
. .
, ,
HOPL. , ,
, . ,
, ,
, .

, ( )
.
. ,
, " ,
!" ,
.
,
892 22 1

.
, , ,
(. . 5 6).
(.
) , ,
" " .
: , .
, ,
( ) . ,
. IBM Bell Labs,
Cambridge University,
. . ,
.
,
.
,
, ,
, .. ,
, .
.
. ,
.
( -
), , ,
,
, ,
. .
.

22.2. 1 .
- 1 949 -
.
.
(,
) . . (
) .
.
.
.
""; ,
. (,
) . ,
22.2. 893

"LD RO 1 23", ,
1 23, . -
.


, , (David Wheeler)
(University of Cambridge
Computer Laboratory) . 1 948 .
- ,
(, ; .
4.4.2. 1 ) . ,
( - ).
(. -
). 1 95 1
. !
(Maurice Wilkes) (. ) (Stanley
G) .
(
1 95 1 ),
(- )
(, (. 25. 5 . 6)
- (Burrows-Wheeler transform) -
, bz1p2).
(Bjarne
Stroustrup). , - .
,
.
(Fellow of the Royal Society).

1 . Burrows, . . and David Wheeler. " Block Sorting Lossless Data Compres
sion Algorithm." Thchnical Report 1 24, Digital Equipment Corporation, 1 994.
894 22 .1

2. Bzip2: www . bzip . org.


3. Cambridge Ring website: http : //koo . corpus . cam . ac . uk/projects/
earlyatm/cr82 .
4. Campbell-Kelly, Martin . "David John Wheeler. " Biographical Memoirs qf Fel
lows of the Royal Society, Vol. 52, 2006. ( .)
5. EDSAC: http : / /en . wikipedia . org/wiki/EDSAC.
6. nuth, Donald. h Art of Computer Programming. Addison-Wesley, 1 968
( ). "David Wheeler"
.
7. : http : / / en . wikipedia . org/wiki/Tiny_Encryption_Algori thm.
8. Wheeler. . J. 1he Use of Sub-routines in Programmes." Proceedings of the
1 952 National Meeting. (
1 95 1 .)
9. Wilkes, . V. , . Wheeler, and S. Gill. Preparation of Programs .for Elec
tronic Digital Computer. Addison-Wesley Press, 1 95 1 ; 2nd edition, 1 957.
( .)

22.2.2.

.
1 950-: 1 960-: 1 970-:

Simula

Fortran

COBOL

,
( ), .
,
.
- Fortran, COBOL Lisp. -
.
22.2. 895

22.2.2. 1 . Fortran
Fortran 1 956 . ,
.
R>rtran "Formula ranslatlon" (
-

).
, , .
, Fortran, ,
, ,
( )
.
Fortran

.
, . Fortran ,
(
, + sin () ).
, -
,
.
-,
Fortran
.
. Fortran
.
, .
. Fortran,
:
(
), ( )
( 1 00 . )
(, 8 ).
,
(
).
Fortran
, , , .
.
Fortran 11, IV, 77, 90, 95 03.
,
: Fortran77 Fortran90.
896 22

Fortran
I (John
Backus): " , .
". ? ,

(, ),
. Fortran
. ,
Fortran,
: - (Backus-Naur Fon - BNF).
Algol60 (. 22.2.3. 1 )
.
BNF , 6 7.

( ),
,
( - ,
). ,
. .
"" ,
.
Lisp (. 22.2.2.3),
STL ( 2 1 ).
22.2. 897

1 . Backus, John. "Can Programming Liberated from the von Neumann


Style?" Corunuications qf the , 1 977. (
.)
2. Backus. John. "h istory of FORRAN 1, 11, and . " SIGPIAN Noti
ces, Vol. 1 3 No. 8, 1 978. Special lssue: History of Programming Languages
Conference.
3. H utton, Graham. Programmig i Haskell. Cambridge University Press,
2007. ISBN 052 1 692695.
4. ISO/IEC 1 539. Programming Languages - Fortran. ( Fortran95.)
5. Paulson, L. . ML.for the Working Prograruner. Cambridge University Press,
1 99 1 . ISBN 052 1 390222.

22.2.2.2. COBOL
, , ,
COBOL (COmmon Business-Oriented Language -
) ( - ) ,
Fortran ( - ) ,
.
.
.
( ).
().

(
,
COBOL). ( ), COBOL
,
.
,
. .
COBOL CODASYL
1 959-1 960 CUlA (U.S. Depart
ment of Defense)
, .
FLOW-ATIC, .
,
(
, Fortran ).
898 22

Fortran, , COBOL
. 60, 6 1 ,
65, 68, 70, 80, 90 04.

(Grace Murray Hopper)


, (Yale
Unlverslty). -
ClllA . 1 ,
,
- .

"- (-
ClllA) ,
.



. , "
"
, ".
- (Anita Borg) .
"Grace Hopper Celebratlo of Women in Computing'', 1 994

,
"" (bug). , ,
.
22.2. 899

()
. ""
.

1 . . . : h ttp : / / terges tesoft . com/-eddysworld/hopper .


htm.
2. ISO/IEC 1 989:2002. lnJormation Technology - Programming Languages -

COBOL.
. Sammet, Jean . "h Early istory of COBOL." SIGPLAN Notices,
Vol. 1 3, No. 8, 1 978. Special Issue: History of Programming Languages
Conference.

22.2.2.. Lisp
Lisp 1 958 (John McCar
thy) (MIT)
(
: LISt Processing). Lisp ,
(
). ( , )
Lisp. , Lisp
.
Common Lisp Scheme.
(
++).
Lisp
- (, ).
Fortran COBOL
.
Lisp
.
. Lisp ,
, .
900 22


Lisp:
.
. Lisp (
Lisp)
.
.


(California lnstitute of Thchnology).
-
(Princeton University). .
.
MIT 1 962 .
(Stan
ford Al lab).
(arttficial intelligence), .

l . Abelson, Harold, and Gerald J. Sussrnan . Strncture artd Interpretation qf


Coputer Programs, Second Edition. MIT Press, 1 996. ISBN 02620 1 l 530.
2. ANSI INCIS 226- 1 994 (forrnerly ANSI 3.226: 1 994). Ameican National
Stadardfor Programming Language - LISP.
3. McCarthy, John. "History of LISP." SIGPLAN Notices, Vol. 1 3 No. 8.
1 978. Special lssue: History of Prograrnrning Languages Conference.
4. Steele. Guy L. Jr: Lisp: The Language. Digital Press. 1 990.
ISBN 1 5555804 1 6.
22.2. 901

5. Steele, Guy L. Jr. , and Richard Gabriel. "h Evolution of Lisp". Proceedings
of the Hlstory of Programming Languages Conference (HOPL-2).
SIGPI.AN Notices, Vol. 28 No. 3, 1 993.

22.2.. Aigoi
1 950- ,
, .
,

.
, IFIP (Internatioal
Federatio of Information Processing -
) ,
.
, ++,
.

22.2..1. Aigoi60
" " ("ALGOrithmic Language" - Algol),
IFIP 2. 1 ,
.
.
.
.
.
(, . .
).
r .
" "
Algol.
(, Fortran), (, COBOL),
(, Lisp). ..
Algol60 Fortran.
, Algol60
. . ,
Fortran, , Algol60
, , Cobol, ,
Algol60 , ,
Lisp, , Algol60 ,
902 22

( ,
)
, , , "
". .
. Algol60
-!
. Algol
.
, Algol60, ,
, .
(Peter Naur).
Algol60, (Edsger Dijkstra).


(University of Copenhagen)
(Thchnical University of Copenhagen - D),
Regnecentralen, .
1 950- 1 95 1
(Computer Laboratory in Cambridge),
,
. -
(BNF), ,
(
- 1 97 1 -
).
.
.
( -
22.2. 903

).
( "" (datalogi)
""; "
" (computer sc1ence), ,
- ).

(Edsger Dijkstra) -
. ,
(Mathematisch Centrum) .
,
(Eindhoven University ofThchnology), Burroughs
Corporation (University ofThxas (Austin)).
Algol,

,
- ,
. ''"
"'Ieclmlsche Hogeschool Eindhoven" - ,
. ,
"Go-To Statement Considered Harmful",
.
Algol .

Simula67

Algol58 Algol60 Algol68

Pascal
904 22

Simula67 Pascal.
(, ) .

1 . Dijkstra, EdsgerW. "Algol 60 ranslation: An Algol 60 ranslator for the x l


and Making ranslator for Algol 60". Report M R 35/6 1 . Mathematisch
Centrum (Amsterdam), 1 96 1 .
2 . Dij kstra, Edsger. "Go-Th Statement Considered Harmful". CommunicatiDns
q.f the ACM, Vol. 1 1 No. 3, 1 968.
3. Lindsey, . . "h istory of Algol-68". Proceedings of the istory of
Programming Languages Conference (HOPL-2). SIGPLAN Notices, Vol.
28 No. 3, 1 993.
4. Naur. Peter, ed. " Revised Report the Algorithmic Langue Algol 60". A/S
Regnecentralen (Copenhagen). 1 964.
5. Naur, Peter. "Proof of Algorithms General Snapshots". I, Vol. 6, 1 966,
. 3 1 0-3 1 6. (, ,
.)
6. Naur. Peter. "h European Side of the Last Phase of the Development of
ALGOL 60". SIGPLAN Notices, Vol. 1 3 No. 8, 1 978. Special lssue:
istory of Programming Languages Conference.
7. Perlis, Alan J . "h American Side of the Development. of Algol".
SIGPLAN Notices, Vol. 1 3 No. 8, 1 978. Special lssue: istory of Programming
Languages Conference.
8. van Wij ngaarden, ., . J. Mailloux, J. . L. Peck, . . . Koster. . Sint
zofT, . . Lindsey, L. G. L. . Meertens, and R. G. Fisker. eds. Revised Report
the Algorithmic Laguage A/gol 68 (Sept. 1 973). Springer-Verlag. 1 976.

22.2..2. Pascai
Algol68,
Algol, . Algol60,
Algol ( IFIP 2. 1 ) .

,
, -
. . (Niklaus Wlrth).
,
Algol. Algol68, ,
Pascal, Algol60.
Pascal 1 970 .
. .
,
22.2. 905

Fortran,
. Pascal
,
,
, Fortran .

Pascal (Niklaus Wirth)


(Technical University
of Switzerland in Zurich - ). ,
1 969 2004 . (
)
(University of California at Berkeley)
.

.
.
Algol W. Modula-2.
PL/360. Oberon.
Euler. Oberon-2.
Pascal. Lola (
Modula. ).


. .

. - ,
906 22

HOPL (History of Programming Languages)


.
, Pascal ,
. 1 980-
(Anders Hejlsberg) -
Borland.
Thrbo Pascal (, ,
),
, ++ (
).
(Thchnical
University in Copenhagen), , -
, , .
Delphi Borland # Microsoft.
Pascal .

Turbo Pascal Delphi

Pascal

Modula Oberon Oberon-2

1 . Borland/Thrbo Pascal. http : //en . wikipedia . org/wiki/Turbo_Pascal.


2. Hejlsberg, Anders, ScottWiltamuth, and Peter Golde . The # Program
ming Language, Second Edition. Microsoft . NET Development Series.
ISBN 032 1 334434.
3. Wirth, Niklaus. "h Programmtng Language Pascal"'. Acta InJonnatics. Vol.
1 Fasc 1 , 1 97 1 .
4. Wirth, Niklaus. "Design and Implementation of Modula"' . Sqftware -
Practice and Experience, Vol. 7 No. 1 , 1 977.
5. Wirth, Niklaus. "Recollections about the Development of Pascal"'. Proce
edings of the History of Programming Languages Conference (HOPL-2).
SIGPIAN Notices, Vol. 28 No. 3, 1 993.
22.2. 907

6. Wirth, Niklaus. Modrlla-2 d r. Proceedings of the Third


SIGPLAN Conference the istory of Programming Languages (HOPL-III).
San Diego, , 2007. http : //portal . acm . org/toc . cfm?id=l23884 4 .

22.2... Ada
Ada
. , ,

.
Pascal Simula (. 22.2.3.2 22.2.4).
Ada (Jean lchblah).
Simula Users' Group.
Ada
( 1 995 );
;
.
Ada
.
. .
,

, .
( 1 975- 1 998) 1 980
AJPO (Ada Joint Program Office).
1 979
(Augusta Ada Lovelace), (Byron).
(
),
(Charles Babbage), 1
( , !)
1 840-
. , .

1 (Henry Lucas).
.
( , "", 1 632).
" " 1 00 .
.
. , ,
. - .. .
908 22

Ada
, . )
, ,
. ( ),
.
.
ClllA
Ada ,
" Ada - , - !" Ada
" " ,

( , ++), ClllA
, ,
Ada.
. ,
,
ClllA.
, Ada .
, , ClllA
,
( ,
, ..).
. Ada
,
.
Ada 1 980 , ANSI
1 983 ( 1 983 -
!), ISO - 1 987 .
22.2. 909

ISO (, )
1 995 .
.

1 . Barnes, John . Programmig i Ada 2005. Addison-Wesley, 2006. ISBN


032 1 340787.
2. Consolldated Ada Reference Manual, consisting of the intemational stan
dard (ISO/IEC 8652: 1 995). Information Technology - Programming La
guages - Ada. as updated changes from Technical Corrigendum 1 (ISO /
IEC 8652: 1 995: 1 :2000).
3. Ada: www . adaic . org/ .
4. Whitaker. William . ADA h Project: h DoD High Order Laguage
-

Working Group. Proceedings of the History of Programming Languages


Conference (HOPL-2). SIGPIAN Notices, Vol. 28 No. 3, 1 993.

22.2.4. Simuia
Simula 1 960-
(Krlsten Nygaard) - (Ole-Johan Dahl)
(Norwegian Computing Center)
(Oslo Universlty). Simula, ,
Algol. Simula
Algol60.
Simula .
, -
. ,
. class
virtual ,
, ++ Simula.
Simula .
-
,
:
;

().
,
, .
910 22

- Simula67 ( -
, ) -
( ).
- ,
,
. ,
.
.
, ,
,
. 1 970-

(University of Aarhus) ,
.

(University of Oslo). 2002 ..
, ( - )
- ,
(Association for Computing Machiner - )
.
- .
. 1 968
. ( )
.
2000
(Commanders of the Order of Saint Olav).
- !
22.2. 911

1 . Birtwistle, G" 0-J . Dahl, . Myhrhaug, and . Nygaard : SIMULA Begin.


Studentlitteratur (Lund. Sweden), 1 979. ISBN 9 1 44062 1 25.
2. Holmevik, J . R. "Compiling SIMULA: Historical Study of Technological
Genesis" . IEEE Annals o..f the History o..f Computing, Vol. 1 6 No. 4, 1 994,
. 25-37.
3. Krogdahl, S. "The Birth of Simula" . Proceedings of the HiNC 1 Conference
in Trondheim, June 2003 (IFIP WG 9.7, in cooperation with IFIP TC 3).
4 . Nygaard , Kristen, and Ole-Johan Dahl. "The Development of the SIMULA
Languages". SIGPLAN NoUces, Vol. 1 3 No. 8. 1 978. Special lssue:
istory of Programming Languages Conference.
5. SIMULA Standard . DATA processing - Programming languages -

SIMULA. Swedish Standard, Stockholm, Sweden ( 1 987). ISBN 9 1 7 1 622349.

22.2.5.
1 970- , -
, -
.
,
Fortran.
.
( 27) .
(Dennis Ritchie)
(Com
puter Science Research Center) Bell Thlephone Laboratories
-, - (Murray Hill. New Jersey) .
91 2 22

. .

. (
++ )
.
,

( . 25.4-25.5).
"
"; . (
) , ,
, .
. .
48 .
. lint,
.

(Ken hompson)
Unix, ,
. -
Unix, - Linux
.
Lucent Bell Labs.

Bell 'Ielephone.
(Harvard University) "",
.
. (60 )
.
22.2. 91 3

1 974- 1 979 ++
Bell Labs. , - (Doug
Mcllroy) , ,
. ++,
Unix, .

(Brlan Kernlghan)
. - .
- The
Programmig Lagrge ( K&R
- ).
91 4 22

. ,

, . -
; ,
.
" ".
, . :
, ,
.

.
AWK - ,
(Aho, Weinberger Kernighan).
L - ( Mathematical Programming Language -
).
-
(Princeton University); ,
.
Bell Thlephone.
Bell Labs & Bell Labs,
& Labs Lucent Bell Labs.
(University of Thronto) "":

.
I .

,
BTL, 1 972

''
" - - - ,
\ , 1 967
'
,
, 1 960-


CPL , BCPL (Basic CPL),
(Cambridge University)
(Martin Richards)
(MIT) . .
. ANSI
ISO ++ (,
const).
22.2. 91 5

CPL
(Imperial College).
, ""
" Cambridge".
. ""
"Comblned" ("") . (
) Christopher
(Christopher Strachey). CPL.

l . : http : //cm . bell -las . com/


cm/ cs/who/bwk www . cs . princeton . edu/-bwk/ .
2. : http : //cm . bell-las . com/cm/cs/
who/dmr.
3. ISO/IEIC 9899: 1 999. Programming Languages - . (
.)
4. Kemlghan, Brian, and Dennis Ritchie. h Programming Laguage.
Prentlce Hall, 1 978. Second Edltion, 1 989. ISBN 0 1 3 1 1 03628.2
5.
Bell Labs: http : //cm . ell-las . com/cm/cs/alumni . html.
6. Rltchards, Martin . BCPL - The Language and Its Compiler. Cambridge
Unlversity Press, 1 980. ISBN 052 1 2 1 9655.
7. Ritchie, Dennis. h Development of the Programming Language. Proce
edings of the History of Programming Languages Conference (HOPL-
2). SIGPLAN Notices, Vol. 28 No. 3, 1 993.
8. Salus, Peter. Quarter Cent11ry of UNIX. Addison-Wesley, 1 994. ISBN
020 1 54 7775.

22.2.6. ++
++ -
.
.
.
.
- .
.

2 : . , . .
, 2- - . : ".. ", 2006.
91 6 22

++
Bell 'Ielephone
Laboratories - (Murray ), - (New Jersey).
, . .
- Unlx.


(Erhus). .
(Carnbrldge),
, (Davld Wheeler).
+ .

.
-
,
.
++ (
"- ")
.
Fortran.
.
22.2. 91 7


.
++ 1 979 . 1 985
.
Bell Labs
++. 1 990
. ++
ANSI ( CllIA).
1 99 1 - ISO ( ).
,
,
. (++98)
1 998 , 20 1 1 (C++ l 1 ) . ISO
++ 1 4, (,
++ 1 ) ++ 1 7 .
++

- STL. .
(Alexander Stepanov).

.

- STL
.
,
( Ada. Scheme ++). 1 979
CllIA,
, GE Labs, & Bell Labs, Hewlett-Packard , Silicon
Graphics Adobe.
91 8 22

I ++ .
1 978-89

Simula 67

" "
Simula.
- ++.

. ++

: .
.
.
.
++ .
: " , .
- ".
. " ,
. ++". ++, , -
( ). .

1 . : www . stepanovpapers . com.


2 . : www . s troustrup . com.
3 . ISO/IEC 1 4882:20 1 1 . Programming Languages - ++. ( ++ .)
4 . Stroustrup. Bjarne. His tory qfC++ : 1 979- 1 99 1 . Proceedings of the
istory of Programming Languages Conference (HOPL-2). SIGPLAN
Notices, Vol. 28 No. 3 , 1 993.
5. Stroustrup. Bjarne. The Design and Evolution qfC++. Addison-Wesley, 1 994.
ISBN 020 1 543303.
6. Stroustrup. Bjame. h ++ Programming Language, Fourth Ediiion. Ad
dison-Wesley. 20 1 3. ISBN 978-032 1 563842.
7. Stroustrup. Bjarne. Tour of C++. Addison-Wesley. 20 1 3.
ISBN 978-032 1 9583 1 .
8. Stroustrup. Bjarne. d ++: Silings; d ++: Casefor Compatiility;
and and ++ : Case Studies in Compatibllity. The /++ Users Journal.
July. Aug .. and Sept. 2002.
9. Stroustrup, Bjarne. "Evolving Language in and for the Real World: ++
1 99 1 - 2006". Proceedings of the Thid SIGPLAN ConJerence the
22.2. 919

Hisiory qf Programming Lang11ages (HOPL-III). San Diego, . 2007. http : //


portal . acm . org/toc . cfm?id=123884 4 .

22.2. 7.

? .
, .
.

,
( ). ,
. -
,
- , ,
, ..
. ,
? ,
? , .
? , ,
? , ,
? . -
-
?
.
, 20 1 4

. IDC (
). ,
920 22

. ,
,
.
? ( ), 90/
Ada, , ++. #, COBOL, Fortran, Java, PERL, . Python
Visual Basic.
.
.
.
, .

.
. ,
, .

22.2.8.
.
.
m -

www . angelfire . com/tx4/cus/people/.



http : / /dmoz . org/Computers/Programming/Languages/ .

l . Scott, Michael L. Programming Language Pragmatics. Morgan Kaufmann,
2000. ISBN 1 55860442 1 .
2 . Sebesta, Robert W. Concepts qf Programming Laguages. Addison-Wesley,
2003. ISBN 032 1 1 93628.

1 . Bergin , . J . , and R. G. Gibson, eds. History of Programming Languages -

Il. Addison-Wesley, 1 996. ISBN 020229502 1 .


2 . Hailpern, Brent, and Barbara G . Ryder, eds. Proceedings of the Third
SIGPLAN Conference the History of Programming Languages (HOPL
III). San Diego, , 2007. http : //portal . acm . org/toc . cfm? id=l238844 .
3 . Lohr. Steve. Go : The Story qf the Math Majors, Bridge Players, Engi
neers, Chess Wizards, Maverick Scientis ts d Iconoclasts - The Prog
rammers Who Created the Sqflware Reoolution. Basic Books, 2002.
ISBN 978-0465042265.
4. Sammet, Jean. Programming Languages: History and Fudamentals.
Prentice Hall, 1 969. ISBN 0 1 37299885.
22.2. 921

5. Wexelat, Richard L . ed. His tory of Programming Languages. Academic


.

Press, 1 98 1 . ISBN 0 1 27450408.


1 . ?
2. ? .
3. -
.
4. ? ?
5. .
6.
.
7.
?
8. ?
.
9. ? .
1 0. - ?
.
1 1 . ?
.
1 2. ?
.
1 3.
?
1 4. ?
1 5.
.
1 6. r ?
1 7.
?
1 8. Algol60?
1 9. ?
20. ?
2 1 . ?
22. J Ada?
922 22

23. Simula?
24. ( ) ?
25. - ?
26. -
?
27. -?
28. .
29. ?
30. ?
3 1 .
STL?
32. ,
22.2.
33. Scheme?
34. ++.
35. "" "++"?
36. "Frtran" ? .
?
37. "COBOL" ? .
?
38. "Lisp" ? .
?
39. "Pascal" ? ,
?
40. "Ada" ? .
?
4 1 . .

"" , -
.

Ada ++ Pascal
Algol COBOL Scheme
BCPL Frtran Simula
Lisp
22.2. 923




Bell Laboratories
- Borland
Cambridge University (-
)
( -
- )

MIT

Norwegian Computer Center

Princeton Universlty

Stanford U niversity

Thchnical University of
Copenhagen

U .S. Department of Defense

u.s. Navy

1 . .
2. .
3. .
?
, .
4. .
?
. , .
5. "Hello, World!" ,
.
6.
.
. ,
. : ,
.
7. , . ,
,
924 22

++. .
,
.
8. ++? 1 0-20- .
9. ? 1 0-20- .
1 0. ( ++)
1 0-20- , .
.
?
1 1 . ?
1 2. ,
, , ?
1 3. ,
, , ? ?
1 4. ,
, ? ?

, .
1 5. , ,
(, ), (Algol, 1 960) (, 1 97 4),
.
1 6. .
(, .()).
(Fortran, 1 956,()), (Algol, 1 960,(Fortran)) (++, 1 985,(C,Simula)),
,
. ,
22.2. 2 22.2.7.

n n

,
.
,
. ,
,

.
, , , - , ..
,
- .


.
, ,
, ,
,
, .
,

: s tring, iostream map.
( regex)
. ,


, ,
.
926 23

2 3. 1 . 23.8. 1 .
23.2.
23.3. -
23.8.2.
23.8.3.
23.4.
23.8.4.
2 3.4.1 .
23.8.5.
2 3.5.
23.8.6.
2 3.6. r
23.6.1 . 23.8.7. r

2 3. 7. r 23.9. r -

2 3.8. 23.10.

23. 1 .
, .
, , . -
, .
( ) . ,
, ,
.
(..
),
.
3 iostream string,
.
( 23.4),

.
(
23.5-23. 1 ).

23.2.
string
, ,
.
s tring .

.
23.2. 927

.
( s tring)
.
<s tring> ( - <s tring . h>).


sl = s2 s2 sl; s2 string
-
s += ; ,
string -
s [i ]
sl+s2 ;
sl, s2
s l==s2 ; sl, s2 ( }
- . ! =
sl<s2 ; sl, s2 (
) -.
<=, > >=
s . size ( ) s
s . length ( ) s
s . c_str ( ) s, -
s . egin ( ) ,
s . end () , , s
s . insert (pos , ) s [pos ] ; string
- . s ,
s . append () s; string
-. s ,

s . erase (pos) , s [pos ] .
s pos
s . erase (pos , n) n s, s [ ] . s
max (pos , size-n)
pos = s . find () s; , string
- ; pos
string : : npos ( s)
in>>s , ,
in s
getline (in , s) in s
out<<s s out

- 1 0 1 1
23.3. ,
928 23

string ,
.
insert ( ) append ( ) ,
. erase ( ) ,
, .

basic_string, ,
Unicode, (,
$ , , , . fl " "). .
, Unicode,
Unicode, :
basic_string<Unicode> a_unicode_string ;
string, ,
basic_string, char.
using string = basic_string<char> ;
Unicode,
,
( .
string, iostream r ).
Unicode,
; ,
, .
,
. ,
12 . 333 .
. ,

.
s tring - .
1 1 .4 ,
ostringstream. ,
<<.
template<typenae > string to_string ( const & t)
{
ostringstrea o s ;
o s << t ;
return os . str ( ) ;

:
string sl to_string ( 1 2 . 333) ;
string s2 = to_string ( l+S* -99/7 ) ;
23.2. 929

sl 11 12 . 33311 , s2 " 1 7 " .


-

to_string ( )
, <<.
, string , ,
.
struct bad_from_string : std : : bad cast
//

const char* what ( ) const // wha t () ba d_ ca s t


{
return " string" ;

1;
template<typename > from_string ( const string& s)
{
istringstream is ( s ) ;
t;
i f ( ! ( i s >> t) ) throw bad_from_string ( ) ;
return t ;

:
doue d = from_string<doue> ( " l2 . 333 " ) ;

void do_soething (const string& s )


try
{
int i = from_string<int> ( s ) ;
11 . . .

catch (bad_from_string ) {
rrr ( " " , s) ;

from_string ( )
to_string () , string
. ,
, string.
, ,
. . .
int d = from_string<int> ( "ary had little lam " ) ; // !

, ,
bad_from_string. 23.9 , from_string ( )
( )
,
. 1 6. 4 . 3 .
930 23

get int ( )
_
.
, to_s tring ( ) from_string ( )
. ;
( , , ..).
" "
s==to_string (from_string<T> ( s ) ) // s

t==from_string<T> ( to_string (t) ) // t

"" ,
, >> <<.
, to s tring ( ) from_string ( )
_
strings tream .

<< >>.
teplate<typename Target, typename Source>
Target to ( Source arg)
{
stringstream interpreter ;
Target result ;
i f ( ! (interpreter << arg) // a rg
1 1 ! ( interpreter >> result) // res ul t
// - ?
1 1 ! ( interpreter >> std : : ws ) . eof ( ) )
throw runtime error { "
_ to<> ( ) " } ;
return result ;

: ! (interpreter>>
std : : ws ) . eof ( ) ,
stringstream .
, , .
,
. ,
int s tring, to<int> ( " l2 3 " ) , to<int> ( " l23 " )
, to<int> ( 11 123 . 5 " ) -

- 5 . .

23.. -
.
-. -
;
23.4. 931

.
- ,
. iostream 1 0 1 1 ,
.

-
in >> in

out << out
in . get () in
getline (in , s) in s

(.
1 4.).

ofstream

-
. ( ,
. , ; . 1 ).
1 0 1 1 , iostream
. (.
1 4 .3), , , stringstream
iostream, istream ostream.
, -
, Unicode, ,
. , ,
- Unicode,
; ,
,
.

23.4.
( -)
. -
, ,
, , , ,
932 23

, ..
,

. .
(. 2 1 . 6).
map, , 2 1 .6.
.

(, Thunderblrd, Seaonkey
Outlook) .
, , , ,
,
. .
.
r (
23.5-23.9)
. ,
, ,
.

.
RFC2822 (.
www . faqs . org/rfcs/rfc2822 . html). .

From: John Doe <jdoe@machine . example>


: .r Smith <mary@example . net>
Suject : Saying Hello
Date : Fri , 21 Nov 1997 0 9 : 55 : 06 -0600
essage-ID : <1234@local . machine . example>
his is messaqe just to say hello .
So , " Hello" .

From : Joe Q . Pulic <john . q . pulic@example . com>


: .r Smith <@machine . tld : mary@example . net> , , jdoe@test . l
Date : Tue , 1 Jul 2003 10 : 52 : 37 +0200
essaqe-ID : <5678 . 21-Nov-1997@example . com>
Hi everyone .

: ".r Smith : Personal Account" <smith@home . example>


From: John Doe <jdoe@machine . example>
Suject : Re : Sayinq Hello
Date : Fri , 21 Nov 1997 11 : 00 : 00 -0600
essaqe-ID : <acd . 1234@local . machine . tld>
In-Reply-To : <345 6@example . net>
2. 933

References : <1234@ 1ocal . m.achine . exaple> <3456@example . net>


This is reply to your reply .

-, ,
, ,
---- ( ). "
", ,
John Doe, . ,
.
-, ,
.
, ,
,
, .
, ,
. , .
- .
ail_file.
( vector<string>)
( vector<essage>).
, begin ( ) end ( ) ,

. .
. " ",
,
, .
:

vector<Message>

vector<string> From: John Doe

: Mary Smith

------. Subject: Saying Hello


934 23

multimap<string,Message*>

"John Doe"

"John . Puic"

vector<Message>

,
John Doe,
.
.
#include<string>
#include<vector>
#include<map>
#include<fstream>
#include<iostream>
usinq namespace std;

Message vector<string>
( ).
usinq Line_iter vector<strinq> : : const_iterator ;

class essage ( 11 Message


11
Line iter first;
Line iter las t ;
puic :
Messaqe (Line_iter pl , Line_iter 2 ) : first(pl } , last (p2 } { }
Line iter eqin ( ) const ( return first ; }
Line iter end ( ) const ( return last ; }
11 . . .
};
23.4. 935

ail_file .
.
typedef vector<essaqe> : : const_iterator ess_iter ;

struct il file // Ma i l_ fi l e
11
strinq name ; 11
vector<strinq> lines ; 11
vector<essaqe> m; //
il_file (const strinq& n) ; //
ess iter eqin ( ) const { return m . eqin ( ) ;
ess iter end ( ) const { return m . end ( ) ;
};

, .
.
,
, .
,
, .
// Mes sa ge ; true,
// ; , s :
bool find_from_addr ( const Messaqe* m , strinq& s ) ;
11 ; , " "

strinq find_suject ( const essaqe* m) ;


,
.
int main ( )

il file mfile { "my-mail-file . txt" } ; // mfi l e


//

11 , )f(
// , mul t imap :
multimap<strinq , const essaqe*> sender ;

for (const auto& m : mfile) {


strinq s ;
if (find_from_addr ( &m, s ) )
sender . insert (make_J>air ( s , &m) ) ;

//
// , John Doe :
auto = sender . equal_ranqe (
" John Doe <jdoe@machine . example>" ) ;
for (auto = pp . first; p ! =pp . second ; ++)
cout << find_suject (p->second) << ' \n ' ;
936 23

.
multimap ( 20. 1 0 .4),
,
. multimap (
).
( ), :
;
.
mul timap
insert ( ) :
for ( const auto& m : mfile) {
string s ;
if (find_from_addr ( &m , s) )
sender . insert (make_yair ( s , &m) ) ;

(, ),
makeyair ( ) . ,
w" find_from-addr ( ) .
Message ,
mul timap? Message
?
.
.
.
.
,
.
ail_file, ,
. ,
( sender) .

: , , . .
( (layers),
) ,
, . ,

. , ,
. ,
- .
,

.
23.4. 937

,
"John Doe" equal_ranqe ( ) ( .4. 1 0).
[first,second),
equal_ranqe ( ) ,
find_suj ect ( ) .
auto = sender . equal_range ( "John Doe <jdoe@achine . exaple>" ) ;

for (auto = pp . firt ; p ! =pp . econd; ++)


cout << find_uject (p->second) << ' \n ' ;

,
(, ), , pair,
( - strinq) first,
( - essaqe) - second (. 2 1 .6) .

23.4. 1 .
, .
, , ,
. ,
. il file
lines m.
ail_file : :&il_file ( const tring& n)
11
// l i nes
11 l ines :
11 ,
11 "--- - "

ifstream in {n } ; 11
if ( ! in) {
cerr << " " << n << ' \n ' ;
exit ( l) ; //

for (string ; getline (in, ) ; ) //


lines . puh_back ( ) ;

auto first = lines . begin ( ) ; 11


for (auto = line . begin ( ) ; p ! =line . end ( ) ; ++) {
if ( * = " ") {
---- //
m . push_back (esage (first , p) ) ;
firt = p+l ; // ----
11
938 23

.
,
.

&
, ,
. " " ?
il_file ,
, "----".

find_from_addr ( ) find_suj ect ( )


- ,

23.6-23. 1 0.
int is_prefix (const s trinq& , const s trinq& )
11 s ?

int n = p . size ( ) ;
if (strinq ( s , 0 , n) ==p) return n ;
return ;

bool find_from_addr (const Messaqe* m , strinq& s )

for ( const auto& : m)


if ( int n = i s_prefix (x, "From: " ) ) (
= strinq ( x , n) ;

return true ;

return false ;

strinq find_suj ect ( const essaqe* m)


{
for ( const auto& : m)
if ( int n = is_prefix ( x , " Suject : " ) ) return s trinq (x , n) ;
return " " ;

, :
string ( s , n) .
s, s [n ] (.. s [n ] . . s [ s . size ( ) -1 ] ),
string ( s , 0 , n) ,
s [ ] . . s [n-1 ] .
,
, .
23.5. 939

find from addr ( ) find suject ( )


? ,
bool, - string. -
.
find_from_addr ( )
( 11 11 ) .
find_from_addr ( ) true (
) s 11 11 (
).
false (
).
find_su j ect ( ) 11 11
, .
,
find_from_addr ( ) ? ? ,
.
: ,
? , find_from_addr ( ) ,
find_suj ect ( ) , find_from_addr ( ) ,
.

, ,
. ,
.
mul timap unordered_
mul timap, ,
.
(map, mul timap, set,
unordered_map unordered_multimap) 2 1 .6.

23.5.
- string
,
.

. .
( ) ,
ClllA ( , ).
for (string s ; cin>>s ; ) {
if ( s . size ( ) ==7
&& isalpha ( s [ O J ) && isalpha ( s [ l ] )
940 23

&& isdiqit ( s [ 2 ] ) && isdiqit ( s [ ] ) && isdiqit ( s [ 4 ] )


&& isdiqit ( s [S] ) && isdiqit ( s [ ] ) )
cout << " "<< s << ' \n ' ;

isalpha () true, .
i sdigi t () true, (. 1 1 .6).
() .
( . ).
(?) ,
(. " 7784 5 " . 77845-1234
77845).
(?)
(. 77845).
(?) ,
(. tx77845).
,
(, 3 OFD).
.
! .
. " "
.
,
if swi tch.
.
( )
if.
- (?) ,
. .
, . .. ,
iostream ( 7 . 8 . 2).
, . .
if,
.
(. -
).
(, .
. 123 ! 123456 ! ) ,
.

. (.
1 1 . 1 1 1 .2).

23.6. r 941

. :
.
2007-06-05
June 5, 2007
jun 5, 2007
5 June 2007
6/5/2007
5/6/07
05 . 0 6 . 2007

. , w
!" ( )
.

(regular expression).

grep Unix ( . . 8),
. (. AWK.
PERL ) .
. ,
++.
PERL. ,
,
++ ( WG2 1 ).
(John Maddock) boost : : regex PERL.
.
.


w"
- .
.

23.6.
,
(pattern). . .
.
77845. :
wwddddd
942 23

w . d .
-

w ( "word"), 1 ( "letter")
1 .
.
. (,
77845-5629). ?
wwddddd-dddd
, , d "
", - "" ?
, w d : -
, ( w "
" .", d " 1 2 " .").
. , ,
, ++ (. \n
).
\w\w\d\d\d\d\d-\d\d\d\d
,
, ,
" ".
. . .
,
, - ? ,
, 5 4,
.
. :
\w2\d5-\d4
- .
, 2, 5 4 ,
2, 5 4.
.
\w{ 2 } \d { 5 } -\d{ 4 }
{ ,
, \ , ,
.
" . :
.
: 77845 77845-562 9.
:
\w{ 2 } \d { 5 } \w{ 2 } \d { 5 } -\d{ 4 }
2.6. r 943

\w{ 2 ) \d { 5 ) -\d { 4 )

, (
), , \w { 2 } \d{ 5 } -\d{ 4 }
\w { 2 } \d { 5 } -\d{ 4 } .
.
(\w{ 2 ) \d{5 ) ) (-\d{ 4 ) )
. . ,
. ,
:
( , \ { .
"" () 1 . ? -
- (). ,
:
(\w{ 2 ) \d{5 ) ) 1 ( \w{ 2 ) \d { 5 ) -\d{ 4 ) )

( \w{ 2 ) \d{5 ) ) (-\d{ 4 ) ) ?

(, \w { 2 } ) .
(?) . , ( -\d{ 4 } ) ?
" -\d { 4 } "; .. ,
, .
, (\w { 2 } \d { 5 } ) ,
:
\w{ 2 ) \d { 5 ) (-\d{ 4 ) ) ?

,
23.5, .
\w{ 2 ) ?\d{ 5 ) (-\d{ 4 ) ) ?
" ?" ,
, .
, ,
:
\w{ 2 ) ( ) ? \d { S ) ( (-\d{ 4 ) ) ?
- ,
, \s (s - "space").
:
\w{ 2 ) \s?\d{ 5 ) (-\d{ 4 ) ) ?
944 23

- ?
,
77845 77845, 77845. .
, " ,
", * .
\w{ 2 } \s *\d{ 5 } ( -\d{ 4 } ) ?

.
. , : -
.
, ,
. .
. (
) ,
.
,
. grep
Unix, .

23.6.1 .

.
++, .
:
\w{ 2 } \s*\d{ 5 } (-\d{ 4 } ) ?
,
"\\w { 2 } \\s*\\d{ 5 } (-\ \d{ 4 } ) ? "
. ,
( " ) .
. .
. "
" ,
++,
(raw string literal).
.

( ),
- .
. .

23.7. 945

R" ( \w { 2 } \s*\d{ 5 } ( -\d( 4 } } ? } "


R" ( , ) " - , 22

\w{ 2 } \s*\d{ 5 } (-\d { 4 } } ?
( ).

23. 7.
.
. ,
, .
- , ,
.
#include <reqex>
#include <iostrea>
#include <strinq>
#include <fstrea>
usinq namespace std ;
int main ( )
{
ifstrea in { "file . txt" } ; //
if ( ! in) cerr << " \n" ;
reqex pat { R" ( \w{ 2 } \s*\d { 5 } (-\d { 4 } } ? } " } ; //
cout << " : " << pat << ' \n ' ;
int lineno = ;
for (strinq line ; qetline ( in , line} ; } { //
++lineno ;
smatch matches ; //
if (reqex_search ( line , matches , pat) )
cout << lineno << " : " << matches [ O] << ' \n ' ;

.
<regex>.
t:
reqex pat {R" (\w { 2 } \s*\d{ 5 } ( -\d{ 4 ) ) ? ) " } ; //

pat ,
.
. regex - , -
,
regex -
946 23

( )
.
regex .
smatch matches ;
if (reqex_search ( line , matches , pat) )
cout << lineno << " : 11 << matches [ O ] << ' \n ' ;
regex_search ( line , matches , pat) line ,
,
pat, - ,
matches. , .
regex_search ( line , matches , pat) false.
matches smatch. s "sub"
( "string" - ). smatch
. . matches [ ] .
.
matches [ i ] . i < matches . size ( ) . ,

N, matches . size ( ) ==N+l .
(sub-pattern)? :
", " .
\w{ 2 } \s *\d{ 5 } (-\d{ 4 } ) ? , -
. ,
, matches . size ( ) =2 . ,
. .
for ( strinq line ; qetline ( in , line) ; ) (
smatch matches ;
if (reqex_search (line , matches , pat) )
11
cout << lineno << " : 11 << matches [ O ] << ' \n ' ;
if ( l<matches . size ( ) && matches [ l ] . matched)
11
cout << " \ t : " << matches [ l ] << 1 \n ' ;

, l<matches . size ( ) ,
,
( ,
pat, ).
, ,
matched, - matches [ 1 ] . matched. ,
, matches [ i ] . matched false,
matches [ i ] , , .
. , ma tches [ 1 7 ]
23.. r 947

, ,
.
, :
address 77845
ffff tx 77843 asasasaa
qqq 3456-23456
howdy
zzz X23456-3456sss qqq 3456-1234
cvzcv 77845-1234 sdsas
77845
12345-123456

:
pattern : " \w{ 2 } \s*\d{ 5 } (-\d{ 4 } ) ? "
1 : 7 7845
2 : tx 77843
5: 23456-3456
: -3456
6: 77845-1234
: -1234
7 : 77845
8 : 12345-1234
: -1234

.

, 999 (, ?).
. z z z ,
( ).
5 6 .
7 , .
( ?) , 12345-123456.

23.8.

.
( , re9ex) .
(regular expressions, regexps regexs).
.
. , -
.

. ( ) -
948 23

, , ,
( PERL).

- , , ,
. ( )
.
,
ECAScript, POSIX awk, grep egrep.
. -
,
, .
, ,
, . ,
" " -
.
,
(, ).
: .
.

23.8. 1 .
,
.
. ,
() " " Is there an here?
"
" , .






)
\
* ,
+
? ( )
()
;
$
23.. 949

,
.

, ,
, , , ,
.
, { . . . } , * , + ?
. , \d+ "
".
,
, ;
, + " ",
\+ - "".

23.8.2.

.


\d [ [ : digi t : ] ]
\1 [ [ : lower : ] ]
\s ( , .. ) [ [ : space : ] ]
\u [ [ : upper : ] ]
\w (a-z A-Z) (0-9), L) [ [ : alnum : ] ]
\D \d [ " [ : digi t : ] ]
\L \ 1 [ " [ : lower : ] ]
\S \s [ " [ : space : ] ]
\U \u [ " [ : upper : ] ]
\W \w [ " [ : alnum : ] ]

"
". , \W "
", " ".
(, [ [ : digi t : ] ] )
,
.
s tring iostream, regex
, Unicode.
s tring ios tream, ,
.
Unicode
.
950 23

23.8..
.


{n}
{n, }
{ n ,m} m
* , , .. { , }
+ , .. { 1 , }
? ( ) , .. { , 1 }

.
*

.
:

Axxvvxxxxxvvvxxvvvvvxv

, .
+ , * . .
+

.
:

(
) . .
\d-?\d

:
1-2
12


1--2
23.8. 951


. ,
\w { 2 } \d { 4 , 5 }
-

, ,
:
-12 34
-54321
22-54321


-123
?-1234

. - \w.

23.8.4.
,
(sub-pattern), .
.
( \d* : )

,
. .
. .
( \d* : ) ? ( \d+)

, ,
.
.
!

23.8.5.
"" ( 1 ) . .
Suject : ( FW : I Re : ) ? ( . * )


FW : Re : .
. .
Suject : FW : Hello , world !
Suject : Re :
Suject : Norweqian Blue


952 2 3

SUJECT : Re : Parrots
Suject FW: No suject !

.
( 1 def) //

.
(bs 1 Bs l bS 1 BS)

23..6.

: (\d); .
(\w) . (. 2 3 . 7 . 2) .
. .
.

[ \w @ ] , @
[a-z ] z
[a-zA-Z ] z
[]
[ \w\- ] ( "-" )
[asdfghjkl ; ' ] QWER1Y
[.]
[ . [ { ( \\ *+? $ ]

(-)
, [ 1- 3 ] (1, 2 3) [w- z ] (w, , z). ,
:
,
. ,
. ,
.
, ,
\w ( " "),
. (\)
? ,
: \ \ .
.
, :

[ aeiouy]
[ \d]
[ aeiouy] ,
23.. r 953

r "
( [ ) . ,
. r .
regex
, . ,
- (.. : a-z
A-Z, 0-9), r
[ [ : alnum : ] ] . alnum
( - ).
- , ,
: " [ [ : al num : ] ] + " . r
.
, :
strinq s = 11 \ " [ [ : alnwn : ] ] +\ 1 1 11 ;

,
regex, ,
:
reqex s { " \ \ \ " [ [ : alnwn : ] ] +\ \ \ 11 11 } ;


:
reqex s2 {R" ( " [ [ : alnwn: ] ] +" } 11 } ;


. .
, ,
.
r
. .


alnum -
alpha
ank ,
cntrl
d
digit
graph
lower
print
punct
954 23



space
upper
w ( -
)
xdigit

regex ,
,
, ,
.

23.8. 7.
,
? .
regex patl ( " ( l qhi ) " ) ; 11
regex pat2 ( " [ c-a] " ) ; //

regex,
.
- , -
, bad_expres
sion.
,
.
#include <reqex>
#include <iostream>
#include <strinq>
#include <fstream>
#include<sstream>
usinq namespace std ;

1 1 ;
// ,

int ma.in ( )
{
regex pattern ;

string pat ;
cout << " : ";
qetline ( cin , pat) ; 11

try {
pattern = pat ; 11
23. 9. r 955

cout << " : " << pat << ' \n ' ;

catch (bad_expression)
cout << 1111 " << pat << " \n" ;
exit ( l ) ;

cout << " 1 : \n 11 ;


int lineno = ;

for ( s tring line ; getline ( cin , line) ; ) {


++lineno ;
smatch matches ;
if ( regex_search ( line , matches , pattern) ) {
cout << 11 1 " << lineno << " : " << line << ' \n ' ;
for ( int i = ; i<matches . size ( ) ; ++i)
cout << 11 \tatches [ " << i << " ] : "
<< matches [ i ] << ' \n ' ;

else
cout << " \n" ;



, , . *, ( * ) , \ ( [ " ) ] * \ )
.

\w+ \w+ ( Jr\ . ) ? .

23. 9.
.
. (
) , - regex_search ( )
.
(
) - regex_match ( )
.
23.6.
.v .

KLASSE ANTAL DRENGE ANTAL PIGER ELEVER IALT


12 11 23
1 7 8 15
18 4 11 15
956 23

KLASSE ANTAL DRENGE ANTAL PIGER ELEVER IALT


2 10 13 23
10 12 22
4 7 7 14
48 10 5 15
5 19 8 27
6 10 9 19
68 9 10 19
7 7 19 26
7G 3 5 8
71 7 3 10
8 10 16 26
9 12 15 27
3 2 5
1 1 2
2 5 5
1 08 4 4 8
1 0
1 8 5 13
2 8 5 13
D 3 3 6
4 4 5
6 3 4 7
8 4 4 8
9 4 9 13
REST 5 6 11
Alle kl asser 2 34 202 386

( 2007 ,
) ( -)

.
.
, ,
, . (
, .)
.
"" ,
.
23.9. 957

,
, :
-
: .

.
, (..
).
, ( -
).
, !
, ,
, ( -
), ,
(. . 1 0 1 1 ) .
r ,
: - - .
reqex header { R" ( " [ \ w ] + ( [\w ] +) * $ ) " } ;
reqex row {R" ( " [\w ] + ( \d+) ( \d+) ( \d+) $ ) " } ;

,
, ?
-
. r (write-only language). .
,
, - -
.
(- ", ).
,
\w, .
[ \w ] , .. (,
) . ( )
[ \w ] +. ,
, [ \w ] +. "" ()
" ".
, : ( [ \w ] +) .
: ( [ \w ] +) * $ . ($)
" ".
,
,
.
958 23

: ,
.
, : " [ \w ] +.
, -
: ( \d+ ) , :
" [ \w ] + ( \d+) ( \d+) ( \d+) $


R" ( " [ \w ] + ( \d+) ( \d+) ( \d+) $ ) "

, . ,
.
int ain ( )
{
ifstream in { " t . txt" ) ; 11
if ( ! in ) error ( "He \n " ) ;
string line ; //
int lineno = ;

regex header { R" ( " [\w ] + ( [\w ] +) * $ ) " } ;


regex row { R" ( " [\w ] + ( \d+} ( \d+} ( \d+) $ ) " } ;

if (getline ( in , line) ) { 11
satch matches ;
if ( ! regex_match ( line , atches , header) )
error ( " " ) ;

while (getline ( i n , line) ) 11


++lineno ;
smatch matches ;
if ( ! regex_atch ( line , atches , row) )
rrr ( " cpoa " , to_strinq ( lineno ) ) ;

ltinclude.
, regex match ( ) ,
regex search ( ) . ,
_

regex_ma tch ( )
, regex search ( ) ,
_

.
regex_ma tch ( ) , regex_
search ( ) ( ),
.
.
23.9. 959

.
("drenge") ("piger"),
. ,
("ELEVER IAL") . ('le
klasser") .
row ,
"Alle klasser".
int main ( )

ifstrea.m i n { " tae . txt" } ; //


if ( ! in) error ( "He r ." ) ;

string line ; 11
int lineno = ;

regex header (R" ( " [ \w ] + ( [ \w ] + ) * $ ) " } ;


regex row {R" ( " [ \w ] + ( \d+) ( \d+) ( \d+) $ ) " } ;

if (getline ( in , line) ) { //
smatch matches ;
if (regex_match (line , matches , header) ) {
error ( " r" ) ;

// :
int boys = ;
int girls = ;

while (getline (in, line) )


++lineno ;
smatch matches ;
if ( ! regex_match ( line , tches , row) )
cerr << " : " << lineno << ' \n ' ;

if (in . eof ( ) ) cout << ": .n\n" ;

11 :
int curr_boy = from_string<int> (matches [ 2 ] ) ;
int curr_girl = from_string<int> (matches [ ] ) ;
int curr total = from_string<int> (matches [4 ] ) ;
if (curr_boy+curr_girl ! = curr_total)
rrr ( " \n") ;

if (matches [ l ] ="Alle klasser" ) { //


if (curr_boy ! = s)
rrr ( ": \n") ;
if (curr_girl ! = girls)
rrr ( ": \n" ) ;
960 23

if ( ! (in>>ws ) . eof ( ) )
rrr ( " " ) ;
return ;
}
11 :
boys += curr_oy ;
qirls += curr_girl ;

error ( " 'l'r " ) ;

:
. ("A\le klasser"). .
,
(
to<> ( ) ; . 23.2).
.
.
from_s tring ( ) 23.2. .
,
string int .

23. 1 .
- .
.
, ,
,
. . .

. , ,
,
.
.
1 . Aho, Alfred V. , Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman . Compilers:
Principles, Techniques, and Tools, Second Edition ( w
") . Addison-Wesley, 2007. ISBN 032 1 547985. '
2 . , Russ. "Regular Expression Matching Simple and Fast (but
Is Slow in Java, Perl. . Python, Ruby, .) . http : //swtch . com/ ... rsc/
" "

regexp/regexpl . html .

1 : . . . . .

. . .: " . : -

"", 2008. ISBN 978-5-8459- 1 349-4.


23.1 . 961

3. Maddoc, J. boost: :regex documentation. www . oost . org/libs/regex/doc/


index . html .
4. Schwartz, Randal L. . Phoenix, and Brian . Foy. Leaming PERL, Fourth
Edition. O 'Reilly. 2005. ISBN 0596 1 1 058.

1 . , regex
. 11: std : : regex trl : : regex.
2. 23. 7;
/
regex
.
3. 2
23. 7.


1 . ""?
2.
?
3. insert ( ) - -
( )?
4. Unicode?
5. s tring ?
6. cin>>s
getline (cin , s ) , s - s tring?
7. .
8. map?
.
9. map?
l . map mul timap?
, map, mul timap
?
1 1 . ?
1 2. ?
.
1 3.
?
962 23

1 4. r regex?
1 5. \w+\s\d{ 4 } ?
.
regex ?
1 6. ( ) , r-
?
1 7. regex_search ( ) ?
1 8 . regex_ma tch ( ) ?
1 9 . ( . ) r ?
20. " " r ?
2 1 . 7 \w? _ ()?
22. ?
23. ?
24. ?
25.
?
26. ,
?
27. ? ?

multimap smatch
regex match ( )
_

regex_search ( )

1 . ,
; , ,
. ,
, .
/
. , ,

, , ----.
2. multimap
.
, .
23.1 . .n 963

3. 23.4

.
4. (. . ,
) ,
,
.
5 . (
)
mul timap, - unordered_mul timap.
,
mul timap.
6. , .
, ,
: .
, 20.02.2004,
. .
7. ( ),
. ,
,
.
8. 23.8. 7 ,
.
( : ),
. ,
.
9. eof ( ) ( . 7 . 2), ,
.
, ( . 2 3 . 9) .
,
, ,
.
1 0 . 23. 9 ,
,
, .
1 1 . 23. 9
. ,
.
1 2 . ,
(. 6) , ISO
964 23

yyyy-mm-dd.
. ,
: .
1 3 . ( . ) ' \n ' ? ,
.
1 4. , . 23.8.7,

.
(
' \n ' ).
. .
.
1 5. ,
.
1 6. : ,
.

, .
,
. , . -
: . ,
.
.
++ .

. ,
.

,-., ;..

, .. .
-

- . . (./J..L. .. )


,
.
,
. -
,
N - .
,
, .



.
966 24

2 4. 1 . 24.5.5.
24.2. , 24.6. :
24.2. 1 .
24.6. 1 . t\
24.3.
24.4. r 24.6.2. r
24.6..
24.5 . atrix 24.7.
24.5 . 1 . 24.8.
24.5.2.
24.5.. 24.9.
24.5.4. - 24.10.

24. 1 .
(, .
) .
.
, , ,
.
,

, . -
++,
.
;

. .
,
(, ).
, , atrix, -
.
.
.
.

atrix, (
).
24.2. . 967

24.2. ,

,
: , (int, long .) -
,
(float, doule .)
. ,

. .
float = 1 . 0/333 ;
float sum = ;
for (int i=O ; i<333 ; ++i ) sum+=x ;
cout << setprecision ( 15) << sum << 11\n" ;
, ,
0 . 999999463558197
- .
, "
" , ,
, . ,
1 /3
(
). l /333,
, 333 (
l /333 float), ,
.
: ,
.
, .
,
,
- . ,
,
.


333 1
. ?
? !


. ,
968 24

,
(.. ).
(.. ).
. ,
( ). .
, ( ).
, ,
.
.
short int = 40000 ;
int i = 100000 0 ;
cout << << " " << i * i << 11\n" ;

. :
-25536 -727379968

. .

. ,
,
. short
40 , int
1 .
++ ( .8)
;
sizeof () . sizeof (char) =1.
.

char
D
short

int, long, float

doue

Windows
Microsoft. ++
, -
. ,
char, int doue. (,
, )
, .
,
. ,
24.2. , 969

, .
.
cout < < " w : " < < sizeof ( int)
<< 1 ' << sizeof (float) << ' \n ' ;
int = 210000000 9 ; 1 1
float f = ;
cout << << ' ' << f << endl ;
cout << setprecision (lS) << << ' ' << f << ' \n ' ;

:
w :4 4
2100000009 2 . le+009
2100000009 2100000000
float int (4 ).
float ( ,
) (.. m 1 ),
int. (
.
?)
, f 2100000009
, . 9
-
.
,
, :
, - - -
. .
float f = 2 . 8 ;
int = f ;
cout << << ' ' << f << 1 \n ' ;
2 . 3,
, " 4/5".
++ float int
, .

. ++ .
.
void f ( int i, doue fpd)

char = i; 11 cha r
11
short s = i ; 11 : i n t
11 short
i = i+l ; 11 , i
970 24

11 ?
lonq lq = i* i ; 11 : l ong i n t
float fps = fpd; 11 : doue
11 floa t
i = fpd; 11 ; , 5 . 7 -> 5
fps = i ; 11 (
11 )

void q ( )
{
char ch = ;
for (int i = ; i<SOO ; ++i)
cout << int ( ch++) << \t ' ;

, !
.
.
.


g ( ) . f ( ) ,
, s, i ..
.


25.5.3. I ,
.
. , doue
float, -
, doue float.
int, doue complex
(. 24. 9), - char -
bool.
.

24.2. 1 .
++
<limi ts>. <climi ts>. <limi ts . h>
<float . h>, -
, ..
.9. 1 .
. , , -
24.3. 971

.
. ,
, "
int?" " char?"
.
.
. .
cout " 11 'l' int : "
sizeof (int) ' \n ' ;
cout " 'l' int : 11
IN_ << endl ;
cout " int : 11
numeric limi ts<int> : : min ( ) << ' \n ' ;

if (numeric_limits<char> : : is_signed)
cout << "n cha.r .\n" ;
else
cout << " char \n " ;

11 :
char ch = numeric_limits<char> : : min ( ) ;
cout << " char : "
<< ch << ' \n ' ;
cout << " int char "
<< "w : " << int ( ch) << ' \n ' ;

.
,
. ""
, .
.

24..
(array) - ,
().
(vector) .
,
. (matrix).

. vector ( .4), array (
20.9), ( .8.2)
. (, )?
?
- .
972 24

(, Matrix<int> v(4));

1 xN

1 1 1 1 1 ''""' 3'4 ("""''"' M.i<i<'iot,2> m(.411:



. , ,
.
, ,
.

/
\'
, - - - - - - - - - - ,

'
'

'

__ - - - - -
'
- - - - - - - - .J
4
( , Matrix<int,2> m(,4));
- - - - - - - - - ;-
.
- - - - - - -- -- - -- : -- - - - - - -
3 , 4
'

-
'
'
- - - - - - - - -

- ,
(-). - ,
(-).

24.4.
++ .
,
.. , . .
int ai [ 4 ] ; //
doue d [ ] [ 4 ] ; //
char [ ] [ 4 ] [5] ; //
ai [ l ] = 7 ;
ad [2] [ ] = 7 . 2 ;
[2] [ 3] [ 4 ] = ' ' ;

.

.
24.4. 973

.
.

- (. ).
.
,
.
.

.
. ,
.
, ().
.
.
.
,
(, h ++ Programming Language). , ++
,
"" , .

,
,
. .
void fl (int [ ] [5 ] ) ; // [ 3 } [ 5 }

void f2 (int [ ] [5] , int diml ) ; 11

void f (int [ 5] [ ] , int dim2 ) ; // :


//

void f4 (int [ ] [ ] , int diml , int dim2 ) ; // (


11 )
void f5 (int* m, int diml , int dim2) // ,
//
for (int i=O ; i<diml ; ++i )
for (int j = ; j<dim2 ; ++j ) m [ i *dim2+j ] = ;

m int*,
.
(),
, m
974 24

(diml ,dim2), ,
. m [ i *dim2+j ]
m [ i , j ] , , , m
, m [ i , j ] .
, .
,
. ,
, ++,
, .

24.5. trix
/ ?
"
, ".
, .
.
.

.
/ .
.
.
: ( ) .
: [ ] .
: =.
(+=, -=, *=. %= ..).
(, res [ i ] = a [ i ] *+ [ 2 ] ).
(res = [ i ] * [ i ] :
inner_product).

, /, ,

( ) .
(
"" ).
atrix . ,
.
24.5. Matrix 975

,
,
.
,
atrix. atrix ISO ++.
atrix . h.
Numeric_lib.
atrix, "" ""
++.
atrix . .

24.5. 1 .
.
#include 11atrix . h "
using namespace Nwneric_lib ;

void f ( int nl , int n2 , int n)


{
atrix<doue , l> adl (nl) ; // dou e
atrix<int , l> ail (nl) ; // i n t
adl ( 7 ) ; // For t ran - ()
adl [ 7 ] = 8 ; // - [ ]

atrix<doue , 2> ad2 (nl , n2 ) ; !/


atrix<doue , > ad3 (nl , n2 , n3) ; !!
ad2 ( , 4) = 7 . 5 ; //
d ( , 4 , 5 ) = 9 . 2 ; //

, ( atrix) ,
.
, atrix ,
.
atrix (
, atrix<doue , 2>) (),
, (, trix<doue , 2>
ad2 (nl , n2 ) ) . , ad2
,

nl n2,
nl x n2 .
atrix, .

trix, , ..
vector,
atrix ( , Fortran):
976 24

, atrix [O,max). max -


.
" ".
,
, . -
,
atrix: .
,
[ ] ( ++).
( ) ( Fortra).

. [] .
atrix; -
atrix, [] -1 - .
-

( , , z )
, atrix;
.
, , .
void f ( int nl , int n2 , int n3)
{
atrix<int , O> aiO ; // : -
atrix<doule , 1> adl ( S ) ;
atrix<int , 1> ai (S) ;

atrix<doule , 1> adl1 ( 7 ) ;


ad1 ( 7 ) = ; // Ma t rix error
// (7 )
-

adl ai ; // :
adl adll ; // Ma t rix_error
// ( )

atrix<doule , 2> ad2 (nl ) ; // :


ad2 (3) = 7 . 5 ; // :
ad2 ( 1 , 2 , 3) = 7 . 5 ; / / :

atrix<doule , 3> ad3 (nl , n2 , n3 ) ;


atrix<doule , 3> ad33 (nl , n2 , n3 ) ;
ad3 = ad33 ; // : ,
//

.v
. .
. ;
. atrix_error.
24.5. Matrix 977

- , - ,
( )
( , ) .
[ ] [ ] ,
- .
.
[1 ][2]

[] : 00 01 02
[1 ] : 10 11 12
[2] : 20 21 22 23

atrix ,
:
void init (atrix<int , 2>& ) // )f(
{ // }'II1
for ( int i=O ; i<a . dillll ( ) ; ++i)
for ( int j ; j<a . dim2 ( ) ; ++j )
a ( i , j ) = lO*i+j ;

void print ( const atrix<int , 2>& ) //


{
for ( int i=O ; i<a . dillll ( ) ; ++i)
for ( int j = ; j<a . dim2 ( ) ; ++j )
cout << a ( i , j ) << ' \t ' ;
cout << ' \n ' ;

diml ( )
, dim2 ( ) -
..
atrix, ,
atrix (
).
void init (atrix& ) ; // u :
//

, atrix
.
.
978 24

.

atrix ( . . 1 2).

24.5.2.
- ?

, .
atrix<int , 1> al ( S ) ; 11 a l -
atrix<int> a ( S ) ; // Ma t ri x<in t , 1 > (8 ) ;

, al (atrix<int , 1>).
atrix
.
.
a . size ( ) ; 11 Ma trix
a . diml ( ) ; //

.
, .. .
int* = a . data ( ) ; 11

atrix
, .
.
a (i ) ; // i - ( Fortran)
a [i ] ; // i - ( )
a ( l , 2) ; 11 : - Ma trix

a trix.
"" (slice),
, , slice ( ) . -
atrix .
a . slice ( i) ; // a [i ]
a . slice ( i , n) ; // a [i ] a [i +n -1 ]


, .
atrix, . .
a . slice ( 4 , 4) = a . slice ( 0 , 4 ) ; //

.
( 1 2 4 5 7 8 }
24.5. Matrix 979


1 2 4 1 2 4 1

,
atrix; .. a . slice ( O , j ) - [O.j).
a . slice ( j ) - [j ,a . size ( ) ). ,
:
a . slice ( 4 ) = a . slice ( 0 , 4 ) ; //
//

. - .
i n, a . slice ( i , n )
.
, .
, . slice ( i , . size ( ) ) [i ,a . size ( ) ) ,
. slice (a . size ( ) ) a . slice (a . size ( ) , 2) - atrix.
.
. , a . slice ( i , O)
atrix.
, . ,
a . slice ( i , n ) n, , (
, ).
( ++)
.
atrix<int> 2 = ; //
= 2 ; //


atrix:
*= 7 ; // a [i } * = 7 i (
11 +=, -=, /= . . )
= 7; // a [ i } = 7 i


(=. +=, -=. /=, *=. %=, "=. &=, 1 =. >>=. <<=)
. .
, atrix:
a . apply (f) ; // a [i } =f (a [i } ) a [ i }
a . apply (f , 7 ) ; // a [ i } =f (a [i } , 7) a [i }

apply ( )
atrix.
. :
= apply (as , a) ; // , , b (i ) ==abs (a ( i ) )
980 24

s -
( 24.8). apply ( f , )
x . apply ( f ) . + +=.
.
= *7 ; // b [ i } a [ i } * ? i
*= 7 ; 11 [ i } a [ i } * 7 i
= apply (f , x) ; 11 y [ i } f (x [i } ) i
x . apply (f) ; 11 x [i } f (x [i } ) i

== =.
Fortran apply
(broadcast function).
f (x) , apply (f , ) .
f (
, Fortran) .
. () apply.
,
- apply ( . apply (f , ) ),
= apply (f , a , x) ; // b [ i } =f (a [i } , x) i

:
doue scale (doue d , doue s ) { return d*s ; }
= apply (scale , a , 7 ) ; 11 b [ i } a [ i } * 7 i
=

, "" apply ( )
, ,
,
atrix. ,
. .
apply ( ) ,
, ; ..
. . .
void scale_in_ylace (dou.le& d, dou.le s) { d *= s ; }
b . apply ( scale_in_ylace , 7) ; // b [ i } *= 7 i
atrix
:
atrix<int> =
scale_and_add (a , B , a2) ; //
int r = dot_yroduct (a3 , a) ; //

scale and add ( )


_ _

(fused multiply-add), .f
: result (i ) =argl ( i ) *arg2+arg3 ( i ) i -
. . inner_pro
24.5. Matrix 981

duct, 2 1 . 5.3; : resul t+=argl


( i ) *arg2 ( i ) i , result
.
;
, vector
atrix. atrix ,
, *=,

.
, "
", ,

.
.
atrix, ,
, ( ,
).
atrix
. .
void some_function (doue* , int n)

doue val [ ] = { 1 . 2 , 2 . 3 , 3 . 4 , 4 . 5 } ;
atrix<doule> data { p , n } ;
atrix<doule> constants {val } ;
11 . . .

,
, ,
atrix.
,
,
constants -
4. , ,
,
data , n.

2&.5..
atrix ,
,
, .
, ,
atrix, .
982 24

atrix<int , 2> ( , 4 ) ;
int s a . size ( ) ; 11
int dl a . diml ( ) ; 11
int d2 = a . dim2 ( ) ; 11
int* = a . data ( ) ; 11


. ,
, .
.
a (i , j ) ; // (i , j ) ( For tran )
a [i ] ; // i - ( )
a [i ] [ j ] ; 11 (i , j ) ( )


[ i ] , i-
. ,
,
(
a [ i ] . data ( ) ). , a ( i , j )
, [ i ] [ j ] ,
.

r 1:::::::J::::: : 1:: : ::J: : ::::J ;__::'::>


.
013 41

a . slice ( i ) ; // a { i }
a . slice ( i , n) ; 11 a {i } a { i +n - 1 }
a.slice(0,2) ""'

r :[ : :: l:: :: :; ::; -:
-
_ _ _
- _
_ :
_
_
Mt><;m,2(3 ,4)
_

' - - - - - - - - - - - - - - - - - - - - _ _ ,

a[2].slice(2)
(
, ).
,
. ,
;
.
24.5. Matrix 983

atrix<int , 2> 2 = ; 11
= 2 ; 11
*= 7 ; 11 ilJ' ( + = , -= . . )
a . apply (f) ; 11 a (i , j ) =f (a (i , j ) ) a (i , j )
a . apply ( f , 7 ) ; 11 (i , j ) =f (a (i , j ) , 7) (i , j )
=apply ( f , a) ; 11 b (i , j ) ==f (a (i , j ) )
b=apply ( f , a , 7 ) ; 11 b (i , j ) ==f (a (i , j ) , 7)

, ,
.
a . swap_rows ( l , 2 ) ; 11 a [ l } <-> [2 }

swap_columns ( ) .
, (. . 1 1 ). -

. ,
[ i ] (
). ( i , j ) i .
.
,
, .
enum Piece { none , pawn , knight , queen , king , bi shop , rook } ;
.atrix<Piece , 2> board ( 8 , 8 ) ; 11

const int white start row ;


const int k start row 7;

atrix<Piece> start row


= { rook , knight , bishop , queen , king , bishop , knight , rook} ;

atrix<Piece> clear_row ( 8 ) ; 11 8

clear_row , none , , ==

.
s tart_row clear_row :
board [white_start_row] = start_row; 11
for (int i = 1 ; i<7 ; ++i)
board [i] = clear_row; 11
board [lack_start_row] = start_row; 11

, , ,
[ i ] , lvalue (. 4.3); ,
board [ i] .
984 24

24.5.4. -
atrix .
- :
atrix<doue> ( 4 ) ;
cin >> ;
cout << ;

doue, ; :
{ 1.2 3.4 5.6 7.8 }
, , .
-
.
. .
atrix<int , 2> m ( 2 , 2 ) ;
cin >> m;
cout << m;

1 2
3 4

.
<< >> atrix , ,
.
.
<< >> atrix
atrixIO . h ( atrix . h). , .
, trixIO . h.

24.5.5.
, ,
, ;
. .
atrix<int , 3> a ( l0 , 20 , 30 ) ;
. size ( ) ; 11
a . diml ( ) ; 11 1
a . dim2 ( ) ; 11 2
a . dim ( ) ; 11 3
int* = a . data ( ) ; 11
a ( i , j , k) ; 11 (i , j , k ) - ( For t ra n )
24.6. : 985

//
a [i ] ; 11 i - ( )
11
a [ i ] [ j ] [k] ; // (i , j , k ) - ( )
. slice (i) ; 11 i -
a . slice ( i , j ) ; 11 i - j -
atrix<int , > 2 ; //
= 2 ; //
*= 7 ; // ( + = , . . )
a . apply ( f) ; 11 (i , j , k) =f (a ( i , j , k) )
11 a (i , j , k )
a . apply ( f , 7 ) ; 11 (i , j , k ) =f (a (i , j , k) , 7)
11 a (i , j , k)
=apply ( f , a) ; // , ,
11 b (i , j , k ) ==f (a (i , j , k) )
b=apply ( f , a , 7 ) ; // , ,
11 b (i , j , k ) ==f (a (i , j , k) , 7)
a . swap_rows (7 , 9 ) ; // [ ?] <-> [ 9}

, ,
:. , ,
-

[ i ] ( , i
-

): a [ i ] [ j ] (
-

, j );
[ i ] [ j ] [k] int ( , k
-

).
,
(,
).
int qrid_nx ; // ;
int qrid_ny ;
int qrid_nz ;
atrix<doue , > cue (qrid_nx , qrid_ny , qrid_nz) ;

,
,
. .
atrix.
N- , 29 h
++ Programming Language.

24. 6. :
,
. ; ,
. ,
: ,
986 24

,
.
,
atrix.
:
a l . \ \ + " . + \ . 11 = \

. 1 1 + + r1.nr1 = r1
. - .
.
r .
- ,
.
:
=
- :
1 . 1 1 ",
=
. 1 11.11

- :
\
\
= =
"

, .
.
. ( . .
, , . . : , 3- . : -

: " .. ", 20 1 3) .
. , ..
. .
:

=
. r1
11

. (
, i .
24.6. : 987

( i. ), , a(k. j).
(i. ==.
i .
.
,
. ( ):

, [\ [\ /(rt. ).
. [- 1 \ ,
x[ I J .
a(ri. ), .
,
. . ,
.

24.6.1 .
.
++. -. ,
. .
typedef NWDeric_lib : : atrix<doule , 2> atrix ;
typedef NWDeric_lib : : atrix<doule , l> Vector ;

.
Vector classical_gaussian_eliination (atrix , Vector )
{
classical_eliination (A , b) ;
return back_sustitution (A , b) ;

. (
),
.
.
. .
classical_elimination ( ) back_susti tution ( ) .
.
void classical_eliination (atrix& , Vector& )
{
const Index n = A . diml ( ) ;

11 ,
11 , :
for (Index j = ; j < n-1 ; ++j ) {
988 24

const doule pivot = A ( j , j ) ;


if (pivot 0) throw Elim_failure ( j ) ;
==

11 , i
for ( Index i = j+l ; i < n ; ++i) {
const doule mult = A ( i , j ) / pivot;
A [ i] . slice ( j ) = scale_and_add (A [ j ] . slice ( j ) ,
-mult , A [ i ] . slice ( j ) ) ;
b (i ) -= mul t * ( j ) ; 11

(pivot) , .
. ,
; ,
.
Vector back_sustitution ( const atrix& , const Vector&
{
const Index n = A . diml ( ) ;
Vector (n) ;

for ( Index i = n-1 ; i >= ; --i) {


doule s = (i) -dot_.Product (A[i] . slice ( i+l) ,
x . slice ( i+l) ) ;
if (doule m = A (i , i) )
x ( i) = s/m;
else
throw Back sust_failure ( i ) ;

return ;

24.6.2.

,
, .
" ",
.
,
( ,
, ).
void elim_with_.Partial_.Pivot (atrix& , Vector& )
{
const Index n = A . diml ( ) ;
24.6. : 989

for ( Index j = ; j<n ; ++j )


Index pivot_row = j ;

11 :
for (Index k = j+l ; k<n ; ++k)
if (as (A (k, j ) ) > as (A (pivot_row , j ) ) ) pivot_row k;

11 :
if (pivot_row ! =j ) {
A . swap_rows ( j , pivot_row) ;
std : : swap ( ( j ) , (pivot_row) ) ;

11 :
for ( Index i = j+l ; i<n ; ++i) {
const doue pivot A ( j , j ) ;
if (pivot=O )
rrr ( " , " ) ;
const doue mult = A ( i , j ) /pivot ;
A [i] . slice ( j ) = scale_and_add (A [ j ] . slice ( j ) ,
-mul t , A [ i ] . slice ( j ) ) ;
(i) -= mul t* ( j ) ;


, swap_rows ( ) scale_and_add ( ) .

24.6..
, . ,
.
void solve_random_system ( Index n)
{
atrix random_matrix (n) ; 11 . 24 . 7
Vector random_vector (n) ;

cout << " = " << << endl ;


cout << " = " << << endl ;
try {
Vector = classical qaussian_elimination (A, ) ;
cout << " : = " << << endl ;
Vector v = * ;
cout << " * = " << v << endl ;

catch ( const exception& ) {


cerr << e . what ( ) << std: : endl ;
990 24

catch .
- (, ,
, ).
- ,
classical_elimination (
elim_withyartialyivot ).
- .
, , ,
classical_elimi
nation .
.. ,
*, (
) . -

if (* ! =) rrr ( " 1D1" ) ;

, .
!
== =

:
.
atrix ,
.
Vector operator* ( const atrix& m, const Vector& u)
{
const Index n = m . diml ( ) ;
Vector v (n) ;
for ( Index i = ; i < n ; ++i)
v ( i ) = dot_product (m [ i ] , u) ;
return v ;

atrix
. 24.5.3,
atrix atrixIO . h.
random_matrix ( ) random_vector ( )
( 24. 7) . Index
( . 1 6) .
atrix.
using:
usinq Numeric_l i : : Index ;
24.7. 991

24. 7.
,
7 1 7,
" ".
, ,
, " "
. : -
. ,
, -
, -
,
.
( ), (
,
) ( ,
).


,
. ,
.
<random>
,
.
.
. .

.
.
.

, .
, random_vector ( ) ,
24.6.. random_vector ( n ) atrix
<doue , 1>. n doue
[O ,n) :
Vector random_vector ( Index n)
{
Vector v ( n) ;

default_random_engine ran{ } ; //
uniform real ditribution<>
992 24

ureal { O , } ; 1 1 i n t do ue
11 [ 0 , )

for (Index i = ; i < n ; ++i )


v ( i ) = ureal (ran) ;
return v ;

(defaul t_random_engine) ,
.


. linear_congurential_
engine, mersenne_twister_engine random_device.
-
defaul t_random_engine, .

, . 1 .
s td_lib_facilities . h
.v :
int randint ( int min , int )

static default_random_enqine ran ;


return uniform_int_distriution<> { min , max ) (ran) ;

int randint (int )

return randint ( O ,max ) ;

, -
, ,
:
auto qen = bind (normal_distriution<doue> { 15 , 4 . 0 ) ,
default_random_enqine { } ) ;

ind ( ) <functi
onal> .

. gen ( )
, 15 4 .
defaul t_random_engine.
.v .
vector<int> hist (2 * 15 ) ;
for (int i = ; i < 500 ; ++i ) 11
24.7. 993

++hist [int (round (qen ( ) ) ) ] ; 11 5 0 0


for (int i = ; i ! = hist . size ( ) ; ++i) { //
cout << i << 1 \t ' ;
for (int j = ; j ! = hist [i] ; ++j )
cout << ' * ' ;
cout << 1 \n ' ;

:

1
2
3 **

4 *

5 *****

6 ****

7 ****

8 ******

9 ************

10 ***************************

11 ***************************

12 **************************************

13 **********************************************************

14 ***************************************************

15 *******************************************************

16 ********************************

17 **************************************************

18 *************************************

19 **********************************

20 **************

21 ************

22 ************

23 *******

24 ******

25 *

26 *

27
28
29

( )
". bernoul l i_dis tribution, exponen
tial_distribution chi_squared_distribution.
h ++ Progrwnming Language.
[.J .
( )
[,).
( , , random_device)
-
994 24

. . ,
,
, ""
(seed), :
auto genl = bind (uniform int_distriution<> { 0 , 9 } , default_random_engine { } } ;
auto gen2 = bind (uniform_int_distribution<> { 0 , 9 } , default_rando111:_engine { lO } ) ;
auto gen = bind (uniform_int_distribution<>{ 0 , 9 } , default_random_engine { S } } ;

( ; . 26.6. 1 ) - .

24.8.

(cos, sin, log ..).
<cmath>.


s ()
ceil () , >=
floor () , <=
sqrt (x) ;
cos (x)
sin (x)
tan (x)
acos () ;
asin () ; ,
atan ()
sinh ()
cosh ()
tanh ()
() ( )
log (x) ( );

loglO ()


float, doue, long doue complex ( 24.9).
.
,
.
2. 9. 995


,
errno. .
errno = ;
doule s2 = sqrt (-1) ;
if (errno) cerr << "- - , " ;
if (errno EDOM) // -
==

/!
cerr << "+ sqrt ( ) "
<< " " ;
pow (very_large , 2 ) ; //
if (errno==ERANGE) // -
cerr << "pow ( " << very_large
<< " , 2) doule" ;

.
errno, ,
- . - ,
. ,
errno ,
.
. errno
. - . ,
. -
errno , .
errno, , .
errno
,
,
D ERANGE. EDOM
, -
(domain error). .. .
ERANGE
(range error). . . .
errno
.
( 1 975).

24. 9.

. , , ,
, ,
ISO ++.
996 24


<complex>.
template<class Scalar> class complex {
//
11 , -
Scalar re , im;
pulic :
constexpr complex ( const Scalar & r , const Scalar & i)
: re ( r) , im ( i) { }
constexpr complex ( const Scalar & r) : re ( r) , im ( Scalar ( ) ) { }
complex ( ) : re ( Scalar ( ) ) , im ( Scalar ( ) ) { }
constexpr Scalar real ( ) { return re ; } //
constexpr Scalar imaq ( ) { return im; } //
11 : = += -= *= /=
};
complex .
float , doue long doue . complex
( 24.8), <complex>
.


zl+z2
zl-z2
zl*z2
zl/z2
zl==z2
zl ! =z2
norm ( z ) s (z)
conj ( z ) : z { re , im } , conj (z) (re , -im)
polar ( , ) (rho , theta)
real ( z )
imag ( z )
s (z) , rho
arg (z) , theta
out << z
in >> z

: complex < % .
complex<T> ,
, doue:
usinq cmplx = complex<doule> ; // complex<do u e> 111

void f ( cmplx z , vector<cmplx>& vc)


24.1 . 997

l z2 = pow ( z , 2 ) ;
l z = z2* 9 . +v [ ] ;
l n = accnulate (vc . begin ( ) , vc . end ( ) , l { } ) ;
11 . . .

, int doue
complexl
if ( z2<z 3) // : <

, ( )
++
Fortran.

24. 1 .
, , , ,
,

.
, ++, ,

.
,
.
MacThtor History of Mathematics, http : / /
www-gap . dcs . s t-and . ac . uk / his tory.
- ,
.
- ,
; .
?
: , .
.
.
.
.
.
.
.
998 24

.
.
.
.

1 . Freeman, . L . . and Chris Phillips. Parallel Nnerical Algorithms. Prentice
Hall, 1 992.
2 . Gullberg, Jan. Mathematics - From the Birth qf Numbers. W. W. Norton,
1 996. ISBN 039304002.
.
(. ). .
3. nuth. Donald . The Art of Computer Programming, Volume 2: Seminume
rical Algorithms. Third d. Addison-Wesley. 1 998. ISBN: 0202496842. 1
4 . Stewart, G . W. Matrix Algorilhms. Volume I: Basic Decompositions. SIA.
1 998. ISBN 08987 1 4 1 4 1 .
5. Wood. Aiistair. Introduction to Numerical Analysis. Addison-Wesley, 1 999.
ISBN 020 1 9429 1 .


1 . char, short. int. long, float, doule,
int* doule* ( sizeof,
<limits>) .
2 . si zeof,
atrix <int> ( 1 0 ) , atrix<int> ( l ) . atrix<doule> c ( lO ) , atrix
<int , 2> d ( 1 0 , 1 0 ) . atrix<in t , 3> ( 1 0 , 10 , 1 0 ) .
3. .
. 2 .
4. , i n t cin
sqrt ( )
int ",
sqrt () (..
. sqrt ( ) ) .
5.
atrix<doule>. atrix

1 : . . .
2. , 3- . - . : "" ,
2000.
24.1 . 999

push back ( ) ,
_

doue.
.
6. [O,n) * [ O ,m) J
atrix. n m cin
(,
m , ).
7. cin complex<doule> (
cin >> complex)
.
.
8. int atrix<in t , 2> m (2 , 3)
.


1 . ?
2. ?
3. ?
4. doue int?
5. ?
6. , -
int?
7. ? ? ?
8. ?
9. (,
) ?
1 0. ?
1 1 . ?
1 2. ?
1 3. ? .
14.
Fotran ?
1 5 . ?
.
1 6. (fused operation)?
1 7. z .
1 8 . ?
1000 24

1 9. lliycca.
20. ? ?
2 1 . ?
22. ?
23. I ?
?
24. r ?
25. - 1 ?

errno
Fortran
atrix
sizeof



1 . f a . apply (f) apply (f , a)


. triple ( )
{ 1 2 4 5 } .
triple ( ) .
a . apply ( triple) , apply ( triple , a) .
,
apply ( ) - r .
2. . 1 , , .
: trix . h.
3 . (, ,
). apply ( f , ) .
void ( & ) , ( cons t & ) ,
. : . boost : : ind.
4. lliycca, .. ,
.
5. =={ { 1 }
{ 1 } } = { 5 } ,
. elim wi th
partial_yivot ( ) .
24.1 . 1001

6. dot_product ( ) scale_and_
add ( ) .
.
7.
atrix. ,
vector, trix.
8. .
9. apply ( ) , r atrix,
, atrix,
. ,
apply (f , ) atrix<R>. R -

f . :
,
.
1 . defaul t random_engine?
_
, n d
. d r randint (n)
.
[ O , n) ,
. n
d, ,
.
1 1 . swap_columns ( ) , swap_
rows ( ) 24.5.3. ,
atrix.
: swap_columns ()
swap rows ( ) .
_
1 2.
atrix<doue> operator* (&trix<doule , 2>& , atrix<doule>&) ;

atrix<doule , N> operator+ (&trix<doule , N>& , atrix<doule , N>&)

.

, , ,
,
. .
, ,
.


; .
. , .
,

.
,

,
.
,
,
. h
,
.


,
.
/ .
1004 25

25 . 1 . 25.4.3. :
25.2.
25.2. 1 . 25.4.4.
25.2.2. 25.5. ,
25.2.3. 25.5 . 1 .

2 5 . 3 . 25.5.2. itset
25.3. 1 . 25.5.3.

25.3.2. 25.5.4.
25.5.5
25.3.3. 25.5.6. :
25.3.4.
25.4. , 25.6.
25.4.1 . 25.6. 1 .
?
25.4.2. : 25.6.2.
25.6.3.

2 5. 1 .

.
. .
.
, .
. . ,
.
..
.
; .
. ,
:
, .
. ,
.
r ( ).
(,
).
25. 1 . 1005

().
.
JUt .
. .
-rr.
(, ).
( ,
) .
JUt (, ).
,
, .
. -
200 .
JUt t .
.
.
.
JUt (
) .
JUt .
,

. , ,
: ", !"

. ,
,
. ,
( ),
.
,

.
.
.
. , .
,
.
, .
1006 25


( ) .
,
.
.
(
) .
1 .
. . .
? : .
"" ,
?
.
: (
) . . - (
. . ,
).
(. . ) .
. . .
. . .
, .
25. 1 . 1007

,
2 I 8 ,

60 I 256 .
- l I
, . ,
(, ,
, , , , ..),
, .
w .
,
1 00 ;
, .. ,
1 1 30
. , .
.
, . , ,
, ,
(, -,
). CUIA
20
( , ,
).
.J .

,
.
(
,
). -
, .

, - .
. -
, .
- ,
, ,
. ,
. .
,
. ,
.
1008 25

++? ,
. ,
.
. ,
, ,
++.

25.2.
,
,
, -
, .
:
. ,
.
. ,
. - .

. , .
,
. .

,
.
. ( ,
) .

.
" r
":
.
, ,

. " "
".
11 .
, . ,

,
.
, -
25.2. 1009

.

. , -
.
.. ,
" .
" .

. .

,
(. ).
. , .
.
.
.
.
. ( ),
. .
.
. .
.
.

. " "
. .
.
. "
". ,
. " "
.
"? - ,
;
( )
( ) .
. . (
) . .
. , .
. , .
.
. ,
1 00 . 32 -
1010 25

,
. w"
,
, .
, ,
. ,
, ,
.
.
u11
(hard real time),
.
,
,
(soft real time).

.

,
.
,

.
, :
, , ,
.
.
. ,
, -
- :
(predictae),

. , -
, +
, +
, , -
. ,
,
(, ,
),
.
( )
25.2. 1011



.
(,
find ( ) ) .
.
,

. ,
.
, - ,
- ,
.
.
, . ,
,

. ,
, (concurrent. parallel). ,
.
.

25.2.1 .
++ ,
. ++ (
) ,
.
new delete
(. 25.3).
( 1 9. 5) .
dynamic cast ( . 5.7).
_

. , new delete,
25.; .
, string
(vector, map .)
, .
dynamic_cast .
.
, .
throw, ,
101 2 25

tch
tch, .
.
catch , ,
++. ,
, ,
, , catch
throw
, , ,
, ,
,
.

2 5. 2.2.

.
-
.
.
,
, .
, -
,
. -
!
( ).
, . ,
. (
) ,

. ,
,
; " " ,
, .

, ,

. ,
,
,
-
.
25.2. 1 101 3

(John Bentley).
. .
: " !"
( ): " !"
. ,
. . ,
( ) .
.
.
. , :

. : .
.

25.2..
, ,
. " "
" ".
? ,
.
. , .

. .
.
.
.
.
( ) .
,
. .
.
(translent error) , .
. ,
, 54.
,
, , ,
, .
, ,
. ,
.
101 4 25


( ) . .
, .
, .. ,
.
, .
. ,
.
. .
, ,
( ).
.
. ,
, , .
. ,
- (,
, , ),
: .v .v
? ,
, .v

. ,

, .v . ,
(,

). ,
.
. , (
) .

(, ).
,
.
, (. 9.4.3)
" " (
(assertions)). ,
, .v ,
.
- .
.
.v. -
25.. 101 5

:
. .
, .
(
, ),
.
.
, ,
, , , . ,
(
),
, ,
( )
. -
, , ,
.
,
.
.
, (,
),
.
, , ,
, "
". ,
(
,
) .
, ,
, , .
,
( 26).

25..
(
) ( ). ++
(. 1 7.4 .4.2).
. ,
.
101 6 25

() .
.
(). new

delete

. ,
, (. 25. 2 . 1 ).

.
-
:
.
.
, .
. -
.
, ,
; ,
, (, fl f2 . . .
fn) .
.

, . .
, factorial ( 1 0 ) factorial
. . ,
factorial (.
1 5.5), .

; . new ,
,
delete . .
.
; . .
. , :
new .
.
.
m..
; ,
"".
25.. 101 7

. .
"" ,
. .
,

.
,
. ,
. new delete

. ,
, ,
.

25.. 1 .
, new?
new delete,
.
.
Message* get_input (Device&) ; // Message

while ( / * . . . * /) {
essage* = get_input (dev) ;
11 . . .
Node* nl new Node (arq1 , arq2 ) ;
11 . . .
delete ;
Node* n2 new Node (arg3 , arq4) ;
11 . . .

, . Node,

Message.
, - .
I , ,
2 *sizeof (Node) (
). ,
,
2*sizeof (Node) . .
( )
. , Message ,
Node.
: - ,
101 8 25

Message, - - , Node,
- "" (.. ).

n1 ( Message Node)

( " " Node)

11 1 n2 ( Node "" )

1 1 / 1 n1

11 11 n2

.____._I_._J _._l_._J _,__._l_.l_I


_ _._
_ _
n2

,
("").
, ,
,
.
"", ,
, (
fragmentation).
"", ,
, , ,
"".
, new
delete; .
, new,

. ,
.
, .
, ?
, "" ?

"" :
Node ,
, .
, .
, ++ ,
25.. 101 9

. , nl n2
. ,
, . , (-)
.
.

j1 1j1j1 1j1!1 1!

, ,
.

, ,
, .
, ? ,
.
++ ,
; , , "
?"
. ,
( ru (compactig garbage collec
tion)) . ,
, ,
, ,
.
. ,
.
"I ?"
,
, .
Node
Message.
while ( . . . ) {
Node* nl = new Node ;
Node* n2 = new Node ;
essaqe* = qet_input (dev) ;
11 . .
. . . .
1020 25

delete ;
11 . . .

-
.
,
.
,
. , .

&
, ,
, ,
"" .
. , ,
, .

2 5..2.
, .
? -, new
: "",
delete. , delete.
, . .
delete , new
: , new
. -
. .
.
.
,
. new
. ( )
( ) .
-
.
.

(. 25.6).
,
.
25.3. 1021

. (stack) - r ,
(
), ,
; ..
. ,
"".
. (pool) - .
,
, .
,
.

.
,
,
, . ,
,
,
( , ).
, ++
(vector, .). string
,
new.
, ( , )
, , ,
++,
.
,
, ,
,
, .
, ,
. .

25...
- ,
, r.
, .
- - ,
,
.
1022 25

Pool :
template<typename , int N>
class Pool { // N
puic :
Pool ( ) ; // N
* qet ( ) ; // ;
11 ,
void free (T*) ; 11 ,
11 ge t ()
int availale ( ) const ; 11
private :
11 T [N] , ,
// , ( ,
// )
};
Pool
. ,
.
Pool<S.all_buffer , 10> sb_J>ool ;
Pool<Status_indicator , 200> indicator_J>ool ;

S.all_buffer* = sb_J>ool . qet ( ) ;


11 . . .
sb_J>ool . free (p) ;
, , - .
"" .
,
get ( ) , ,
.
get ( ) - ,
.
,
. ,
.
(, dial_buffer_POOl . get ( ) ),
(
, ).

.
, Pool
. ,
, ,
25.. 1023

, .
.
, .

25...
- ,
. -
- .
, .

: f---
--+-- - 1. )
, "" .
, .
template<typename , int N>
class Stack { //
11 . . .
1;

. . ,
, ""
. .
template<int N>class Stack { // N
puic :
Stack ( ) ; // N
void* get (int n) ; 11 ;
11 ,
void free ( ) ; // ,
11 ge t ()
int availale ( J const; 11
private :
// cha r [N] , ,
// , ( , )
1;

get ( ) void* .
,
. . .
.
Stack<50*1024> my_free_store ; // 50

void* pvl = my_free_store . get ( 1024 ) ;


int* buffer static_cast<int*> (pvl) ;
1024 25

void* pv2 = :y_free_store . qet ( si zeof (Connection) ) ;


Connection* pconn = new (pv2) Connection ( incominq , outqoinq ,buffer) ;
s tatic_cast 1 7.8.
new (pv2 ) new. .:
, pv2.
. ,
Connection (incoming ,
outgoing , uffer) . ,
.
, Stack
. ,
, ,
.

25.4. ,
,
- .
,
(
, ). ++
.
.
( ) .
.
,
(). .
, ,
() (.
array; . 20. 9).
.

25.4.1 .
(,
)
.

, :
Device driver* = reinterpret_cast<Device driver*> (Oxff8 ) ;
25.4. , 1025

(. l 7.8.)
.
- /
( , ) -
,
, .
(
).
(,
) reinterpret_cast,
int .
(reinterpret_cast, static_cast ..; .
.5 . 7) , .
() ,
, ++
.

25.4.2. :
( 1 8.6. 1 ),
( ).
"" ,
,
.
. ,
, .
(, , )
.
void poor ( Shape* , int sz) 11
{
for (int i = ; i<sz ; ++i ) p [i ] . draw ( ) ;

void f ( Shape* q, vector<Circle>& sO) //


{
Polygon sl [lO ] ;
Shape s2 [10 ] ;
//
Shape* pl = new Rectangle { Point { 0 , 0 } , Point { l0 , 20 } } ;
poor ( &s0 [ 0 ] , s0 . size ( ) ) ; // # 1 ( )
poor ( sl , 10) ; // #2
poor ( s2 , 20 ) ; // #3
poor (pl , 1 ) ; // # 4
delete pl ;
pl = ;
1026 25

poor (pl , 1 ) ; 11 # 5
poor (q,.ax) ; 11 #6

poor ( )
:

.


, ,
f ( ) ? ,
poor ( ) ?

, ,
.
.
1 . (. poor ( & [ ] , .
size ( ) ) . , sO ,
&sO [ ] .
2. " " ( ):
poor ( s l , 1 0 ) . .
3. " " ( -
) : poor ( s2 , 20 ) .
4 . poor (pl , 1 ) ( ).
5. poor (pl , 1) .
6. poor ( q , max) , , . ,
. .
q , max ,
q max
.
.
- .
.
poor ( ) ,
. ,
,
. pl . ,
.

(, poor (pl , 1 ) , pl=O ) .
25.4. . 1027

,

Shape.
poor ( ) , .
Shape, ,
.
, poor ( &sO [ O ] , sO . si ze ( ) )
? &sO [ 0 ]
Circle; Circle*.
Shape* ,
Shape ( Circle*). :
,
-
(
Shape) ( 1 4.2). poor ( )
Shape* ; ,
.
for ( int i = ; i<sz ; ++i ) p [i ] draw ( )
. ;

, . & [ ] , &p [ l ] , & [ 2 ] ..


&[] &[1 ] &[2 ]
1 \, \,
J.____1L---1'---
- ----'

sizeof (Shape) (. 1 7.3. 1 ).
, poor ( ) , sizeof (Circle) ,
sizeof ( Shape) ,
.

>- :;:/:-:)-_"J_
&[ ] &[1]& [ 2]
), - - - - - -: -1- - - - - - - :
-:..:.1 - - - --/:- - - - - - - - - - -
- - - - -

- - - - - - - ::_:____: - - -

7
1 - Circle 2- Circle 3- Circle

, poor ( ) draw ( )
, - Circle! ,
.
poor ( s l , 1 0 ) .
" ".
, -
, . ,
Polygon
1028 25

,
Circle, , Polygon
Shape ( Circle; .
1 3.8 1 3 . 1 2), .. sizeof ( Shape) ==sizeof ( Polygon)
- - Polygon
, Shape. , ,
Polygon
. , poor ( s l , 1 0 ) , ,
. .
, ,
, " D -
" , " Container<D> -
Container<>" (. 1 9.3.3). .
class Circle : pulic Shape { /* . . . */ } ;
void fv (vector<Shape>&) ;
void f ( Shape &) ;
void q (vector<Circle>& vd, Circle & d)
{
f (d) ; 11 : Ci rcle Shape
fv (vd) ; // : vector<Ci rcle> vector<Shape>

, poor ( ) - .
? ,
,
?
:
" "?
,

- .
iPod,
-
.
(, ),
. , - ,
- .
,
.
,
. ?
,
vector. ,
25.4. . 1029

void poor ( Shape* , int sz) ;



void qeneral (vector<Shape>&) ;
, -
std : : vector ( ),
vector ( )
.
vector
, ,
, -
(Array_ref) .

25.4.. :
,
s td : : vector, .
,
vector, ( ) ,
vector, .
, ,
.
( ,
, ..).
( .
).
( .
, ) .
() (, -
).
.
.
, .
" , "
. ,
,
.
.
template<typenal8 >
class Array_ref {
pulic :
1030 25

Array_ref (T* , int s) : p (pp } , sz ( s } ( }

& operator [ ] ( int n) ( return p [n ] ;


const & operator [ ] ( int n) const ( return p [n] ;

bool assign (Array_ref )


(
if (a . sz ! =sz) return false ;
for ( int i=O ; i<sz ; ++i ) ( p [i ) =a . p [i] ;
return true ;

void reset (Array ref ) ( reset (a . p , a . sz) ;


void reset (T* , int s) ( -
- ; sz=s ; }

int size ( ) const ( return sz ; }


// - :
11 Array_ ref
11 Array_ref
private :
;
int sz ;
};
Array_ref .
push_back ( ) (
) at ( ) ( ).
Array_ref .
(p, sz).

acca Array_ref, ,
.
(p, size) reset ( ) ,
Array_ref (
).
Array ref (
).
Array_ref , .
Array_ref
,
. -
array (. 20. 9).
Array_ref,
.
template<typenae > Array_ref<T> make_ref (T* , int s)
(
25.4. . 1031

return () ? Array_ref<T> (pp , s } : Array_ref<T> ( nullptr , 0 } ;

Array_ref ,
. . ,
, . ,
,
,
Polygon [ 1 0 ] Shape* ( ,
25.4.2), .
.

( ) .
template<typename > Array_ref<T> make_ref (vector<T>& v)
{
return (v . size ( ) ) ? Array_ref<T> { &v [O ] , v . size ( ) }
Array_ref<T> { 0 , 0 } ;

, .
vector, ,
Array_ref . ,
,
(. , : .
25.3.3).

, .
template <typename , int s> Array_ref<T> make_ref (T ( &pp) [ s ] )
{
return Array_ref<T>{pp , s } ;

( &) [ s ]
s .
Array_ref .
, , .
Polyqon ar [ O ] ; // 011 :
Array_ref,
.
void better (Array_ref<Shape> )
{
for (int i = ; i<a . size ( ) ; ++i) a [ i ] . draw ( ) ;

void f ( Shape* q, vector<Circle>& sO)


1032 25

Polyqon sl [ lO ] ;
Shape s2 [20] ;
//
Shape* pl = new Rectanqle { Point { 0 , 0 } , Point { l0 , 20 } } ;
etter (make_ref ( sO ) ) ; 11 : Array_ ref<Shape>
better (make_ref ( sl) ) ; 11 : Array_ ref<Shape>
better (make_ref ( s2 ) ) ; 11 ( )
better (make_ref (pl , 1) ) ; 11 :
delete pl ;
pl = ;
better (make_ref (pl , 1) ) ; 11 :
better (make_ref ( q , max) ) ; // ( )

.
.
, ,
( Array_ref),
.
, Circle [ ]
Shape [ ] Polygon [ ] Shape [ ] .

sl s2.
max (
,
) - ,
.

.

2 5.4.4.
Circle
Shape, . . ,
better ( ) (
draw_all ( ) : . 1 9.3.2 22. 1 .3)
? , . 1 9.3.3 25.4.2
,
vector<Circle> vector<Shape>.
Array_ref<Circle> Array_ref<Shape>.
, , 1 9.3.3,
. .
25.4. . 1033

, ,

( ). a [ i ] . draw ( )
better ( ) .
, (->). .
? -, (
). ,
Array_ref<Circle*>. Array_ref<Shape*> ,
Array_ref<Circle>. Array_ref<Shape> ..
- Array_
ref<Circle*> Array_ref<Shape*>.
Array_ref<Shape*> ,
Circle*. . .
acca Array_ref<Shape*>;
Shapel
:
Array_ref<Circle*> Array_ref<Shape*>
, Array_ref<Shape*>.

( , ),
, 25.4.2.
, ,
Array_ref<Circle*>
Array_ref<Shape*>. . ""
. .

vector<Clrcle*>

Circle*

Silly_face
Circle
(
Smlley_face)
Smiley_face
(
o Clrcle)


Circle* Shape*
( Array_ref).
1034 25

, .
,
. ,
,
( : .
25.4.2). , :
, , .
,
. .
better ( ) ,
, -
.
void etter2 (const Array_ref<Shape*const> )
{
for (int i = ; i<a . size ( ) ; ++i )
if ( a [ i] )
a [ i] ->draw ( ) ;

.
. ,
better2 ( )
Array_ref,
const. const ,
Array_ref , assign ( )
reset ( ) . const (*).
, (
): .
, , .
, : ,
Array_ref<Circle*>
Array_ref<Shape* > (
better2 ( ) ) :
Array_ref<Shape*>.
, Array_ref .
template<typename >
class Array_ref {
puic :
11

template<typename Q>
operator const Array_ref<const Q> ( )
{
25.4. . 1035

11 :
static_cast<Q> ( *static_cast<T*> (nullptr) ) ;
11 Array_ref:
return Array_ref<const Q> ( reinterpret_cast<Q*> (p) , sz } ;

11
};

. .
Array_ref<const Q>
Q,
Array_ref<T> Array_ref<Q> (
, ,
).
Array_ref<const Q>,
( reinterpret_cas t) ,
.
;
Array_ref ,
( . 1 2.4).
const Array_ref
<const Q>: ,
Array_ref<const Q> ,
Array_ref<Q>.
,
. acca Array_ref
( ).
void f ( Shape* q, vector<Circle*>& sO)
{
Polyqon* sl [lO] ;
Shape* s2 [20 ] ;
11
Shape* pl = new Rectanqle { Point { 0 , 0 } , 10 } ;
etter2 (make_ref ( s0 ) ) ; 11 :
11 Array_ ref<Shape *con s t>
etter2 (make_ref ( sl) ) ; 11 :
11 Array_ ref<Shape *con s t>
etter2 (make_ref ( s2 ) ) ; 11 ( )
etter2 (make_ref (pl , 1) ) ; 11
etter2 (make_ref (q,max) ) ; 11

,
Shape* , better2 ( ) Array_
ref<Shape*>: . better2 ( ) ,
1036 25

, . etter2 ( )
, (.
) .
make_ref ( &pl , 1 ) .
(
),
.
, ,
, ,
. .
(David Wheeler) "
" " ".
.

25.5. ,
.
, , ,
.
, doule,
string, atrix Simple_window.
,
.

, .2. 1 . 1 .

25.5. 1 .
- , .

7: 6: 1 : :
5: 4: 3: 2:
l1lol1 lolol1 l1 l1I
( ) (
). .
.
3: 2: 1: :
1 Oxf f
1 1 0 1 Oxde 1 Oxad

, ..
. :
. (.
). ,
25.5. . 1037

, - . I -
,
8- 4- .
,
<limits> (. 24.2. 1 ).
.
:
static_assert ( 4<=sizeof ( int) , " int 111 " ) ;
static_assert ( ! numeric_limits<char> : : is_siqned , "w char" ) ;
static_assert
, . , ..
, ()
.
++? ,

.
.
bool - 1 , . 8 .
char - 8 .
short - 1 6 .
int - 32 .
1 6- .
long int - 32 64 ( int).
long long int - 32 64 (
long int) .
,
,
. ,
.
std : : vector<ool> - ,
B*sizeof ( long) .
std : : itset - , B * sizeof (long)
.
std : : set - (.
2 1 .6.5).
: ( 25.5.6).
,
++.
1038 25

(enu.m); . 9.5.
; . 25.5.5.
,
, ,
,
, .
,
(, 8, 1 6, 32 64),

, ,
.
.
,
: -
- ,
-
.
. ++
,
.
.


n- 1 1 , n- n-
1
& n- & 1 , n- , n-
1 .
" - n- " 1 , n- , n-
1 ,
<< n- x<<s (n+s)-y
>> n- x>>s (n-s)-y
n- n-

,
w " ( " ,
w").
.
, ,
<< .
, ,
ostream, - .
, & & & ,
1 1 1 ,
25.5. . 1039

( .5.5),
, .
& & 1 1 true false.
.
. ( ) -
.


0000 1000
Oxl 0001 9 1 001
2 0010 1 01 0
0 0 11 1011
4 0100 1 1 00
5 0101 Oxd 1101
0110 1110
7 0111 Oxf 1111

, .
.
.
.
. . ,
.


0000 0000
OxOf 0 000 1111
OxfO 1111 0000
Oxff 1111 1111
1 01 0 1010
55 0101 01 01

, unsigned ( 25. 5.3).


.
unsigned char = ;
unsigned char = - ; //

: l 1 1 l 1 1 l 1 1 1 1 1
- : 1 1 1 1 1 1 1 1 1 55
unsiqned char = OxOf;
unsigned char xl = &; //
1040 25

: l 1 1 l 1 1 1 1 1 1 1 1
: 1 1 1 1 1 l 1 1 1 Oxf
& : 1 1 1 1 1 l o l l o l oxa
unsigned char 2 ; // : xor

: l 1 1 l 1 1 1 l o l 1 l o l oxaa
: 1 1 1 1 1 1 1 1 Oxf
" : l 1 1 l 1 1 1 1 1 1 1 5
unsigned char a<< l ; //

: l 1 1 l 1 1 l 1 1 i 1 1 1
1 : 1 l 1 1 l 1 1 1 1 1 1 1 54
, "" ,
, ,
( 7) .
unsigned char 4 == >>2 ; //

: l 1 1 1 1 1 1 1 1 1
2 : 1 1 1 1 1 1 1 1 1 2
,
, ( )
.

, .
,
.
int main ( )
{
for (int i ; cin>>i ; )
cout << dec << i << 11 = 11
<< hex << "" << i << "=="
<< bitset<B*sizeof ( int) > ( i } << ' \n ' ;

,
i tset .
bitset<B*sizeof (int) > { i }
25.5. . 1041

i tset .
, int - B * s i
zeof (int) , itset i .
-

&

,
.
, 25.5.3.

25.5.2. i tset

i tset <i tset>.
i tset ,
.
bitset<4> flaqs ;
bitset<l28> dword_bits ;
bitset<l2345> lots ;
i tset ,
. i tset
, :
itset<4> flaqs = ;
itset<l28> dword_its ( strinq ( " l010101010101010 " } } ;
itset<l2345> lots ;
lots , dword_i ts 1 1 2 ,
-

1 6 .
itset ,
' ' ' 1 ' , std : : invalid_argu.ment.
strinq s ;
cin>>s ;
itset<l2345> my_its ( s } ; //
11 s t d : : i n va l id_a rgumen t

i tset
. , , 2
itset.
= 2& ; //
= 2 1 3 ; //
1 = 2 ; // xor
= 2 ; //
= 2<<2 ; //
= 2>>3 ; //
1042 25

, i tset
unsigned int ( 25.5.3),
, . ,
unsigned int (
), i tset. ,
itset .
cin>>b ; // bi t s e t
cout<<i tset<S> { ' c ' } ; // ' '

i tset,
. .
10121
101 , 21 .
, i tset
(
), , , 27
7: : 5 : 4: : 2: 1: :
[1ioi1 ioioi1 !1 l1 i
i tset ,
i tset .
int main ( )
{
constexpr int = 10 ;
bitset<max> ;
for (bitset<ma.x> ; cin>>b ; } {
cout << << ' \n ' ;
for ( int i = ; i<ma.x ; ++i )
cout << b [ i ] ; //
cout << ' \n ' ;

i tset,
, .

2 5. 5..
, ++
: .
: .
- , - ..
:
? ++
,
25.5. . 1043

.
( ) .

1{1

8 == 1

1 6- ( )

, .

. ,
.
2 4 7
0000 0001 0010 0100 0111
1111 1110 1101 1011 1000
-1 -2 -3 -5 -8

(x+l )
-

( , ": . 25.5. 1 ) .
(,
int).
.
(
, int).
-
(, unsigned int).
, ,
.
,
. , ,
,
int 1 6 , -
v . size ( ) vector :
.
vector<int> v ;
11 . . .
for (int i = ; i<v . size ( ) ; ++i ) cout << v [i ] << ' \n ' ;
" ,
(.. i) (.. v . size ( ) ).
, ,
. , i :
, v . size ( ) ,
1044 25

int . i
,
int ( ,
int, , .. 2 1 5 - 1 ) .
++ ,
, .
! ,
,
int. ,
1 6- int (,
). v . size ( ) 32* 1 024 ; 32-
int ,
i 2* 1 024* 1 024* 1 024.
,
.
, ,
,
. .
size_type, vector,
for .
for (vector<int> : : size_type i = ; i<v . size ( ) ; ++i )
cout << v [i ] << ' \n ' ;
for (auto = v . beqin ( ) ; p ! =v . end ( ) ; ++)
cout << * << ' \n ' ;
for ( int : v) cout << << 1 \n ' ;
size_type - ,
, int. .
- (
, ). ,
, .


,
:
void infini te ( )
{
unsiqned char = 160 ; //
for ( siqned char i=O ; i< ; ++i)
cout << int (i) << ' \n ' ;

, .
25.5. . 1045

,
.
( +, -. * /).
.

, .
, .
, , ++ ( )

. .
unsiqned int ui = -1 ;
int si = ui ;
int si2 = ui+2 ;
unsiqned ui2 = ui+2 ;
, : ,
ui 4 294 967 295.
32- ,
- 1 ( ).
- 1
, ;
.
, si
- 1 . , si2 1 (- 1 + 2 = 1 ). ,
ui2 : 4 294 967 295 + 2 1?
4 294 967 295 (Oxffffffff),
: 4 294 967 295 - 32-
, 4 294 967 297
32- - , .
. 4 294 967 295+2
( )
. . 32-
2 32
? . ,

- .
.
? .
Int i = ;
while (++i) print ( i ) ; 11 i

?
, Int ( ,
1046 25

I ).
, ,
. Int (, unsigned char,
unsigned int unsigned long long). ++
, ,
, ( ). Int
(, signed char),
,
( ). , signed char
1 2 . . . 1 26 1 27 - 1 28 - 1 27 . . . -2 - 1 .
?
.
, .
, .
( ) .
.
int si = 257 ; 11 cha r
char = si ; 11 cha r
unsigned char uc = si ;
signed char sc = si ;
print ( si) ; print (c) ; print (uc) ; print ( sc) ; cout << ' \n ' ;
si 12 9 ;
= 11 s i gned cha r
= si ;
uc = si ;
sc = si ;
print ( si) ; print (c) ; print (uc) ; print ( sc) ;
:
257 1 1 1
129 - 127 129 -127
: 257 ,
(255 "
"), 1 29 ,
( 1 27 " "),
. , ,
char (
sc uc).


.
si=l28.

.
25.5. . 1047

, print ( ) ?
.
cout << i << ' ' ;

, i char,
, . ,
,
teplate<typename > void print (T i ) { cout << i << ' \ t ' ; }
void print ( char i) { cout << int ( i ) << ' \ t ' ; }
void print (signed char i ) cout << int ( i ) << ' \ t ' ;

void print(unsigned char i) { cout << int ( i ) << ' \t ' ;


:
( ).
, .
,
.
, -
.
,
.

.
.

25. 5.,.
? ,
.
, -
, , .
.
.
, .
, ,
( ').
. : ,

, " ("People who play with its will itten").

1 ( .
. 2- 11.. . : " .. ", 20 1 4)
-

: Hacke's Delight ( ). . .
-
1048 25

, ?
,
:.
(""),
(
), (
) ( -
) .
, ()
(, ,
-).
void f ( short val) 11 i n t 1 6 (2 )
-

(
unsiqned char riqht = val&Oxff ; // ()
unsiqned char left = val>>B ; // ()
11 . . .
bool neqative = val&OxSOOO ; 11
11 . . .

- . "
" ("shift and mask"). ("shift"),
<< >>. (
), . ("mask"),
"" ( &) (
Oxff), ( ) .
.
:
enwn Printer_flaqs {
acknowledqe "1 ,
paper_epty =1<<1 ,
busy =1<<2 ,
out_of_ack=l<< ,
out_of_color=1<<4 ,
11 . . .
};
,
. .
out of color 16 OxlO 0001 0000
out of k 0000 1000
busy 4 4 0000 0100
paper_empty 2 2 0000 0010
acknowledqe 1 Oxl 0000 0001
,
.
25.5. . 1049

unsigned char = out of color out_of_ack ; // 24 ( 1 6+8)


1 = paper_epty ; 11 26 (24+2)

. 1 " " (
=

" "). &


" ?":
if (& out_of_color) /! o u t of col o r ?
11 . . . 1 1 ,
1
& .
unsigned char = & ( out_of_color 1 out_of_ack) ; // = 24
4 3 (out_
of_color out_of_lack).
enum .
, .
:
11
Flaqs z = Printer_flaqs (out_of_color 1 out_of_lack) ;
. .
out_of_color 1 out_of_lack
Flags. :
,
. 24 (out_of_color 1 out_of_ack) .
, ( - ).

25.5.5.
,
. ,
-
.
, (device
register). ++
- (tfields).
.
. ,
.
: 31: 9: 6: 3: 2: 1: :

PPN: \ 22 \3 \3 \ \1 \ 1 \1 1
1

: PFN dirty global


rh valid
1050 25

32- ( 22
- 3 ) ( ).
.
( ) .
:
struct PPN { // R
unsiqned int PFN 22 //
int : ; 11
unsiqned int ; 11
bool nonreachae : 1 ;
bool dirty 1
bool valid 1
bool global 1
};

, PFN
, .
.
.
.
(, 8) .
, , , ,
. , , .
,
.
void part_of_V_syste ( PPN )
{
11
if (p->dirty) //
//
p->dirty = ;
}
11


, , .
, PPN pn,
:
unsiqned int = pn . CCA; //


int pni, :
unsiqned int = (pni>>4 ) &0x7 ; //
pni ,
, 7 (. .
25.5. . 1051

).
. pn . CCA,
, , . .
(
, PPN, PFN) .

25.5.6. :

- Tiny Encryption Algorithm ().
(David Wheeler)
(. 22.2. 1 ) . ,
.
(
).
,
.
. .

- http : //en . wikipedia . org/wiki/
Tiny_Encryption_Algori thm ,
(Simon Shepherd)
(Bradford University). .
( !).
/ (
/) . .
, - .
, , ,
, ,
.
. ,
( )
, . , (
, ).
, ; ,
, .

long (v [ ] , v [ 1 ] ) . ,
; ,
long (w [ O ] , w [ l J ).
; long (k [ O J . . k [ J ),
.
1052 25

void encipher (const unsiqned lonq *const v ,


unsiqned lonq *const w ,
const unsiqned lonq * const k)

static_assert ( sizeof ( lonq) ==4 ,


" lonq " ) ;

unsiqned lonq = v [O ] ;
unsiqned lonq z = v [ l ] ;
unsiqned lonq sum = ;
const unsiqned lonq delta = 937 7 9 9 ;
for (unsiqned lonq n = 32 ; n-->O ; ) {
+= ( z<<4 z>>S) + zsum + k [ sum&3 ] ;
sum += delta ;
z += (<<4 y>>S ) + ysum + k [ swn>>ll & 3 ] ;

w [ O ] =y ;
w [ l ] =z ;

,
, , .
(<< >>).
"" (") "" (&) (
) . ,
long . " "
(, , sizeof ( long) 4).
,
.
.

.
, . n
: , .
, n==2 .
.
void decipher (const unsiqned lonq *const v ,
unsiqned lonq *const w,
const unsiqned lonq * const k)

static_assert (sizeof ( lonq) ==4 ,


" lonq : " ) ;

unsiqned lonq = v [O ] ;
unsiqned lonq z = v [l ] ;
unsiqned lonq sum = OxC6EF3720 ;
const unsiqned lonq delta = 937799 ;
25.5. , 1053

// s = del ta<<S , s um = del ta *

for (unsigned long n = 32 ; n-- > ; ) {


Z -= ( << 4 >> 5) + SWll + k [ SUJll>> ll & 3 ] ;
swn -= delta ;
-= ( z << 4 z >> 5) + z swn + k [ swn&3 ] ;

w [ O ] =y ;
w [l ] =z ;

, ,
.
int Dlllin ( ) 11

const int nchar 2*sizeof ( long) ; 11 64


const int kchar = 2 *nchar ; 11 1 28

string ;
strinq key ;
strinq infile ;
strinq outfile ;
cout << " "

<< " . : \n" ;


cin >> infile >> outfile >> key ;
while (key . size ( ) <kchar) key += ' 0 ' ; //
ifstream inf ( infile . c_str ( ) ) ;
ofstream outf (outfile . c_str ( ) ) ;
if ( ! inf 1 1 ! outf) rrr ( " ." ) ;

const unsiqned long* k =


reinterpret_cast<const unsiqned long*> (key . data ( ) ) ;

unsiqned long outptr [ 2 ] ;


char inuf [nchar] ;
unsiqned long* inptr =
reinterpret_cast<unsiqned long*> ( inuf) ;
int count = ;

while (inf . get ( inuf [count] ) )


outf << hex ; //
if (++count == nchar) {
encipher (inptr , outptr , k) ;
11 :
outf << setw ( B ) << setfill ( ' O ' ) << outptr [ O ] << ' '
<< setw ( B ) << setfil1 ( ' 0 ' ) << outptr [ l ] << ' ' ;
count = ;

if (count) { //
while (count ! = nchar) inuf [count++] '0' ;
1054 25

encipher ( inptr , outptr , k) ;


outf << outptr [ O ] << ' ' << outptr [ l ] << ' ' ;

while;
. while inuf
, ,
encipher ( ) . ;
, .
, .
, 64 (
long ), . ,
inuf, unsigned long*
. ;
1 28 ( unsigned long),
, 1 28
. ,
64 (8 ) .
? .
,
ASCII Unicode,
. - ( 1 1 .3.2).
.
58f57 806f 2d72335 2398 9dld 991206 036308
8f8111ac 38f3f2f3 91104 c5e1389f 64d7efe8 133559
4cc00fa0 6f77e537 de7925f f87045f0 472bad6e dd228bc3
5686903 5191 fc1 9144e d3bcde62 4fd7dc8 43d565e5
fld3f026 2887412 97580 690 d2ea4f8b 2d8f7 936cfa6d
6a13ef90 fd036721 b80035el 74 67d8d8 d3267 29923fde
1 97d4cd6 7 6874951 418843 96442 10848 ba67dcd8
7115211f d32069 e4e92f87 8f33 8f942 96587
44489114 18d4f2bc 25dalbf 571 788 9 11372 1266223
645 82499657 a8265f44 7866 780631 e91475el
59918 6d73 71424 8d78f 68b d02bfe4 dleadde7
55f20835 la6d3a4b 202368 6ale0f2 7 7 19 9f3 lldldOa
74a8cfd4 4ce54f5a e5fda09d acd.fllO 259ala19 96439
456fd8a3 le78591b 07c8f5a2 101641 d0c9d7el 60d11
b9ad8e72 ad30b839 201f c553 34794 217ca84d 30f 666c6
d018e61c dlc94ea6 73314 cdOdefl 616870 45b94dc0
d7b44fcd 960425 72839f71 d5b6427c 214340f9 8745882f
0602cla2 437759 03903 bd4d8460 edd0551e ld34dd3
c3f943ed d2cae477 4d9d0b61 f647c377 Od9d303a celde974
25.5. . 1055

f9449784 df460350 5d42b06c d4ded54 17811b5f 4f723692


14d67ed llda5447 67059 4600f047 64393 2e9dl5f7
4f2lbbbe d759 433564f5 c3ff2597 alealdf 3052 7 13
9421d209 252384f f78fae7 d03clf58 6832680 207609f3
9f2c5a59 ee31f147 23651 e017d9d6 d6d60ce2 2elf2f9
9d58 9565730 cad37fda 7bce06f4 457daf44 257206
418245 de687477 51155 f744fff 26800820 92224e9d
4051 dl68f2dl 624c54fe 799473 18f 62452495
5de382cl la789445 00178 583446 dcd64c5 dddale73
fa168da2 60109 710240 9fed3a0b 44245e5d f612ed4c
b5c161f8 97ff2fc0 ldf5674 45965600 b04c0afa 537770
997 1624516 d3556 6de6eda7 d159 71d5cla6
887d 31 6a0fc9 62c01a3d Oa24a51f 86365842 52daf4d
37218 9a5df281 35c9f8d7 07c8f9b4 36d95 08934
239f5 5fe3fa6f 659df805 faf4c378 4c2048d6 e8bf4939
311 6793 4d17818 9 98244 55d8 79 9077 43d26aef
d5682864 05e641dc 59488 03457e3f 80c934fe cc5ad4f9
d162 a50aalef d62eflcd f8fbf 67 30c17f12 718f4d9a
432 95fed 561de2a0


bs; ?

,
- . .
, -
, ,
.
.
. .
.

:
unsigned long inptr [ 2 ] ;
char outuf [nchar+l] ;
outuf [nchar] =O ; 11
unsigned long* outptr
reinterpret_cast<unsigned long*> ( outuf) ;
inf . etf (ios_base : : hex ,
ios_base : : basefield) ; 11

while (infinptr [ O ] inptr [ l] ) {


1056 25

decipher (inptr , outptr , k) ;


outf<<outuf ;


inf . setf (ios_base : : hex , ios_base : : basefield) ;
.
outuf, ,
.

? ,
,
.
:
, -
: , ,
.
encipher ( ) decipher ( )
. ,
, ,
++,
+, . , "
" .

25.6.
.

, .
( ), ,
.. .
, -
, .. - ,
.
2
, " ",
++,
. .
, -

2 "", "
", -
, .
. . . . -
25.6. 1057

"" , r
new.
, ,
r , r ,
. .
, for,
while.
,
r . ,
r -
, .
,
++ ,
.
, ,
, ,
,
. ,
, .

.
u . ,
,
r .
"
.
1111 11.
,
, .
.
,
.
. ,

.
.

,
, r
.
1058 25

25.6 . 1 . ?

; ..
,
.
: " ".
, , .
,
, ,
. .

, . ,
. -
: " -
?"
. : "
! " .
,
, ,
. ,
.
,
,
. ,
;

. . ,
CamelCodingStyle ,
underscore style, ,
_

. ,
. ,
, , ,
- .
.

.
,
.


.
25.6. 1059

- , ,
, :

, " ";
, "
new throw";
, "
, ";
, "
<iostream>, <stdio . h>", "
vector string, -".
,

,
,
,
.
.
,
.
,
. (..
,
), .

,
. .
++, . .
, .
.
, .

, .

25.6.2.

, . ,
, .
,
1060 25

35 . .
. ,

.
.
() .
,
, , .

.
.
.
R .
r.
.
.
.
.
.
.
.
, .

, . ,
.

(,
, "?),
. JSF++ (.
25.6.3) ;
JSF++.
; , -
.


R lOO. ( ) 200
( ).
. ( ) ,
.
rlO l . ( )
.
25.6. 1061

. , ,
. , ,
, , , .
Rl02. ++
ISO/IEC 1 4882:20 1 l (E).
. ISO/IEC 1 4882
, .


R200. ,
#ifdef #ifndef.
.
.
, .
R20 1 . #include
(* . h} .
. #include
, .
R202 . #include ,
.
. #include, , ,
, ,
-.
R203. (* . h}
, ,
.
.
, .
;
(
). .
.


R.
.
. .
RO l . .
. .

int = 7 ; = +7 ; f (x , 9) ; 11
int = 7 ; // OJ:.:
1062 25

= +7 ; 11
f (x , 9 ) ; 11

if (p<q) cout << * ; 11

if (p<q)
cout << * ; 11

R2. .

.
, , i, j
.
numer_of_elements,
numerOfElemen ts .
.
,
.
.
. Device_driver Buffer_pool.
. .
. . ,
++ , ,
.
.
.
R. ,
.
.
/ .
D.
1 1 l.
S 5.
Z 2 .
h.

Head head //

. .
25.6. 1063

R304.
.

BLUE BLUE CHEESE //

. , .
, r
, .
. , #include.

n
r400.
.

int var = 9 ; { int var = 7 ; ++var ; } // : va r va r

. .
R40 1 .
.
.
. ;
.
R402. .

int var ; // : va r

.
.
. .
, .
. , vector strinq,
, .
R403. .
. .
. dynamic_cast.
.
,
void*,
(, ),
.
R404. .
. , ,
.
Array_ref.
1064 25

.
, ,
. ,

.

n
R500. -
class. - -
struct. ,
-.
. .
r50 1 . ,
, .
. .
. .
,
.
.
R502. ,
.
. , , ,
. ,
,
,
.
r503 . , ,
explici t.
. .

n r
R800. .
. .
R80 1 . new
.
. .
. , ,
new ( ).
R802. delete .
. ;
.
R80. dynamic cast._
25.6. 1065

. ( ).
R804. .
std : : array.
. ( ).

,
R900.
.
:
int = v [++i] ; //

:
++i ;
int = v [i ] ; // OI\

. .
R90 1 .
.
11:
= *+ ; 11 OI\

:
if ( < 1 1 c<=d ) // : ( < (c<=d)

1m. :,
/++.
.
. r
. ,
.

25.6..
++.
.
. , , -
. . , .
.
.
1 . Google ++ Style Guide: http : / / google-styleguide . googlecode . com/ svn/
trunk/cppguide . xml . ,
.
2. Loc kheed Martin Corporation. Joint Strike Fighter Air Vehicle Codirlf} Stan
dards .for tfie Syslem Development and Demonstration Program. Document
1066 25

Number 2RDUOOOO 1 Rev . December 2005. r


':JSF++". ,
Lockheed-Martin Aero,
( "").
, ,
(www . stroustrup . com/ JSF-AV-rules . pdf).
3. Programming Research. High-integrity ++ Coding Standard Manual
Version 2.4. (www . programmingresearch . com) .
4. Sutter. Herb, and Andrei Alexandrescu. ++ Coding Standards: 1 0 1 Rules.
Guidelines, and Best Practices. Addison-Wesley. 2004. ISBN 032 1 1 1 3586.:1
" ";
, ,
. .

, .

. - -
-
/ .
++.
ISO .
(ISO/IEC TR 1 80 1 5; www . stroustrup . com/performanceR . pdf);
( ) . .
.

,
(. ISO ++). -
.
, .

1 . :
int v = 1 ;
for (int i = ; i<sizeof (v) * B ; ++i) {
cout << v << ' ' ; v <<=1 ;

2. , v
unsigned int.

3 : . . 11m
++ . - . : " .. ", 2005.
25.6. 1067

3. , , -
short unsigned int.
.
.
.
.
.
(
).
(
).
4.
.
5. 4, ( 1 , &, <<)
() 1 .


1 . ? ,
.
2. ?
.
. .
4.
?
5.
?
6.
?
7. ? ?
8. .
?
9. ?
1 0. ?
.
1 1 .
?
1068 25

1 2. ? .
1 3 . ++.
1 4. ?
1 5.
?
1 6. new
?
1 7. s td : : vector -
?
1 8.
?
1 9. ?
, , ?
?
20. ?
2 1 . ( )?
22. ? ?
23. ? .
2 4 . ?
25.
?
26. ?
27. ?
28. ?
29. reinterpret_cast? ?
30. ?
.
3 1 .
? .
32. ( )
.
33. " "?
34. ?
35. ?
36. ?
37. ?
25.6. 1069

38. "JUi " ?


39. ( ) ?
40. ?
4 1 . ?
42. ?
43. , , ?
44. Oxf7?
45. ?
46. JUiacc i tset ?
47. unsigned int signed int?
48. unsigned int,
signed int?
49. JUI, ?
50. unsigned int
-3?
5 1 . (
)?
52. ?
53. ?
54. ()? ?
55. ?
56. ?
57. ?
58. ? .
59. ?
60. .
6 1 . ?
62. ,
( ) . ?
63. a ALL_CAPITAL?

itset
unsigned

1070 25

1 . ,
.
2. ,
, , 1 L 2
to .. , Fool Beef .
.
3. 32-
:
. , (
- ), (
- ), 1 1 00 l 1 00 1 1 . . . , 00 1 1 00 1 1 00 . . . .
,
, , .
32- .
4. 7 &,
1 . " -.
5. . .
6. .
. h ,
. .
7. 400:
-200 200.
8. .
9. (
<limi ts>). , int
, char ++.
1 0 . 25.5.5.
. PPN,
.
( )
. ,
PPN 32-
(. 25.5.4)
.
25.6. 1071

11 . ,
i tset<32>.
1 2 . 25.5.6.
13. (. 25. 5.6) ""
.
.
1 4. . N
, .
N=lOOO .
1 5 . (. 26.6. 1 ).
1 (; 1 OOOJ
new; ,
delete.
:
. - .
1 500 ,
. 1 0
(; 1 OOOJ ,
( ). .
,
.
1 6. ,
( 25.6).
, 300 , .
( - )
. ?
? , ?
, .
1 7 . 25.4.3-25 .4.4 Array_ref,
. ,
, .
Rectangle*
vector<Circle*>. Array_ref<Shape*>,

. .

, , -
. " "? , -

.
1072 25

: , .
.
.
, ,
.



. ,
.
-

, .


.
,

.

, .
1074 26

26. 1 . 26.3.5 . ,
26.1 . 1 . .13;
26.2. 26.4.
26..

26.3. 1 . 26.5 .
26.3.2. 26.6.
26.3.3. 26.6. 1 .
26.3.4. 26.7.

26. 1 .
. I
. ,
. , i!
- .
.
, .
,
.
,
.

.
, .
()
"" (<) . ,
,
. ,
, - , ,
. ,
(inary_search equal_range),
.
.
, . ,
. ! - ,
? ,
. ?
?
26.1 . r 1075

,
.
200 .
- 1 50 .
. ,
,
500 2 .
? .
,
, ?
, , ,
(
).
,
.

.
, -
:
. . .
, ; .
. " ?
, ,
.
, , , , -
. ,
,
.
. , - , ,
,
. ,
" .
, . ,
- ( ,
) , .

. ,

. .
,

, . ,
"" , .
1076 26

26.1 . 1 .
- .
, ,
.
:
,

. ,
, .

,
.

26.2.
! ,
, ?
(Edsger Dij kstra), "
, ".
, .
,
(
), -
(
),
. ,
,
.
( 26.3) ,
( 26.4).

26..
5. 1 1 "
". .
11 (unit testlng)
(system testtng).
, .
, ,
; ,
, ( .
26.. 1077

).
, ,
. "- " . ,
. - .
, -
. ,
, .
, (, )
(, ). (,
) (,
. ,
).
. ,
,
.

, , .
. .
, , ,
. .
.
( ).
, (. .
, ).
() (
) (
).
;
. , -
, ,
. .

.
. - ( , )
, ,
. -
, , ,
.
.
- ( , ).
.
11 11
11 (regression testing) .
1078 26

, . ,
. ,
.

26.. 1 .
,
.
.
, ,
. !

. ,
. .
,
,
,
.
: ,
. .

,
. .
. , -
.
,
. .
( ) ,
,
,

.
.

,
.
, .

26..2.
! :
. ISO (
25.3.3.4):
26.3. 1079

teplate<class Forwarditerator , class >


bool binary_search (Forwarditerator first,
Forwarditerator las t , const & value ) ;

teplate<class Forwarditerator, class , class r>


bool binary_search (Forwarditerator first , Forwarditerator last,
const & value , r ) ;

. \first,last)
e<value ! (value<e) comp ( e , value)
! cornp (value , e) . , [first,
last) e<value ! (value<e) , comp ( e ,
value) ! comp (value , e) .
. true. [first,last)
i , ! ( * i<value) && ! (value< * i )
comp ( * i , value) ==false && comp (value , *i ) ==fal se.
. log ( last-first) +2 .
,
( , ) . .
,
,
, ,
. ()

(. 20. 1 0 . 1 )
true, ,
, .
. ()
<. inary_search,
.
.
,
, , ,
- .
11 : i n t - :
binary_search ( l , 4 , 5 ) ;

11 : :
vector<int> v ( lO) ;
inary_search ( v . egin ( ) , v . end ( ) , " 7 " ) ;

11 : :
inary_search (v .egin ( ) , v . end ( ) ) ;
1080 26

inary_search ( ) ?
,
,
-
! ,
.
( ).
( . -
).
.
.
. . ,
.
. , ,
inary_search , .
inary_search
, -
. , ,
( ) inary search,

, .
"". , -
, "
" . ,
(, ) .

26..2. 1 .
inary_search?
. .. .
, ,
(first,last) .
. ,
inary search,
(first,last).
last<first. ,
inary_ search , ,
. ,
,
.
inary_search, ,
, inary_search,
.
26.3. 1081

binary search
.
(. - ).
(. . ).
,
.
.
.
. ""
.
(. { 2 , 1 , 5 , -7 , 2 , 10 } ) .

(. binary_search ( & [ 1 0 0 ] ,
& [ 50 ] , 77 ) ).
( )
inary_ search (pl , 2 , v) ?
. .
( ) .
. , .
, .
.
1 , 2 , 3 , 5 , 8 , 13 , 2 1 11 " "
} 11
1 } 11
1,2,3,4 11
1,2,3,4,5 11
1, 1 , 1, 1, 1, 1 , 1 } //
0,1,1,1,1,1,1,1,1,1,1,1,1 11
,,,,,,,,,,,,,1 //

.
:
vector<int> vl ;
for (int i=O ; i<lOOOOOOOO ; ++i ) v . push_back ( i ) ;
.
( ) .
.
. . " "
. .
; .
.
.
1082 26

.
.
, " " .
.
.
. .
.
, .
.
.
?
.
. .
. .
, .
. . .
.
:
(
. ..):
(. ).

.

26.3.2.2.
: .
(, . ).
. (.
).
.
.
. .
inary_search.
vector<int> v { 1 , 2 , , 5 , 8 , 13 , 21 } ;
if (inary_search ( v . beqin ( ) , v . end ( ) , 1) false) ut<<" " ;
if (inary_search ( v . eqin ( ) , v . end ( ) , 5) false) ut<<" " ;
if (inary_search ( v . beqin ( ) , v . end ( ) , 8 ) -- false) ut<<"" ;
if (inary_search ( v . beqin ( ) , v . end ( ) , 21 ) == false) ut<<" " ;
if (inary_search ( v . beqin ( ) , v . end ( ) , -7 ) == true ) ut<<" " ;
if (binary_search ( v . beqin ( ) , v . end ( ) , 4 ) == true ) ut<<" " ;
if (inary_search ( v . eqin ( ) , v . end ( ) , 22 ) == true ) ut<<" " ;
26.. 1083

, .
- .

.
. . ,
- ,
. . .
" ".
, .
:
vector<int> v { 1 , 2 , 3 , 5 , 8 , 13 , 21 } ;
for (int : { 1 , 5 , 8 , 21 , -7 , 2 , 44 } )
if (binary_search ( v . egin ( ) , v . end ( ) , x) == false)
cout << << " : " ;
.

, , , .
, (
) : ,
, .
,
. , ,

. ,
,
,
.
?
() .
() .
( ).
, .
. ,
, :
{ 2 7 7 { 1 2 3 5 8 13 2 1 )
27. 7 { 1 , 2 ,
3 , 5 , 8 , 13 , 21 } , . (.. false).
, ?
.
. ,
. , ,
1084 26

, . ,
,
.
struct Test {
string lael ;
int val ;
vector<int> seq;
bool res ;
};

11 >> J :
istream& operator>> (istream& is , Test& t) ;

int test_all (istream& is)

int error count = ;


for (Test t ; is>>t ; )
bool r = binary_search ( t . seq . begin ( ) ,
t . seq . end ( ) , t . val) ;
if (r ! =t . res) {
cout << " : " << t . lael
<< " inary_search : "
<< t . seq. size ( ) << " , val=="
<< t . val << " -> " << t . res << ' \n ' ;
++error_count ;

return error_count ;

int main ( )

int errors = test_all (ifstream ( "y_tests . t.xt " ) ) ;


cout << " : " << errors << 11\n" ;

.
1.1 1 1 2 3 5 13 21 1
1.2 5 1 2 3 5 13 21 1
1.3 1 2 3 5 13 21 1
1.4 21 1 2 3 5 13 21 1
1.5 -7 1 2 3 5 13 21
1.6 4 1 2 3 5 8 13 21
1.7 22 1 2 3 5 8 13 21
2 1 } }
3.1 1 1 } 1
3.2 1 }
3.3 2 1 }
, , :
,
26.. 1085

.

.

26..2..
,
, (
), , .
(, ,
, . . ) . ,
. ,
. ,
, .
. ,
, - .
,
: . ,
. cout
randint ( ) 24. 7 std_
lib . facilities . h.
void .ake_test (const string& l. , int n , int base , int spread)
11 l a b co u t
11 ,
11 ba se;
11 [ , spread]

cout << { " << l << " " << n << "
" ";
vector<int> v ;
int elem = base ;
for (int i = ; i<n ; ++i ) { //
elem += randint ( spread) ;
v . push_back (elem) ;

int val = base + randint (elem-base) ; 11


bool found = false ;
for ( int i=O ; i<n ; ++i ) { //
if ( v [ i ] ==val) found = true ; /! va l
cout << v [ i ] << " " ;

cout << " } " << found << " } \n" ;

, , val
, inary_search.
, ,
!
1086 26

binary_search -
.
, -
, ,
", .
.
int no_of_tests = randint ( lOO) ; 11 5 0
for ( int i = ; i<no_of_tests ; ++i) {
string l = "rand_test_" ;
:make_test (la+to_string ( i) , 11 to s t ring 23 . 2
randint ( SOO ) , 11
, 11 ba se
randint ( SO ) ) ; 11 spread


,
, ,
. .. (. 5 . 2).
,
binary_search, ,

(,
binary_search .
).
(. 5).

26...
inary_search ( ) .
, .
.
.
( - ).
,
.

(,
, ).

- (. 5. 1 0). ,
- .
, ( )
.
26.. 1087

! ?
. .
? ,
. ,
. , .
,
.
.
,
.
.
. .
. (
) , .
. ( )
. .
. (. .
.) . .
.
,
, -. .
.. ? ,
,
. ,
.
. -
; .
. , .
.

. -
, .
. -
. ..
. .
. ? -
. I ? ,
?
.
. .
. . .
1088 26

.
( new delete), , . .
. (
inary_search ( ) ).
i f swi tch (
). .
.
.

26.3.3.1 .
.
int do_dependent (int , int& //
//
int val ;
cin>>val ;
vec [val ] += 10 ;
cout << ;
++ ;
return ;

do_dependent ( )
, .
,
cin, cout vec.
,
. , ,
. ,
. ,
.
.
do_dependen t ( ) ,



int, -

cin ( val) cin
cou t
vec, - vec [val ]

26.. 1089


int,
( )
cin (
)
cout (
)
vec ( vec [val] )
, vec (
vec [val ] ).
. , .

( ). - -
,
, : .
,
, , inary_
search ( ) . (
), ,
( ). do_dependent ( ) .
val
val , , .
, vec ,
(
). , ,
, , ,
,
"" (.. )
.
.

26.3.3.2.
.
void do resourcesl (int a , int , //
const char* s ) //
11
FILE* f = fopen ( s , "r" ) ; 11 ( )
int* = new int [a] ; //
if (<=) throw Bad_arg ( ) ; 11
int* q = new int [b] ; 11
delete [ ] ; // ,
1090 26

do resourcesl ( ) .
_

. . ..
.
.
s .
, . , <=
new .
, q. > .
, ,
.
,
( fopen ( ) -
). .
:
void do_resources2 ( int a , int , //
const char* s )

ifstream is ( s ) ; //
vector<int>vl (a) ; // ( )
if (<=) throw Bad_arq ( ) ; //
vector<int> v2 (b) ; //
11 ( )

,
. ,
, -
. r
RAII, 1 9.5.2.
,
,
. (.
), - (
). .
. .
FILE* do_resources ( int , int* , //
const char* s )
//

FILE* f = fopen ( s , "r" ) ;


delete ;
delete var ;
var = new int [27 ] ;
return f ;
26.3. 1091

, do resources ( ) ()
? ,
do resources ( ) ,
?
var (, ).

, , ,
. ?
/?
. ( . . . )

.
, ()
,
. -
, .
, - .

26....
. inary_
search ( ) . .
?
( )?
. .
int do_loop ( const vector<int>& v) //
//

int i ;
int sum;
while ( i<=vec . size ( ) ) sum+=v [i ] ;
return sum;

. ( ?) ,

swn.

.
,
,
, ,
.
1092 26

char buf [AX] ; //


char* read_line ( ) //
{
int i = ;
char ch ;
while ( cin . qet (ch) && ch ! = ' \n ' ) buf [ i++] ch ;
buf [i+l] = ;
return buf ;

. ! ( ?
read_line ( ) ?) . .
.
//
qets (buf) ; // buf
scanf ( 11 % s 11 , buf) ;
11 buf

gets ( ) scanf ( )
. ""
, -
. ++
gets ( )
.

26...4.
, , .
. - if swi tch
. .
.
?

?
:
void do_branchl (int , int ) //
// i f

i f (<) {
if (< )
cout << " .\n" ;
else
cout << " \n" ;

else if (> ) {
if (< )
cout << " :.\n" ;
else
26.3. 1093

cout << " \n " ;

,
, .
( ) ,
(.
). ,
( ) , :
(> && < ) (> && >= ) -
. , "
''.

if, .
. do_
branchl ( ) .
do_branchl (-1 , -1) ;
do_branchl (-1 , 1) ;
do_branchl ( 1 , -1 ) ;
do_branchl ( 1 , 1) ;
do_branchl (-1 , 0 ) ;
do_branchl ( 0 , -1 ) ;
do_branchl ( 1 , 0 ) ;
do_branchl ( , 1) ;
do_branchl ( , ) ;
, " ",
, , do_branchl ( )
< >.
,
.
swi tch
if.
void do_branchl (int , int ) //
// swi t ch

if (< && <)


switch () {
case 1 :
cout << "\n" ;
break ;
case 2 :
cout << "\n" ;
case :
cout << "\n " ;
1094 26

.
(. ) .
break,
==2 .
defaul t (,
if).
<. <.
.
, . .
, .
, .
. .
.
( ).
.
(. , ).
. if:
. ,
. ,
. :
" ?" ,
( ,
. ).
. : "
?" "
". .
. 1 /- - .

26..4.
-
. .
, ,
. .
.

. ,
,
( .
. ).
- . .
.
26.3. 1095

, - ,

.
,
,
.
. ,
.
, -
.
,
, , -
.
:
.
,
().
,
( , , )
.
.
, , , ,
. .
(
); .
,
.
,
.
, , -
, . .
,
,
. (- ,
).

26..5. .
inary_search ,
, . .

. ,
. (
1096 26

1I ).
(. 1I . . )
?
, - .
, . ,
().
,
(. 5 . 5). ,
. . ,
. . (
).
.
binary_search : .
l first, last)
(. 26.3.2.2).
, .
template<class Iter , class >
bool b2 (Iter first , Iter las t , const & value)
{
11 , { fi rs t , la s t )
1 1 :
if (last<first) throw Bad_sequence ( ) ;
11 :
if (2<=last- first )
for ( I ter = first+l ; p<last ; ++)
if ( *<* (-1 ) ) throw Not_ordered ( ) ;
11 , bin a ry_ sea rch :
return binary_search (firs t , last , value) ;

, inary_search
.
last < first
; . s td : : list
< ( .3.2). ,
,
( first.
last. - ).
,
. .
inary_search (
inary_search II ,
,
std : : find) .
26.4. 1097

?
inary_search 2 (,
inary_search ).
inary_search
.
teplate<class Iter , class >
11 :
bool binary_search ( I ter firs t , I ter las t , const & value)
{
if (_1J11) (
if (Iter : ) {
11 , [fi rs t , l a s t )
11 :
if (last<first) throw Bad_sequence ( ) ;

11 ,
11 :
if (first ! =last) {
Iter prev = first ;
for (Iter = ++first ; p ! =last ; ++ , ++ prev)
if ( *p<*prev) throw Not_ordered ( ) ;

11 bina ry_sea rch

_1J:
( ),
: . ,
, test_enaed.
I ter ,
.
.
(iterator traits) ++.

26.4.
, ,
. ,
. . .
.
"
" . ,
. -
1098 26

(
). .
.
26.3.2. 1 26.3.3
.
.
.
,
, .
.
(assertions)
. r
.
.
.

.

, (,
). ,
. ,
. , ,

, inary_sort. ,
,
.
(..
) ,
: ,
, .

26.5.
- ,
. , 5.
, .
,
, ,
. ,
.
, ,
26.6. 1099

- .

.

26.6.
, ,
. ,
. ,
.
; ,
.
,

(-
).
(
).
. ( ) "
"? .
.
. .
(..
25 , ) ,
, (
,
, "- ").
, "
?" : " ,
". ,
,

.
,
.
system_clock () ( 26.6. 1 ),
,
. ( )

.
.
1100 26



.

. ,
. (
atrix 24).
11 [ ]
doue row_swn (&trix<doule , 2> , int n) ;

,
, v [ n ] - n .
11 [ , )
doule row_accwn (&trix<doule , 2> , int n)
(
doule s = ;
for ( int i=O ; i<n ; ++i) s+=row_swn (, i ) ;
return s ;

11 :
vector<doule> v ;
for (int i = ; i<. dil ( ) ; ++i ) v . push_back ( row_accwn (, i+l) ) ;

,
.
, : ,
, ,
m. ? ,
; ,
,
, - ;

..
, , ,
row_ sum ( )
. ,
.
: "
!" , , , , (
) ,
.
?
26.6. 1101

, .
, .

for (int i=O ; i<s trlen ( s ) ; ++i ) { / * - s [i ) */}

s 20 .
, ,
. (
26.3.3) , ,
. ""
. .
(, ,
).
(,
,
,
,
- ).
( ).
" " . ,
" ", ,
,
.
,
,
. -
! - , ,
.
, ,
. ,

.

26.6. 1 .
, ? ,
? ,
, (
, ). ,
( ),
, .
1102 26

,
.

,
, , . ,
Unix
time, .
, , time, ,
. .

q++ .

,
time.
tie g++ .
.
.
.
,
, .
, , ,
.
,
? ,
, ?
clock ( )
,
do something ( )
_ .

#include <chrono>
#include <iostream>
using namespace std;

int main ( )
{
int n = 10000000 ; // do_ some th ing ()
auto tl = system_clock : : now ( ) ; //
for (int i = ; i<n ; i++ )
do_soething ( ) ; //
auto t2 = system_clock : : now ( ) ; //
cout<< 11m do_something ( ) 11 << n << 11 11
<< duration_cast<milliseconds> ( t2-tl) . count ( ) << 11\n11 ;
26.7. 1103

system clock - , system_clock : : now ( )


(time_point), .
time_point ( t2-tl),
(duration). auto,
duration time_point,
.


, .
dura tion,
, seconds , mi l l i seconds nanoseconds,
("")
duration_cast. -
duration_cast,
. count ( ) .
.

(" ", clock ticks)


duration, ,
.
system_clock
.
.
: ,
,
. " "?
1 /. ,
:
. ,
,
, ,
.
,
.

26.7.
1 . Stone, Debble, Caroline Jarrett, MarkWoodroffe, and Shailey Minocha.
User Inter:.face Design and Evaluation. Morgan Kaufmann, 2005. ISBN
1 20884364.
2. Whittaker, James . to Break Sojtare: Practical Guide to Testing.
Addison-Wesley. 2003. ISBN 032 1 1 94330.
1104 26

3

inary_search.
1 . Test 26.3.2.2.
2. 26.3.
{ 1 2 5 8 13 2 1 } // " "
{ }
{ 1
{ 1 2 4 } 11
{1 2 4 5 } 11
{ 1 1 1 1 1 1 1 11
{ 1 1 1 1 1 1 1 1 1 1 1 1 } 11
{ 1 11
3 . 26.3. 1 .3 .
(
?);
;
. 1 , 2, . . 9 (
. .

) .
4. , { Bohr
Darwin Einstein Lavoisier Newton Turing } .


1 . ,
, - ;
. - : 23 1 :
500 . .
2. ,
?
3. -
?
4. ?
5. ?
6. inary search ?
7. ,
?
8. ,
, ?
9. ?
1 0. . ?
26.7. 1105

1 1 . ,
?
1 2. ,
?
1 3 . ?
1 4. ?
1 5. , ?
1 6. , ?
1 7 . , ,
()?
1 8. / ?
1 9 . ?
20. ?
2 1 . ( ) ,
.

D
system_clock




D
1 . inary search 26. 1 ,
26.3. 1 .
2 . inary search
.
string .
3. . 1 inary search,
.
, -
.
4. ,
.
5. inary_search
()
.
1106 26

6. 7,
(
-) .
.
7. 20.6.
8.
1 2- 1 5. , Circle{ Point
{ 0 , l } , 15 } Circl e { Point { 0 , 1 } , 15 } .
"
": , .
9.
. , Circle { Point{ 0 , 1 } , 15 }
Circle { Point{ , 1 } , 15 } .
1 . . 9,
.
1 1 . 26.6,
m
- , 1 00, 1 0 000,
1 000 000 1 0 000 000.
[- 1 0, 1 0) . v,
( O(N2)) ,
.
1 2. ,
, std : : sort ( ) .
, 500 5
du.
13. ,
, [, 1 00).
1 4. ,
map, vector, .

,
- . :

, ( ) .
, , -
, ,
. ,
(
).

. -
u- -
JJ ?...L1U. (:J )



,
++ . ++,
, ,
.
+ + ,

.
- .

, ,
.
1108 27

2 7. 1 . + + : 27.3.5.
27. 1 . 1 . void*
+ + 27.3.6.
27. 1 .2. + + , 27.3.7.
2 7.4.
2 7. 1 .3. 2 7.5.
27.5 . 1 .
27.2. const
27.2. 1 . 27.5.2.
27.5.3. : strcpy ( )
27.2.2. 27.5.4.
2 7.6. -:
27.2.3. stdio . h
27.2.4. - 27.6. 1 .
+ + -
27.6.2.
27.2.5.
27.6.3.
27.3.
2 7.7.

2 7.8.
27.3. 1 .
struct 27.8. 1 . ,

27.3.2.
27.8.2.
27.3.3.
27.8.3.
27.3.4.
2 7.9. :

27 . 1 . ++:

(Dennis Ritchie) Bell Labs.
The Programming Language (Brian Kernighan)
(Dennis Ritcble) ( "K&R").
. ,
(. 22.2. 5).
++ ,
1 980 .
. ++,
ISO.
++.
, ++
.
,
++.
27 . 1 . ++: 1109

++, ,
,
.
++
"". "
", , -
h Programming Language,
.

1 967

1
1
1
1
1
1
1
1
1
1 :
1
1
:
1 978 1
1
1
1
1
:
1
\
\
\
1
1

1 980

1 985

1 989

1 998

20 1 1

20 1 4 ++ 1 4


89 ( K&R1).
. , -

1 : ., . .
2- . - . : " .. ", 2006. - . .
1110 27

- ,
99, ,
++ 89.
++ ""
Bell Labs (Computer Science Research Center of Bell
Labs). -, - (Murray Hill, New Jersey) (,

).


ISO.
.
,
. , .
++, .

. .
.
.
.
.
.
,
++, .
27 . 1 . ++: 1111

++, . ,

.

27 . 1 . 1 . ++
"/++".
.
. -

, .
++ , ,
.
, ++. (-
). ++ , "
++, , ":
:
.
, ,
++.
, , ++,
, ++
( 27.3.2) .
int class (int new, int l ) ; / * , ++ */
. , ,
, , .
int s = sizeof ( ' a ' ) ; / * si zeof (i n t ) , 4 1 ++ */

, ' ' . int char -

++. ch char
sizeof ( ch ) = = 1 .
, ,
.
, .
printf ( ) ( 27.6),
( )
. :
,
.
, ,
, , , ,
++. ,
.
111 2 27

, ++.
,
. . .
. ++.
. ,
, .
. :
. .
.

w ". :
(
). (
, ).

1 . ISO /IEC 9899: 1 999. Programming Langrges - .


99; 89 (
).
2. ISO /IEC 9899:20 1 1 . Progranuning Languages - . 1 1 .
3. ISO/IEC 1 4882:20 1 1 . Programming Languages - C++.
4. Kernighan, Brian W" and Dennis . Ritchie. h Programmirrg Laguage.
Addison-Wesley. 1 988. ISBN 0 1 3 1 1 03628. 2
5. Stroustrup, Bjarne. WLearning Standard ++ as New Language". /++
Users Joumal, 1 999.
6. Stroustrup, Bjarne. wc and ++: Silings"; wc and ++: Case for Compati
lity"; and " and ++: Case Studies in Compatility". The /++ Users
Joumal, July. Aug" and Sept. 2002.
7.
.

2 7 1 2 ++.
. . .

++ (.. 89)
.
-
.

2 : . , . ,
2- . - . : w .. ", 2006. - . .
27. 1 . ++: 111 3


.
( 27.2.3).

( 27.8).

, ,
, . .

.
new/delete
malloc ( ) /free ( )
.

.
const, constexpr

.
bool
int.
static_cast, reinterpret_cast const cas t
, . . ( int) static_
cast<int> (a) .
,
,
. -
- -
. ,
,
. ,
++
.
,
.
++ .
,
++, .
1114 27

,
, ++.

.
(. -
27.2.2).
, ++,
. .
, .
,
, .
, ,
- ,
++.
++
.
.
.
typedef ( ++98 using; .
20.5 . 1 6).
sizeof.
.

.
++ / /,
, BCPL, / * . . . * / .
/ / .
99, .
/ * . . . */
, , .
99 1 1
++ ( ,
++), 89,
.

27 1
. . .

, ++,
, .
.
27.2. 1115

vector.
map.
set.
string.
STL, sort ( ) , find ( ) ( ) .
- iostream.
regex.
- ,
.
.
<stdlib . h>. (, malloc ( ) free ( ) ) ; .
27.4.
<stdio . h>. -; . 27.6.
<string . h>. ; .
27.5.
<math . h>.
; . 24.8.
<errno . h>.
<math . h>; . 24.8.
<limi ts . h>. ; . 24.2.
<time . h>. ; . 26.6. 1 .
<assert . h>. ( ); . 27.9.
<ctype . h>. ; . 1 1 .6.
<stdool . h>. .

, K&R. (
) ++.

27 2
. .

.
.
.
( . ).
-.
( 99).
.
1116 27

, ++.
.

27 .2.1 .
:
void print (int) ; /* */
void print ( const char* ) ; / * */ / * ! */

, ,
, .
, .
void print int ( int) ; /* i n t */
void print_strinq (const char*) ; / * */
:
! ,
,
,
,
.

27 .2.2.
:
int main ( )
{
f ( 2) ;

:
( ).
f ( ) - . , f ( )
, ,
.
,
:
/* o ther fi l e . c : */
int f (char* )

int r = ;
while ( *++) r++ ;
return r ;

.
.
27.2. 111 7

?
.
, , -
,
#include,
. .

,
: . ,
,
, .
lit.
. , lint
++. .
++, ,
( ),
lint.
.
(
, ++).
(functlon prototype). ,
:
.
int q ( doule) ; /* - ++ */
int h ( ) ; / * - * /

void my_fct ( )
{
q() ; /* : * /
q ( "asdf" ) ; /* : */
q (2) ; /* : 2 2 . 0 */
q (2 , 3) ; /* : */

h() ; / * ! */
h ( "asdf" ) ; /* ! */
h (2 ) ; / * ! */
h (2 , 3) ; /* ! */

h ( ) .
, h ( ) :
, ,
. ,
. lint .
1118 27

+ +
void f ( ) ; // void f (void) ;
void f (void) ; void f (void) ;
void f ( ) ; //
. . . void f ( ) ; //

.
. .
, char short
int, float doue.
-

, , long.
. :
, .
. .
. char*
int, .
,
-

".

27 .2..
, ++.
.
doule square (doule d)
{
return d*d ;

void ff ( )

doue = square ( 2 ) ; /* : 2 2 . 0 */
doule = square ( ) ; /* */
doule = square ( " Hello" ) ; / * : */
doue = square (2 , 3 ) ; /* : */

.
void f ( ) / * - */ )
void q ( )
{
f (2 ) ; /* ; + + */

void f ( ) ; / * */
. f ( )
, .
27.2. 1119

, ""
void ( void -

, ""):
void f ( void) ; /* */
, .

. , (
) (Doug Mcllroy,
Bell Labs (Bell Labs Computer
Science Research Center: . 22. 2 . 5)) "
". ,
, .
++, .
.

Algol60, ( )
.
int old_style (p , b , x) char* ; char ;

/ * . . . */

" "
++ .
int. , -
old_style ( ) , int.
old_s tyle ( ) :
old_style ( ) ; /* : */
old_style ( "hello" , ' ' 17)
, ; /* : * /
old_style ( 12 , 13 , 14) ; /* : 1 2 , */
-

/* */

( ,
).

.
(
).
,
, .
(-) lint.
,
++.
1 1 20 27

27 . 2.4. - ++-
.
, .
++.
. . ,
++. GNU
GCC. ,
++. Microsoft ++ (MSC++).
,
, .
++ .
. ,
++ , -
f ( int) f (doue) .
. .
, , ,
++. , .
.
11 + + :

extern "" doule sqrt (doule) ; 11 -

void my_c_plus_plus_fct ( )
{
doule sr = sqrt ( 2 ) ;

, extern "" .
. . .
++ .
sqrt (doule) ++
.
. ++, .
++ ,
.
extern " " ,
++ . .
11 + + -, :
extern "" int call_f (S* , int i)

return p->f ( i) ;

- f ( ) .
27.2. 1 1 21

/ * ++ - : */
int call_f (S* , int i ) ;
struct S* ake_S (int, const char* ) ;

void my_c_fct (int i)


{
/ * . . . */
struct S* = make_S (x , "foo" ) ;
int = call_f (p , i ) ;
/* . . . */

++ ,
.
:
++. , ++
, .
, ++. ,
(, Fortran) ,
, ,
.
, ,
++, .
.
. , , :
11 + + :
class complex {
doue re, i;
puic :
11
};
,
, .
re im , .
/* : */
struct complex {
doue re , i;
/* */
);

,
., ,
. ,
++,
() .
,
11 22 27

. ++.
call_f ( ) : - f ( )
. ,
, .
,
struct,
++.
++.
.

2 7 .2.5.
, -
(. 1 4.2-1 4.4)? .
- .
, .
" " ("type field"), ,
. .
struct Shapel
enum Kind { circle , rectanqle } kind;
/* " . */
};

void draw (struct Shapel* )


{
switch (p->kind) {
case circle :
/ * */
break ;
case rectanqle :
/ * */
break ;

int f ( struct Shapel* )

draw (pp) ;
/* " . */

. .
"" (, draw ( ) )
swi tch.
, .
(, draw ( ) ).
case swi tch.
27.3. 1123

,
,
-
.
.
typedef void ( *PfctO) (struct Shape2 * ) ;
typedef void ( *Pfctlint) (struct Shape2 * , int) ;

struct Shape2 {
PfctO draw;
Pfctlint rotate ;
/ * . . . */
) ;

void draw (struct Shape2 * )


{
(p->draw) () ;

void rotate ( struct Shape2 * , int d)


{
(p->rotate) (p , d) ;

Shape2 , Shapel.
int f ( struct Shape2* )
{
draw (pp) ;
/* . . . */

. ,

.
( ,
++).
,
.

27 . .

++, ,
.
, .
1 1 24 27

27..1 . s truct
( class,
struct)
. , (
r (structure tag))
struct. .
struct pair int ,; };
pair pl ; /* : i r */
/* */
struct pair 2 ; / * */
int pair = 7 ; /* : i r */
/* */
struct pair ; / * : i r */
/* i n t */
pair = 8 ; /* : pa i r */
/* i n t */

, , ,
++. (
) , , -
. .v.
struct
. typedef (. 20. 5).
:
typedef struct { int ,; } pair ;
pair pl = { 1 , 2 } ;
typedef
,
.

, . .
.
struct S {
struct { / * . . . */ } ;
/ * . . . */
};

struct ; /* ( ++) */

++ :
S : : ; 11 + + ( )

,
:
( ) .
27.. 1 1 25

27 ..2.
++
(
)
.

++,
alignas class inline private true
alignof compl mutale protected try
and concept namespace puic typeid
and_eq const cast new reinterpret_cast typename
asm constexpr noexcept requires using
itand delete not static assert virtual
itor dynamic_cast not_eq static cast wchar t
bool explicit nullptr template xor
catch export operator this xor_eq
charl t false or thread local
char32 t friend or_eq throw


,
++. -
, ++.
++ .

++,
and itor false or wchar t
and_eq bool not or_eq xor
itand compl not_eq true xor_eq

<iso64 6 . h> <s tdool . h>


(bool, true, fal se). ,
.

27 ...
++
89. .
for (int i = ; i<max ; ++ i) 11 i ,
x [ i] = y [ i] ; 11
while (struct S* = next (q) ) 11 ,
/* . . . */ 11
1 1 26 27

void f ( int i )

i f ( i< 1 1 <=i) rrr ( " " ) ;


int [] ; // :
//
/* . . . */

(89)
for, .
:
int i ;
for ( i = ; i<max ; ++i) x [i ] = y [i ] ;

struct S* ;
while ( = next (q) )
/ * " . */

void f ( int i)

if (i< 1 1 <=i) rrr ( " " ) ;

int [] ;
/ * " . */

++
; ,
.
int ;
int ; / *
; ++ */

++ .
, int
.
/ * . : * /
int ;

/ * . : */
int ;
, ++
. or . . . .
++. ,
. . .
.
27.. 11 27

( ) ,
, .
. . ,
, ,
.
/ * . : */
int = ; / * */

/ * . : */
extern int ; /* , */

, .
/* x . h : */
extern int ; / * , */

/ * . : */
#include "x . h "
int = ; / * * /

/ * . : * /
#include "x . h "
/ * * /

27 ...
( ++) v ,
.
(T) v
" ", "
". ,
( ) (
, , v
). , ,
,

. ++ ( (new-style
casts). (template-style casts); .
.5. 7) , .
.
int* = (int* ) 7 ; / * :
rein terpret_ca s t <in t *> ( 7 ) */
int = (int) 7 . 5 ; / * : s t a t i c_ ca s t<i n t > ( 7 . 5) */
1 1 28 27

typedef struct Sl /* */ Sl;


typedef struct S2 /* */ 82 ;
Sl ;
const S2 ; /*
*/

Sl* = ( S2 * ) &a ; /* :
rein terpre t_ ca s t <Sl *> ( & ) * /
S2* q = ( S2 * ) & ; / * con s t :
con s t ca s t <S2 *> (&) */
Sl* r = (Sl*) & ; / * con s t ;
, */


( 27.8), , ,
:
#define REINERPRET_CAST ( T , v) ( () (v) )
#define CONST_CAST ( T , v) ( () (v) )

Sl* = REINERPRET_CAST (Sl * , &a) ;


S2* q = CONST_CAST (S2* , &) ;
reinterpret_
cast const_cast,
.

2 7..5. void*
void*
.
; ++ . .
void* alloc ( size_t ) ; /* */

void f (int n)
{
int* = alloc (n*sizeof (int) ) ; / * ;
+ + */
/* " . */

void* alloc ( )
int*. ++
:
int* = (int* ) alloc (n*sizeof (int ) ) ; /* , ++ */
( 27.3.4).
,
++.
27.. 11 29

void* *
++? ,
.
void f ( )
(
char i = ;
char j = ;
char* = &i ;
void* q = ;
int* = q ; / * ; , + + */
* = -1 ; /* , &i */

,
. , j ? .
, f ( ) (
f)? , f ( )
.
, ()
* void* . -
, , -
, ++.
, void* *
, ,
++ ( . 27.4).

27..6.

in t enwn:
enum color { red, u , green } ;
int = green ; / * ++ */
enum color col = 7 ; / * Of\ ; ++ */

,
(++) (--)
, . ,
.
enum color = u ;
++ ; /* green ; ++ */
++ ; /* ; + + */

,
.
, , ,
.
1 1 30 27


enwn.
color 2 = u ; / * : color
; ++ */
enum color = red; / * */

27.3.7.
( ,
++). ,
, ? ,
.
/ * bs . h : */
typedef struct bs_string
{ / * . . . */ } bs_string ; / * */
typedef int bs_bool /* */

/* pe t e . h : */
typedef char pete_string ; /* */
typedef char pete_bool ; /* */

, -
: .

27 4
. .

new delete, .
,
.
<s tdlib . h>.
void* m.alloc (size_t s z ) ; / * s z */
void free (void* ) ; / * ,
*/
void* calloc (size_t n , / * n *sz ,
size t s z ) ; */
void* realloc (void* , / * ,
size t s z ) ; ,
sz */

size_t - , typedef
<s tdlib . h>.
malloc ( ) void?
,
. - . -
.
27 .4. 1 1 31

struct Pair {
const char* ;
int val ;
1;

struct Pair 2 = { "apple " , 78 } ;


/ * : */
struct Pair* (struct Pair*) malloc ( sizeof ( Pair) ) ;

-> = "pear" ; / * */
pp->val = 4 2 ;

* = ( "pear" , 4 2 } ; / * : ++98 */
, ++.
++ Pair
Pair* = new Pair ( "pear" , 42) ;
( ++; . 27.3.4)
malloc ( ) ,
.
int* = malloc (sizeof ( int) *n) ; / * */
,
,

<stdlib . h> malloc ( ) .
, ,
.
= malloc ( sizeof (char) *m) ; / * , -
i n t */

malloc ( ) /free ( ) ,
++; new/delete
, ( )
( ), ,
( ), .
, malloc ( ) ,
delete, ,
new, free ( ) :
int* = new int [200 ] ;
// . . .
free (p) ; 11

* q = (X* ) malloc (n*sizeof (X) ) ;


11 . . .
delete q; 1 1
1 1 32 27

.
. , ,
, ++
.
realloc ( ) .
int = 1000 ;
int count = ;
int ;
char* = (char*) alloc (max) ;
while ( ( c=qetchar ( ) ) ! =EOF) { / * */
if (count==max-1 ) { / * */
+= ; /* */
= (char*) realloc (p , ax) ;
if (=-0 ) quit ( ) ;

p [ count++] = ;

27.6.2 . 1 1 .2 .
realloc ( ) ,
.
realloc ( ) ,
new.
++, ()
:
vector<char> buf ;
char ;
while (cin . qet (c) ) buf . push_back (c) ;

"Leaming Standard ++ as New Language" (.
27. 1 ) .

27 5
. .

( , ++.
- ( C-string) ( C-style)) -
, :
char* "asdf" ;
char s [ ] = "asdf " ;

: -9_ _."----la ls ld 11 I 1
s: l a l s l'd' 1 1 1
27. 5. 1133

-,
(, ) .
=


( ) .
++ <string . h>.
size_t strlen (const char* s ) ; /* */
char* strcat ( char* sl , / * s2 s l */
const char* s2) ;
int strcmp (const char* sl , / * */
const char* s2) ; / * */
char* strcpy (char* sl , /* s2 s l */
const char* s2) ;
char* strchr (const char *s , / * s */
int ) ;
char* strstr ( const char *sl , /* s2 sl */
const char *s2) ;
char* strncpy (char* , / * (<= ) */
const char* , size t n) ;
char* strncat ( char* , / * s trca t (<= ) */
const char , size_t n) ;
int strncmp (const char* , / * s trcmp (<= ) */
const char* , size_t n) ;
,
.
.
. ( ) =

;
strcmp ( ) -.
const char* sl = "asd.f" ;
const char* s2 = 11asdf11 ;
if ( sl==s2 ) { / * s l s2 */
/ * ? ( ) */

if ( strcmp (s l , s2 ) ==0 ) ( / * sl s2 */
/ * ? */

strcmp ( ) .
sl s2 strcmp ( sl , s2 ) ,
. sl s2
, , sl
s2 ,
. . (lexicographical) "
." .
11 34 27

strcp ( "doq" , "doq " ) ==O


strcp ( " ape" , "dodo " ) <O / * " " "dodo " */
strcp ( "piq" , "cow" ) > / * "pi g " "cow" */
sl=s2 (false).

,
1 (true). strcmp ( )
-.
- strlen ( ) :
int lqt = strlen ( sl) ;
, strlen ( )
.
strlen ( s l ) =4, "asdf" .
.
- ( )
.
strcpy ( s l , s2) ; / * s2 sl */

, ()
,
.
s trncpy ( ) , s trnca t ( ) s trncmp ( )
s trcpy ( ) , s trca t ( ) trcmp ( ) , n ,
n . ,
n , strncpy ( )

-.
strchr ( ) strstr () ,

(
). find ( ) , .
,
.
: .
@ . std : : string
:
strinq s = id + ' @ ' + addr ;
-
:
char* cat (const char* id, const char* addr)
{
int sz = strlen ( id) +strlen (addr) +2 ;
27.5. 1135

char* res = (char*) malloc ( s z) ;


strcpy (res , id) ;
res [ strlen (id) +l] = ' @ ' ;
strcpy (res+strlen (id) +2 , addr) ;
res [sz-1 ] =0 ;
return res ;

? free () .
cat ( ) ?


cat ( ) .
2? cat ( ) ,
.
. "" .
, ,
-.

27.5.1 . cons t
:
char* = "asdf" ;
[2 ] = ' ' ;
, ++ - . ++
, ..
, [ 2 ] = ' ' (
" asxf") . ,
.
. ,
, .
:
const char* = " asdf" ; //
11 "a sdf"

, ++.
s trchr ( ) ,
. .
/ * s ( + +) : */
char* strchr (const char* s , int ) ;

const char [ ] = "asdf" ; / * - */


char* q = strchr (aa , ' d ' ) ; / * ' d ' */
*q = ' ' ; / * ' d ' ' ' */
11 36 27

, ++,
.
(transmutation):
. .
++
s trchr ( ) .
// s
char const* strchr (const char* s , int ) ;

// s
char* strchr (char* s , int ) ;
strstr ( ) .

27 .5.2.
(- 1 980- ),
void* , , ++,
.

void* void*,
.
/* s2 sl ( s trcpy) : */
void* memcpy (void* s l , const void* s2 , size_t n) ;
/* s2 sl */
/ * [ s l , sl +n ) [s2, s2+n) ) : */
void* meJDOve (void* sl , const void* s2 , size_t n) ;
/ * s2 sl ( s t rcmp) : */
int memcp ( const void* sl , const void* s2 , size_t n) ;

/ * ( unsigned cha r) */
/ * s : */
void* memchr (const void* s , int , size_t n) ;
/ * ( un s i gned cha r) */
/* , s : */
void* memset (void* s , int , size_t n) ;
++. ,
memset ( ) ,
.

27. 5.. : s trcpy ( )


strcpy ( )
, ( ++):
27 .5. 1 1 37

char* strcpy (char* , const char* q)


{
while ( *++ = *q++) ;
return ;

, - q -
, .

&
strcpy ( ) ?
, .

, .
, (
).
, .

27 5 4
. . .


. , , .
. :
char* ; 11 - char

, :
char * ; /* - , , */

,
. (
++) ,
( )
.
.
char , * , [ 177 ] , *f ( ) ; / * , */
.
,
.
char = i t r
/* f () */
char* = ; /* , f () */
char [177] ; /* */
char* f ( ) ; /* ; */
/* */

, .
1 1 38 27

2 7 . 6 . -: s tdio . h
- iostream,
- ,
<stdio . h>. cin cout
++ s tdin stdout.
- iostream
(
-), .
,
( ios_base : : sync_with_stdio ( ) ) .

. (. . 1 1 .)

2 7 .6. 1 .
stdio
printf ( ) . printf ( )
-.
iinclude<stdio . h>
void f ( const char* )
{
printf ( " Hello , World! \n" ) ;
printf (p) ;

. , printf ( )

, , .
prin tf ( ) :
int printf (const char* format, . . . ) ;
( . ) ", , ".
. .

printf ( ) , :
,

void fl (doue d , char* , int i , char ch)


{
printf ( "doule %q strinq % int %d char %c\n" , d, , i , ch) ;

%g " ,
", %s " -
", %d " ,
," % " ".

, %g
d; %s s, %d
- i,
-
27 .6. -: stdio.h 1 1 39

% - ch. printf ( )
. 1 1 .2 .
, printf ( )
. .
char [ ] = { ' ' , ' ' } ; / * */
void f2 (char* s , int i )

printf ( "qoof % s\n" , i ) ; /* */


printf ( "qoof %d: %s\n " , i ) ; /* */
printf ( "qoof % s\n" , ) ; / * */

printf ( ) :
[ ] ,
[ 1 ] .
,

.
,
iostream,
-. stdio ++,
. ,
stdio :
printf ( ) ,
. iostream.
, ,
% s truct .
printf ( ) ,

.
int fprintf (FILE* stream, const char* format , . . . ) ;
.
fprintf (stdout , " Hello , World ! \n" ) ; 11 prin t f ( "Hel l o , World ! \ " ) ;
FILE* ff = fopen ( "y_file" , "w" ) ; 11 My_file
fprintf (ff , "Hello , World ! \n" ) ; 11 "He l l o , World ! \ n " My_ fi l e

27.6.3.

27 6 2
. . .

s tdio
.
int scanf (const char* format , . . . ) ; / *
s tdin */
int qetchar (void) ; / * s tdin */
int qetc (FILE* stream) ; /* s t ream */
char* qets (char* s ) ; /* s tdin */
1140 27

-
gets ( ) :
char [ 12] ;
gets (a) ; / * ' \ ' */
! , gets ()
. "" -
scanf ( 11 % s 11 ) - gets ( ) .. JI.
. ,
. ,
, ,
1 1 ? . ,
gets ( ) (
, ). ..
JI. . ,
. . ,
"" - .
.
scanf ( ) ,
printf ( ) . printf ( ) ,
.
void f ( )
(
int i ;
char ;
doue d ;
char* s = (char*) alloc ( lOO) ;
/ * , : */
scanf ( " %i % %q %s " , &i , & , &d , ) ;
/ * %s
*/

printf ( ) scanf ( ) ..
.

. ( .
) .
.
. ,
%s .
gets ( ) scanf ( 11 % s 11 ) !
?
% s ,
:
char buf [20] ;
scanf ( " % 19s" , buf) ;
27.6. -: stdio.h 1141

, (
scanf ( ) ) . 1 9 -
, buf.
, , 1 9 .

.
scanf ( ) ,
getchar ( ) .
getchar ( ) :
while ( (x=qetchar () ) ! =EOF)
/ * " . */

EOF, stdio . h, " "; .


27.4.
scanf ( 11 %s 11 ) gets ( )
++ :
strinq s ;
cin >> s ; //
qetline (cin , s) ; //

27 .6..
( ++)
fopen ( ) , - fclose ( ) . ,
FILE EOF ( ).
<stdio . h>.
FILE *fopen (const char* filename , const char* m.ode) ;
int fclose (FILE stream) ;
:
void f ( const char* fn , const char* fn2)
{
FILE* fi fopen (fn , "r ) ; / * fn */

FILE* fo " fopen (fn2 , "w" ) ; / * fn */

if (fi = 0 ) rrr ( " 1111 11 ) ;


if (fo == 0 ) rrr ( " s ll " ) ;

/ *
s tdi o , ge t c () */
/*
s tdi o , fprin t f () */

fclose (fo) ;
fclose (fi) ;
1142 27

: .
.

27. 7.
const .
const int 30 ;
const int ; / * : */
/ * ( + +) */

void f ( int v)
{
int al [max] ; / * : */
/* ( ++) . ( */
/* ! ) */
int 2 [] ; /* : */
/* */

switch (v)
case 1 :
/* . . */
.

break ;
case : / * : ca s e */
/ * ( + +) */
/* . . . */
break ;

, ( ++) const
:
/ * . : */
const int ; / * */

/ * . : */
const int = 7; /* */

++ .
.
cons t
:
#define 30

void f (int v)
{
int l [) ; / * */

switch (v)
case 1 :
27.8. 11 43

/* */
break ;
case : /* OI\ */
/ * . . . */
break ;

30,
; ,
al 3 0 , case
30.
. .
.

27 8
. .

: -
,
,
,
++. - . (.
. 1 7.2.)
, ,
( ,
++?)
,
: ALL CAPS. -

, ,
, .
"" ,
max min.
,
.
:
"";
"" ;
;
.
, ,
.
1 1 44 27

, ,
.
++ (
,
).
, , ,
, " ", ,
. ,
( )

. .

27 8 1 ,
. . .

, .
lldefine ( , ) ( () >= () ? () : () )

,

max ( ). ,
: ,
, return , ,
? :
int = ( l , 2 ) ;
doue dd = (++ , 2) ;
char = AX (dd, aa) +2 ;
:
int = ( ( 1 ) >= ( 2 ) ? ( 1 ) : (2 ) ) ;
doule dd = ( (++) >= ( 2 ) ? ( ++) : ( 2 ) ) ;
char = ( (dd) >= () ? (dd) : () ) +2 ;
,
.
char = dd>=aa?dd : aa+2 ;
,
, , .
, -
, .
,
. ++,
.
.
, .
27.8. 1 1 45

- ""
. ,
max, , . ++

template<class > inline max ( T , { return <? : ; }
max .

template<class > inline ( ( ) >= ( ? ( ) : ( )


{ return <? : ; }
. , "
". .
:
#undef max
, .

: , .
.
:
#define ALLOC ( T , n) ( (T*) malloc ( sizeof (T) *n) )
.
, -
sizeof.
doue* = malloc (sizeof (int) *lO) ; / * */

, ,
. - . .
error_var
error ( ) .
#define ALLOC ( T , n) (error_var = (T*) malloc ( sizeof (T) *n) , \
(error_var==O) \
? ( error ( 11 11 ) , ) \
: error_var)
, \. ;
.
++, new.

27 .8.2.
,
. .
1146 27

#define forever for ( ; ; )


#define CASE break ; case
#define eqin {
#define end }
.
. ( ,
) . .
,
.
:
.

.
, ,
, ,
, , .
,
.
, ,
, -
( ).

27 8
. . .

, ,
- Linux, -
Windows.
? :
#ifdef WINDOWS
#include "y_windows_header . h"
#else
#include "y_linux_header . h"
#endif
, - WINDOWS ,
, :
#include "y_windows_header . h"
.
#include "y_linux_header . h"
#ifdef WINDOWS ,
WINDOWS; , .
27. 9. : 1147

(
) , . .
. :
++ :
#ifdef cplusplus
11 ++
#else
/* */
#endif
. 11
(include guard).
.
/* my_ windows_h eader . h : */
#ifndef WINDOWS HEADER
#define WINDOWS HEADER
/* */
#endif
#ifndef ,
; .. #ifndef #ifdef.
.
. ,
.
.

27.9. :
++. vector
map, ; .
. .
( . ).
.
uu. (intrusive container),
-

, ++.
.
. .
.
void init (struct List* lst) ; /* l s t */
struct List* create ( ) ; /* */
/* */
void clear ( struct List* lst) ; /* l s t */
void destroy (struct List* lst) ; /* l s t , */
/* l s t */
1 1 48 27

void push_back ( s truct List* lst, /* */


struct Link* ) ; /* l s t */
void push_front (struct List* , /* */
struct Link* ) ; /* l s t */

void insert ( struct List* lst, / * q */


struct Link* , / * */
struct Link* q) ; / * l s t */
struct Link* erase (
struct List* lst , / * */
struct Link* ) ; / * l s t */

struct Link* advance ( /* , - */


struct Link* , /* */
int n) ; / * */

, ,
List*
Link * . ,
, . ,
STL. List Link
.
struct List {
struct Link* firs t ;
struct Link* las t ;
};

struct Link { /* */
struct Link* pre ;
struct Link* suc ;
};

List:
List :
flrst
last

Link :
,.-----<-----
pre pre
suc suc


, .
, ,
( ).
- , , -
Link List.
27.9. : 1 1 49

. Link* List*
(opaque types); . . , Link*
List* ,
List, Link
Li st.
r List
.
#include<stdio . h>
#include<std.lib . h>
#include<assert . h>
,
using. ,
(Link, insert,
ini t ..).
.
,
assert ( ) .
void init (struct List* lst) / * *l s t */
{ / * */
assert (lst) ;
lst->first = lst->last = ;

.
.
assert ( ) ,
,
. w " .
assert ( ) ,
. asert ( ) - ,
<assert . h>,
. ,
.
create ( ) Lit .
( ini t ( )
) new ( malloc ( ) ).
struct List* create ( ) / * * /
{
struct List* lst = (struct List* ) malloc ( sizeof ( struct List) ) ;
init (lst) ;
return lst ;
11 50 27

clear ( ) ,
,
free ( ) .
void clear ( s truct List* lst) / * l s t */
{
assert (lst) ;
{
struct Link* curr = lst->first ;
while (curr) {
struct Link* next = curr->suc ;
free (curr) ;
curr = next ;

lst->first = lst->last = ;

, ,
suc Link.
free ( ) ,
next,
Li st, Link
free ( ) .
Link
, clear ( ) ,
.
destroy ( ) , , create ( ) .
.. delete.
void destroy (struct List* lst) / * l s t ; */
{ /* l s t */
assert ( lst) ;
clear ( lst) ;
free (lst) ;

,
() ,
.
++, . ,
, .
push_back ( ) - Link -
.
void push_back ( struct List* lst, / * */
struct Link* ) /* l s t */

assert (lst) ;
{
27. 9. : 11 51

struct Link* last = lst->last ;


if ( last) {
last->suc = ; / * l a s t * /
p->pre = las t ;

else {
lst->first ; / * - */
p->pre = ;

lst->last = ; / * - */
p->suc = ; / * */

,
. ,
, .
, .
,
. -
( ).
erase ( ) :
struct Link* erase ( s truct List* l s t , struct Link* )
/*
l s t ;
,
*/
{
assert ( lst) ;
if (==) return ; / * erase (O) */

if ( ==lst->first)
if (p->suc) {
lst->first = p->suc ; / * */
p->suc->pre = ;
return p->suc ;

else {
lst->first
lst->last ; / * */
return ;

else if ( == lst->last) {
if (p->pre) {
lst->last = p->pre ; / * */
p->pre->suc = ; / * */

else {
lst->first
1152 27

lst->last = ; / * */
return ;

else {
p->suc->pre = p->pre ;
p->pre->suc = p->suc ;
return p->suc ;

,
( ) .
:
? ,
-? :
struct {
s truct Link lnk ; / * Li s t */
char* n ; / * */
};

, ,
Link? , List
Link , ,
Name .
struct * make_name ( char* n )

struct * = ( s truct Hame* ) malloc ( sizeof ( struct ) ) ;


p->n = n ;
return ;

.
List :
first
last

Link :
--L-----,
pre pre pre
suc suc suc
n n n

.
int main ( )
{
int count ;
27.9. : 1153

struct List n8Jll& s ; / * */


struct List* curr ;
init ( &names ) ;

/* Name : */
push_back ( &names , ( s truct Link* ) k_n ( "Norah " ) ) ;
push_back ( &names , ( s truct Link* ) ake_name ( "Annearie " ) ) ;
push_back ( &nass , ( s truct Link*) ake_name ( "ris " ) ) ;
/ * ( 1 ) : */
erase ( &names , advance (names . first , l ) ) ;
curr = names . first ; / * * /
for ( ; curr ! =O ; curr=curr->suc) (
count++ ;
printf ( "element % d : %s\n" , count ,
( ( struct Name* ) curr) ->n ) ;

-, "". ,
Name* Link * .
. " " Link. ""
" " Name. ? . :
( ++) struct
. .
, -
++.


, ++.
, , :
", . !"
List ++. ,
.


1 . "Hello Worldl" ,
.
2. . "Hello" "Worldl"
;
Hello World ! .
3. , char*
int,
: is " foo" and i s 7.
.
11 54 27

wr1

.
ISO 89.
1 . ++ ?
2. ?
3. .
4. ++?
5. ++ () ?
6. ++ ?
7. ++,
.
8. "" ++?
9. ++,
.
10.
++?
1 1 . ?
1 2 . ++, ,
? . .
1 3 . , , ,
++?
1 4 . , ++, ,
?
1 5. ++
? .
1 6. ?
1 7 . ++, -
.
18. int ; ++? ?
1 9. ?
20. void*
++?
2 1 . ++?
22. , ,
?
27.9. : 1155

23.
.
24. -?
25. = s trcmp ( ) -?
26. -?
27. -?
28. int?
29. printf ( ) .
30. gets ( ) ?
?
3 1 . ?
32. (const)
++?
33. ?
34. ?
35. w "?

#define malloc ( )
#ifdef printf ( )
#ifndef scanf ( )
Bell Labs strcpy ( )
++ void
- void*
FILE
fopen ( )
K&R m


.
++. ++,
, .
, , ,

1 . strlen ( ) , strcmp ( ) s trcpy ( ) .


2. List 27.9
.
1156 27

3. Li st
27.9 .
.
, ..
4. List
27.9 ++,
.
5. . 3 4.
6. Link List 27.9
,
. first, last,
pre suc int ( ).
7.

++ . "" ""
.
8. ?

; ,
.
9 . ,
, stdin
stdout . :
qsort ( ) ; .

. .
1 . . ++
( 27. 1 ).
1 1 . , ++.
1 2. (-. int) find ( struct
t* , const char* ) , insert ( s truct t* , const char* , int) remo
ve ( s truct tale* , const char* ) .
( const char* [ ]
int*); .
. .
1 3. ,
string s ; cin>>s ; . .
.
, ,
, ;
27.9. : 11 5 7

14. ,
int
.
. .
.
1 5. . "
"
( ,
" "
) (. 27.2.3).
,
.
.
Shape. draw ( )

. ,
.
1 6.
.

.
.
( ). -, - -.
.
. ++
, . ,
Lis t ,
-

( , ++).
4 \ 'J
\!\
\l p \!\ J\)\(

----
---
- ----

----
---
"
-
-
- -
----
--

'"""" -

rr ... _ ,
_____ -

--

-
- tUA..
.//&

++ .

,
, . -
, .
1162

. 1 . .8.
.1 . 1 . r .8.1 .
.1.2. .8.2.
r .8.3.
.1 .3. .9.
.2. .9. 1 . r
.2. 1 . .9.2. r
.2 .2. .9.3. r
.2.3. .9.4.
.2.4. . 1 0 . ,
.2.5.
.2.6. . 10. 1 . r
.. . 11 .
.3. 1 . . 1 2 .
.4. , . 1 2. 1 .
. 1 2.2.
.4.1 . . 1 2.3. , -
.4.2.
.4.3. . 1 2 .4.
.5 . . 1 2.5.
.5 . 1 . , . 1 2.6.
. 1 3.
.5.2. . 1 3. 1 . r
.5.3. .1 3.2.
.5.4. sizeof . 1 3.3. -
.5 .5. r -
.5.6. new delete . 1 4 .
.5.7. . 1 5 .
.6.
. 1 6.
.7.
. 1 7.
.7. 1 .
.1 7. 1 . #include
. 1 7.2. #define

. 1 .
.
, .
( ) ++.
, . .
. .
, ,
.
. 1 . 11 63

.
.
The ++ Prograing
Langage. ++ ISO ++,
. ,
. ,
J . ,
, . ,
.
.
++ ,
ISO (Intemational Organization for Standardization -
)
, INCIS (ClllA), BSI ()
AFNOR (). ISO/IEC
1 4882 :20 1 1 Standard.for Programing Langage ++.

. 1 . 1 .
++ , ++
.
. , ++
,
(conforming), .
. (
) (, int
' ' ) ,
. ,
.. , ,

, , <limi ts>
(. . 1 . 1 ). ,
++.
.
(unspecified), (undefined)
, (not confor
ming but not requiiing diagnostic) . ,
. .
, .
(
: . 8.3).
1 1 64 1


( a [ i ] =++i ; ) .
().
reinterpret_cast.

. 1 .2 .
++
main ( ) .
. , main ( ) int ( .

void ). ,
main ( ) "".
,

,
, -
, (.
) .
main ( ) ,
(
).
int main ( ) ; //
int main ( int arqc , char* arqv [ ] ) ; 11 a rgv [ } a rgc -

main ( )
. , , .
++:
int main () {}

(
) . ,
, main ( ) ,
- main ( ) (
main ( ) ,
- main ( ) ).
,
.

.1 ..
, .
. ++ ,
, .
//
/*

.2. 11 &5


*/

,
,
.
11 ,
11
/ * , */


(. 7.6.4).

.2.
. , 12
". "Morning" -

Moming. true true.


-

.2 . 1 .
(integer literals) .
: .
: , 1, 2, 3, 4, 5, 6, 7, 8 9.
: .
.
: . 1, 2, 3, 4, 5, 6 7 .
:
, .
: . 1, 2, 3, 4, 5, 6, 7, 8, 9, , , , d, , f, .
, , D, F.
: .
(++ 1 4).
: , 1 .
u u , . ..
unsigned (. 25. 5.3), 1 L
long, lOu 123456UL.
++ 1 4
. , ' 0001 ' 0010 ' 0011
100100011, 1 ' ' 1000000.
11 66

.2. 1 . 1 .
. 123
1 2 3 1 * 100+2 * 1 0+3* 1 . (
" ) 1 * 1 0 "2+2 * 10"1+3* 10"0.
"
" (base- 1 0). 1 0 .
1 *base"2+2 *base"1+3*base"O base==lO .
, ,
. :
, , , l 2,
,
digit. Digit .
, .
,
, .. 2
( 1
). ,
,
( 8),
(
1 6).
.
1 5.
. 1 , 2 , 3, 4 , 5, 6, 7 , 8 , 9, , . . D, , F,
1 , 1 1 :
-

-10 , == l l , ==12 , D==l , ==14 , F==lS .


123 7
. , ,
7 7 * 1 6+11,
123. .
123 1 * 1 6"2+2 * 1 6+3, .. 1 *256+2 * 1 6+3,
2 9 1 .
,

. ,
.

1 2 3 4 5 7
0000 0001 0010 0011 0100 0101 0110 0111

8 9 D F
1000 1001 1010 1011 1 100 1101 1 1 10 1111
.2. 1167

. ,
.
++ ( ) ,
. ,
, ( hex),
123==07 123=2 91.
, 123==07 123=2 9 1 .
, , , d, f
. , 123=07.
, .
: , 1, 2,
3, 4, 5, 6, 7 . ++
, 0123 - 123,
1*8"2+2 *8+3, . . 1* 64+2 *8+3 ( ) 83. ,
83, .. 083, 8 * 8+3, .. 67.
++, 0123=83
083==67 .
, .
: 1 . ++
.
- ++
, .
,
. , 123 1 * 2 " 6+1 *2"5+1*2"4+1*
2"3+0*2"2+1*2+1, .. 1* 64+1 *32+ 1 * 1 6+1*8+0 *4+1*2+1, .. ( )
1111011.

.2.2.
rn (floating-point-literal)
( . ), (, 3) ,
(d f). .
123 11 i n t ( ,
11 )
123 . 11 dou e : 1 2 3 . 0
123 . 11 doue
. 123 11 dou e : 0 . 1 23
0 . 123 11 doue
1 . 233 11 dou e : 1 2 30 . 0
1 . 23-3 11 d : 0 . 0 0 1 23
1 . 23+3 11 dou e : 1 2 30 . 0

doule,
. .
11 68

1 . 23 // d
1 . 23f // floa t
1 . 23L // l ong d

.2..
bool true false.
true 1 , false - .

.2.4.
(character literal) - ,
, ' ' ' @ ' ,

fl .

ASCll ++
NL \n
\t
v \v
BS \
CR \r
FF \f
BEL \
\ \\
? \?
\'
11 \ 11
\
hhh \xhhh

++,
, ' \n ' ( ) ' \ t '
().
:
acdefqhijklmnopqrstuvwxyz
ACDEFGHIJRLNOPQRSUVWXYZ
0123456789
! @#$% & * ( ) -+ 1 - ' { } [ ] ; 11 ; 1 <>? t /

. , ' ' ,
( , .
cout << int ( ' a ' ) ).
.3. 11 69

.2.5.
(string literal)
-

, , "nuth" " King Canute" .


;
\n.
"inq
Canute " // :
"inq\nCanute" 11 :

, ,
. .
"inq" " Canute " // "KingCa n u t e " ( )

,
, \n.

.2.6.
(pointer literal):
nullptr.
, :
t * pl = ; 11 :
int* 2 "
2-2 ; 11 :
int* = 1; 11 : 1 - i n t ,
int z = ;
int* 4 = z; 11 : z -

.
++ ( ,
) NULL .
:
int* 4 = NULL ; // (
// NULL)

..
(identifier) ,
-

. (
) , (
).
int foo_bar ; 11
int FooBar ; 11
int foo bar ; 11 :
int foo $bar ; 11 : $
11 70

,
,
; :
int _foo ; //
int foo_bar; //
int foobar ; //
int foo_ ; //

.. 1 .
(keywords) - ,
.

( )
alignas class explicit noexcept signed typename
alignof compl export not sizeof union
and concept extern not_eq static unsigned
and_eq const false nullptr static_assert using
asm const cast float operator static cast virtual
auto constexpr for or struct void
bi tand continue friend or_eq switch volatile
itor decltype goto private template wchar t
bool default if protected this while
break delete inline pulic thread local xor
case do int register throw xor_eq
catch doue long reinterpret_cast true
char dynamic_cast mutae requires try
charl t else namespace return typedef
char32 t enum new short typeid

.4. .
++ (
; . . 1 7)
(scope); , ,
. () ; .
, (storage
class). (lifetime)
.
.4. , 11 71

.4.1 .
(. 8.4).
(global scope).
,
(, ).
(namespace scope).
,
, -
(. ). ,
- "
".
(local scope).
, (
).
(class scope).
, .
(statement scope).
, ( . . . )
for, while, swi tch if.
()
, . .
for ( int i = ; i<v . size ( ) ; ++i) {
// i

if (i < 2 7 ) // i for
11 r

,
:
void f ( ) ; //

namespace N
void f ( ) // N
{
int v ; //
: :f() ; / / f ( )

void f ( )
{
N: :f ( ) ; 11 f (x ) N
11 72

. , N : : f ( ) : :f()
. (. 1 5. ) .

..2.
( 1 7.4).
(automatic storage). ,
( ).
(. . ),
static.

. . ( )
,
. (. 8.5.8).
(static storage). .

. , ,
static .

.
() (free store (heap)). ,
new, .
.
vector<int> vg ( lO ) ; //
// ( " ma in ( ) ")
vector<int>* f ( int )
{
static vector<int> vs (x) ; // f ()
vector<int> vf ( +) ; // f ()

for ( int i=l ; i<lO ; ++i ) {


vector<int> vl ( i ) ; //
11
11 vl ( )

return new vector<int> (vf) ; // vf


//
11 vf

void ff ( )
{
vector<int>* = f ( l O ) ; 11 f ()
11 . . .
delete ; // ,
1 1 f
.4. . 11 73

vg vs, ,
( main ( ) )
. .
.
- ,
( , . ).
. , -
;
.
(. 1 4. 1 7.4.)

.4. .
() ,
. ,
. ,
.
,
(, . 1 7.4 .4.2).
() ,
,
.

. -
,
.
r1
( main ( ) )
( main ( ) ) .

, ()
(
) .
11 new (-
) delete.
,
, , .
.
const char* s trinq_t [ ] = { 11ozart11 , 11 Grieq11 , 11 Haydn 11 , 11 Chopin11 } ;
const char* f ( int i ) { return strinq_tl [ i ] ;
void q ( strinq s ) { }

void h ( )
11 74

const string& r = f ( O ) ; 11 r
g ( f (l) ) ; //
string s = f ( 2 ) ; // s
cout << 11 f ( 3 ) : 1 1 << f ( 3 ) //
<< 11 s : 11 << 8
<< 11 r : 11 << r << ' \n ' ;

:
f ( 3 ) : Chopin s: Haydn r: Mozart

, f ( 1 ) , f ( 2 ) f ( 3 ) ,
, .
. f ( 0 ) , r
"" h ( ) .

. 5 .
++.
, , m -

; - ; . ;
-

- ; v lvalue; lst
-
-

. "
" ( .5.2.2). .
, ,
, ,
, ,
, (. 9.6).


N: :m m N; N
-

: :m m

, ,
, N : : : : m (. 8.7).


x.m ;
p->m ; ;
( * ) . m
[] ; * ( +)
f (lst) : f lst
(lst) : lst
v++ ; v++ v
. 5 . 11 75

1
v-- ; v-- v

typeid (x) -

typeid (T)
dynamic_cast<T> (x)

static_cast<T> (x)

const_cast<T> (x) ,
cons t ,

reinterpret_cast<T> ()

typeid ;
.
, .
,
( .5. 7).


sizeof ()
sizeof (x) ( )
++v ; v+=l
--v ; v-=1
; -
! ; true false
&v v
* ,
new
new (lst)
lst
new (lst) , lst
new (lst) T ( lst2) , lst,
lst2
delete ,
delete [ ] ,
() ;

, delete
delete [ ] . new
11 76

( .5.6) . , ( )
- , , -
( .5. 7).


. *ptm , ptm
p->*ptm *, ptm

:
.



/
% ( ) ( )

=, / % .
, % .


+
-

<<
>>

>> << (.
25.5.4). iostream,
(. 1 0 1 1 ) .


< ; bool
<=
>
>=

bool .


== ; bool
! =

, ! = ! ( ==) .
bool.
.5. 11 77

" "
& ""

& ( '". 1 . >> <<)


.

. , unsigned char,
& unsigned char,
&
( .5.5).

" " ()
" " "
" "
1 ""
" "
&& ""; true false; ,
true
" "
1 1 ""; true false; ,
false

(. .5.5.)

1
x? y : z true, ; - z

.
teplate<typename > & max (T& , & ) { return ( >) ? : ; }

"? : " 8.4.


v=x v; v
v*=x v=v* ()
v/=x v=v/ ()
vl!i=x v=vl!i ()
v+=x v=v+ ()
v-=x v=v- ()
v>>=x v=v>> ()
v<<=x v=v<< ()
v&=x v=v& ()
v"=x v=v" ()
v l =x v=v 1 ()
11 78

" v=v* () " . v*=x


v=v* ( ) , ,
v . , v [++i ] *=7+3
(++i , v [ i ] =v [ i ] * ( 7+3) ) , (v [++i ] =v [++i ] * ( 7+3 ) ) (
; . 8.6. 1 ) .

throw
throw

throw - void.

" "
, , .

,
.
, .
, +* + (*) . (+) ,
* +.
*++ * (++) , ( *) ++ .
(right
associative); - . ,
== = (=) , ++ (+) +.
Lvalue , .
(, lvalue,
const, ) .
lvalue rvalue, ..
, ,
, , , , (&f ()
, , f () ,
rvalue).

.5.1 . .
, , .
, ,

, ,
, . .
class ine { /* " . */ } ;
bool operator== (ine , ine ) ;

void f (ine , ine )


. 5 . 11 79

if ( ) { // == opera tor== (a , )
11 . . .

, , - (. 9, . 1 2)
(. 9.5, . 1 1 ).

.5.2.
( .8)
.
.
. . ,
, .

.5.2 . 1 .
, ,
11 (promotios). ,
in t
(integral promotion).
:
"" . ,
float doue .

( .5 . 2 . 2).

.5.2.2.

.
,
(. 3.9 25. 5.3).
.
.
.
.
(unsigned),
,
(
). ,
,
;
. , l char
.
1 1 80

.

.
, ', .

, .
. ,
float doule .
.
void* (.
1 7.8 27.3.5). ()
()
(. 1 4 . 3).
(. .5 4.3. 1 ), ,
. *
const . &
const &.
. ,
bool .
true, -
false.
.
,
. .
.
,
.

, .

, .
.
,
,
.
1 . long doule,
long doue. ,
doule,
doule. ,
float, float.
.
. 5 . 1181

2. unsigned long,
unsigned long. ,
long int, unsigned int,
-

unsigned int long int


, long int
unsigned int.
unsigned long int. ,
long,
long. ,
unsigned, unsigned.
int.
,
.

.5.2.. .

.
, .
.
explici t (. 1 8.3. 1 ) , ,
.
.

. 5 . .
(constant expression) ,
-


int. ( ,
.) .
const int = 2 * 3 ;
const int = +3 ;

constexpr int 2*3 ;


constexpr int = + ;

(const) ,
. (constexpr)
.
, ,
case,
int. .
int var = 7 ;
switch () {
case 7 7 : // i
11 82

case +2 : // OI\
case var : // (va r - )
11
};

, constexpr,
.

.5.,. s i zeof
sizeof ()
. - , si zeof ( )
. - , sizeof ()
. .
sizeof (char) =1.

.5.5.
++
.

1
& ""
1 ""
" " "

&& ""; true false; ,
true
1 1 ""; true false; ,
false

,
(&& 1 1 )
false, - true.
.

& 1 1 1
1
1 1

.5.6. new delete


( )
new, - delete (
) delete [ ] ( ) .
. 5 . 1183

, new bad
alloc. new
, .
new:
int* pl new int ; 11 ()
11 i n t
int* 2 new int ( 7 ) ; 11 i n t ,
11 7
int* new int [ lOO ] ; 11 1 00 ()
11 i n t
11 . . .

delete pl ; 11
delete 2 ;
delete [ ] ; 11

new
. . .
. new
, . ;
. (.
1 7.4.4).
delete (
). ,
( . 1 2.3. 1 ).

. 5 . 7 .
.


x=dynamic_cast<D*> (p) D * (
)
x=dynamic_cast<D&> ( *p ) * D &
( bad_cast)
x=static_cast<T> (v) v ,
v
x=reinterpret_cast<T> (v) v , -

x=const_cast<T> (v) v ,
const
x= (T) v :

x=T (v) :

x=T { v } v ( )
11 84


, - ,
D - . v D*,
. ,
dynamic_cast ,
bad_cast, . .
- ,
.
" ",
.. , v
(. 1 7.8).
reinterpret_cast
. .
, .
,
(. 1 7.8 25.4. 1 ).

,
s tatic_cast reinterpret_cas t const_cast.
.
.
1 7 . 8 25.4. 1 .
:
, (. 27.3.4).
,
.

.6.
++
(opt "").
statement:
declaration
{ statement-list0pt
try { statement- list0pt } handler- list
express ionopt ;
selection-s tatement
iteration-s tatement
labeled-statement
control-statement
selection- statement:
if ( condition ) statement
.6. 1185

if ( d ) statement else statement


swi tch ( d ) statement
i/.eration-s tatement:
while ( condition ) statement
do statement while ( expression
for ( .for- init-staternent codition0pt expressiOopt ) statement
for ( declaration : expression ) statement
laeled-statement:
case constant-expression : statement
defaul t : statement
identifier : st.atement
control-statement.:
break ;
continue ;
return expressiorli>pt
goto idenfier ;
statement- list:
statement statement- list0p t
condition:
expression
type-spec{rer declarator expression
Jor- init-statement:
expressiorlopt
type-speclfrer declaral.or expression
handler- list:
catch ( exception- declaration ) { statement- list0pt
hadler- list handler- lis l.0pt
, - ,
.
.
(for while); . 4.4.2.
(if, swi tch, case break); . 4.4. 1 .
break
swi tch, while, do for. ,
,
.
; . .5 4.3.
; . .6 8.2.
( try catch); . 5.6 1 9.4.
, ,
( ?).
1 1 86

int* f ( int [ ] , int n )

i f (== ) throw Bad_p (n) ;


vector<int> v ;
int ;
while ( cin>>x)
if (x==terminator) break ; // wh i l e
v . push_back ( x) ;

for ( int i = ; i<v . si ze ( ) && i<n ; ++i ) {


if ( v [ i ] ==*p)
return ;
else
++ ;

return ;

. 7 .
(declaration) :
;
;
(
).
:
,
( .8);
, ( ) (-
. 1 -. 1 1 , 9);
( ) ( . 1 3);
( . 1 6);
( . 1 5 8.7);
( - ) ( .9, 8);
( ) ( . 1 1 9.5);
( . 1 7. 2 27.8).

( 3.9.2, 9.4.2, 1 8.2).
:
vector<int> v { a , b , c , d} ;
int { y * z } ;
.. 1187

auto,
, ( 1 3.3,
2 1 .2). :
auto = 7 ; // - in t
const auto pi = 3 . 14 ; // pi - doue
for ( const auto& : v) // - v

.7 . 1 .
,
- ,
,
(definition). ,
. .
doue f ( ) ; //
doue f ( ) { / * . . . */ } ; //
extern const int ; //
int ; //
int z = 10 ; //

.
.
extern (
) ,
( . 1 2.3).

( . 1 2 .3).

..
++ ,
.


bool - (true false)
char - ( 8 )
short - int ( 1 6 )
int -
float - (" doule")
doule -
void* - ( )
* -
1 1 88


*const - ()
a [n] - , n
& r r-
f (arguments) f - , argnents

const - ()
long -
unsigned -
signed -

" ",
long unsigned int, long doule, unsigned char const char* (
char) . ;
, short doue ( float) :
signed bool ( ): short long int (
) long long long long int. long
long, , 64 .
(floating-point types) - float, doue
long doue.
++.
(integer types) - bool, char, short, int,
long long long, .
,
.
3.8, 1 7.3. 1 25.5. 1 ;
- 1 7 1 8; - 8.5.4-8. 5.6.

.8.1 .
(pointer) - .
.
:
int = 7;
int* pi = & ; 11 pi
int = *pi ; 11 *pi - , pi , . . 7

- ,
.
int* pi2 ; 11
*pi2 = 7 ; 11
pi2 = nullptr ; 11 ( pi2
11 )
.. 11 89

*pi2 = 7 ; //
pi2 = new int ( 7 ) ; // pi2
int = *pi2 ; // : 7

, (nullptr).
:
if (2 == ) { // " "
// *2

:
if (2 ) { 11 " "
// *2

(. 1 7.4 1 8.6.4.)
(e-void).


* /
p [i ] /
p=q
p==q
p ! =q
p+i
p-i
p-q :
++ ( )
++ ( )
-- ( )
p- ( )
p+" i i
p -=i i

, (. + +
+=7 ) . .
,
, (. ,
).
<. <=. > >=
, .
void*
( ), (
) (=. ! =. <. <=. > >=).
1 1 90

(. 27.2.5)
. .
using Handle_type = void { * ) {int) ;
void my_handler { int) ;
Handle_type handle = my_handler ;
handle { lO ) ; // my_handler ( l O )

.8.2. 1
(array) - (
) , .
int a [ l O ] ; // 1 0

.
, .
, [ 7 ] .
( ) new,
,
, .
.
,
:
int* = ; // [ ]


[ ] :
[7 ] = 9;
int = [) ;
( 1 8.6).
. ,
,
,
.
vector.
- , :
int a [max] ; // s i zeof (a ) == s i zeof ( i n t ) *max

( ).
.. ( ). :
doule da [ lO O ] [200) [300) ; // 300 ,
11 2 0 0 ,
11 1 0 0 doue
da [ 7 ] [ 9 ] [ 1 1 ) ;
.9. 1191

-
(. 24. 4). ,
atrix (, 24).

.8..
(reference) - (alias). ..
.
int 7;
int& r ;
r = 8; // 8

,
.
void f ( const strinq& s ) ;
11 . . .
f ( " , " ) ;

(. 8.5.4-8. 5.6.)

. 9 .
(function) - .
(, ) ()
.
, .
char f ( strinq , int) ;

f - , s tring int
char.
, , .
,
.
char f ( strinq s , int i) { return s [ i ] ; }

(. 8.2) try (.
5.6.3).
. , -
, ( return).
char f ( strinq s , int i ) {
char = s [ i ] ; 11 :

main ( )
(. . 1 .2). main ( ) ,
11 92

,
void. . void
.
void increment ( int& ) { ++ ; } // :

( )
.
char xl = f ( l , 2 ) ; !/ : f ()
strinq s = "Battle of Hastinqs " ;
char 2 = f ( s ) ; 11 : f ()
char = f ( s , 2 ) ; 11

. 8.

constexpr.
, ,
. , constexpr,
( 8.5.9).

. 9 . 1 .
(overload resolution)
-

.
.
void print ( int) ;
void print (doule ) ;
void print ( const s td : : strinq&) ;
print ( 12 3 ) ; 11 pri n t ( i n t )
print ( l . 23 ) ; // pri n t (doule)
print ( " 12 3 " ) ; 11 pri n t (con s t s t ring&)

, .
. ,
, .
.


( ).

.
1 . , ..

(, .
cons t ).
.9. 1193

2. , .. (l
int, char int, short int ; . .8),
float doue.
3. , int
doule, doule int, doule long doule, Derived* * (.
1 4 .3), void* (. 1 7. 8), int unsigned int (.
25. 5.3).
4. (.
.5.2.3).
5. ( . . . )
( .9.3) .
, ,
, .
, .
(. .5.3).

.
,
, .
;
. .
void f ( int , const string& , doule) ;
void f ( int , const char , int) ;

f ( l , "hello" , 1 ) ; // : f (i n t , cons t cha r * , i n t)


f ( l , string ( "hello" ) , 1 . 0 ) ; 11 : f (i n t , con s t s tring& , doule)
f ( l , "hello" , 1 . 0 ) ; // :

"hello" const char


, const string& - .
, 1 . doule ,
int - ,
f ( ) .
,
, . , .
, , .
- ;
.

.9.2.
,
.
1 1 94

,
, ,
. .
void f ( int , int=O , int=O ) ;

f ( 1 , 2 , 3) ;
f ( 1 , 2 ) ; // f ( 1 , 2 , )
f ( l) ; // f {l , 0 , 0)


. .
void q ( int , int =7 , int) ; 11 :
11
f ( l , , 1) ; / / :

, ,
( ).

. 9
. .

, ,
. ( ), ", ,
. . .

". ,
, , : printf ( ) (.
27.6. 1 . 1 1 .2):
void printf ( const char* format . . . ) ; 11
11 , , -
int = ' ' ;
printf ( " hello , world ! " ) ;
printf ( "print char 1 %c 1 \n" , x) ; //
11
printf ( "print strinq \ " % s \ '"' , ) ; 11 " "

, % %s,
. ,
. ++
.

. 9 4
. .

++
; ,
++ (
++), - (
). , ++
(linkage specfications),
.1 . . 1 1 95

,
.
.
extern "" void callae_from_C ( int) ;

.
extern "" (
void callae from_C (int) ;
_
int and_this_one_also ( doue , int* ) ;
/ * . . . */

27.2.3.
.

.

. 1 . ,
() :
(class, struct union: . . 1 2) (enum:
. A. l l ) .

. 1 0.1 .
,
.

. ,
( ). .
operator: , ,
+, operator+:
atrix operator+ ( const atrix& , const atrix&) ;

std : : ostream ( 1 0
1 1 ), std : : vector ( 1 7- 1 9 .4), std : : complex ( .9.3)
atrix ( 24).
:
?: sizeof typeid alignas noexcept

. ,
:
[] () ->
1196

-,
.
,
= ( ). & ( ) , ().
.

.

. 1 1 .
(enumeration) ,
().
// " " :
enum Color { green , yellow, red } ;
//
enum class Traffic_light { yellow, red, green } ;

enwn class
, ""
enum . :
Color col = red; //
Traffic_light tl = red; // :
// ( , Color : : red) Tra ffi c_ l i gh t

, Color : :
green=O, ,
Color : : yellow=l Color : : red==2 . ,
:
enum Day { Monday=l , Uesday , Wednesday } ;

onday=l, Tuesday=2 Wednesday==.


"" enwn
,
.
int = green ; 11 : Color i n t
Color = green ; 11
= 2; 11 :
11 i n t Color
= Color ( 2 ) ; 11 : ()
int = ; 11 : Color i n t
enum class
,
:
. 1 2 . 1197

int = Traffic_liqht : : qreen ; // : -


11 Tra ffic_ l i gh t i n t
Traffic_liqht = qreen ; // : -
11 i n t Tra ffi c_ l i gh t

9.5.

. 1 2 .
(class) .
-

, .
class {
puic :
//
private :
11
};
, , ,
.J . 9.

. 1 2.1 .
(puic) ;
(private) .
class Date
puic :
11 . . .
int next day ( ) ;
_
private :
int , m, d ;
1;

void Date : : next_day ( ) { return d+l ; } //

void f (Date d)
{
int nd = d . d+ l ; // : Da t e : : d -
11 . . .

- ,
.
struct S
// ( , )
};
1 1 98

,
(protected) , 1 4.3.4.
. (),
. -> ().
.
struct Date {
int d, m, ;
int day ( ) const { return d ; 11
int month ( ) const ; // ;
//
int year ( ) const ; // ;
11
};

Date ;
x . d = 15 ; 11
int = x . day ( ) ; //
Date* = & ;
p->m = 7 ; 11
int z = p->month ( ) ; //

. . (
):
int Date : : year ( ) const { //
return ;

-
, .
struct Date {
int d , 111 , ;
int day ( ) const { return d ; }
11 . . .
};

. :
void f (Date dl , Date d.2 )
{
dl . day ( ) ; 11 dl . d
d.2 . day ( ) ; // d2 . d
11 . . .

.1 2.1 . 1 . thi s
.
. this.
.12. 1199

struct Date
int d, m, ;
int nth ( ) const ( return this->m; }
11 . . .
};

-, const (
-). ,
.
struct Date (
int d, m, ;
int nth ( ) const ( ++m; } / / : mon th () -

11
11 . . .
};

-
9. 7.4.

.12.1 .2.
, ,
-, friend.
.
// Ma trix Vec t o r :
Vector operator* ( const atrix& , const Vector&) ;

class Vector {
friend Vector operator* ( const atrix& ,
const Vector&) ; //
11 . . .
};

class atrix (
friend Vector operator* ( const atrix& ,
const Vector&) ; //
11 . . .
};

, ,
. friend -

,
.
class I ter
puic :
int distance_to ( const iter& ) con s t ;
friend int difference ( const I ter& , const Iter& ) ;
11 . . .
};
1 200

void f ( I ter& , Iter& q)


(
int = p . distance_to ( q) ; // -
int = difference (p , q) ; // ,
//
11 . . .

, , friend,
.

. 1 2.2.
, ,
, / (
9. 7.3), ( 9.4.4).
struct S (
int = 1;
int 2 ;

void f ( )
void f2 ( ) ;

struct SS ( int ; } ;
struct SS2 ;
};

, ,
"- ".
int S : : 2 = 7;

void S : : f2 ( ) (

struct S : : SS2 ( int m; } ;

- ,
, .
- , .
struct S (
int m ;
void f ( } ;
};

sizeof ( S ) =sizeof ( int) .


,
. ,
"" , (. 1 4.3. 1 ).
.12. 1 201

.1 2.. ,
,
(constructors). -
-, ,
:
class Date {
puic :
Date ( int , int mm, int ddl : { } , m{mm} , d { dd } { }
11 . . .
private :
int y , m, d ;
};

Date dl { 20 0 6 , 11 , 15 } ; // :
Date d2 ; 11 :
Date d3 { 11 , 15 } ; 11 :
11 ( )

, -
(
).
.

(. 9.4.2 9.4.3).
" ",
( 1 4.3. 1 ) .
, .
- ,
, .

explici t,
:
class Date {
puli c :
Date ( const char* ) ;
explicit Date ( long) ; 11
11 . . .
1;

void f (Date) ;

Date dl = " June 5 , 1848 " ; //


f ( " June 5 , 1848" ) ; 11

Date d2 = 2007*12*31+6* 31+5 ; // : Da te (long)


// exp l i ci t
1 202

f ( 2 0 0 7 * 12 * 31+6*31+5 ) ; // : Da te ( l ong)
!/ expl i ci t

Date d3 { 20 0 7 * 12 *31+ 6 *31+5 } ; //


Date d4 = Date { 20 0 7 * 12 *31+6*31+5 } ; //
f (Date { 20 0 7 * 1 2 * 31+6*31+5 } } ; 11


,
rw (default constructor).
,
( .
, ). .
struct S {
string name , address ;
int ;
};

s s ( ) .
name address. . ,
:
S sl { "Hello ! " } ; 11 sl "Hel l o ! ", )
S s2 { " Howdy ! " , 3 } ;
S* = new S { "G ' day ! " } ; // * "G ' da y " , ) ;

.
( -
int).

.1 2. 3 . 1 .
(.. ,
)
(destructor). - (
). .
class Vector { // doue
puic :
!/ :
explicit Vector ( int s ) : s z { s } , p { new doule [ s ] } { }
!/ :
-vector ( ) { delete [ ] ; 1
11 . . .
private :
int sz ;
doue* ;
1;
. 1 2. 1 203

void f (int ss)


{
Vector v (s) ;
11 . . .
11 f () v ;
11 Vector

. ,
. ,
(. 1 7.5.2).
, , ""
.
" ",
, ,
, - ,
, .. , .

.1 2.3.2.
:
class Vector { // doue
pulic :
11 :
explicit Vector ( int s) : sz { s ) , p { new doule [ s ] ) { )
vector ( ) { delete [ ] ; ) / /
Vector ( const Vector& ) ; //
Vector& operator= ( const Vector& ) ; 11
11 . . .
private :
int sz ;
doue* ;
);

void f (int s s )
{
Vector v ( s s ) ;
Vector v2 = v; //
11 . . .
v = v2 ; 11
11 . . .

(..
)
. (.
1 4.2.4 1 8.3).
1 204

.1 2.3.3.

:
class Vector // doue
puic :
11 :
explicit Vector ( int s ) : s z { s } , p { new doule [ s ] } { }
vector ( ) { delete [ ] ; } / /
Vector (Vector&&) ; // Ij1
Vector& operator= (Vector&&) ; //
11 . . .
private :
int s z ;
doue* ;
);

Vector f ( int s s )

Vector v ( s s ) ;
11 . . .
return v ; // Ij1

(.. .
)
.
(. 1 8.3.4).

. 1 2.4.
.
, (
).
struct {
int ;
void f ( ) { );
};

class D : {
int md ;
void fd ( ) ;
};

: m f ( ) ,
D - : m, f ( ) , md fd ( ) .
,
(puic private).
.12. 1 205

Class DD : pulic Bl , private 2 {


11 . . .
};

Bl
DD, 2 DD.
-


, DD
Bl 2 .
(,
, DD), .
(multiple inheritance).
D
,
D. .
struct { };
struct Bl : { } ; // - B l
struct 2 : { } ; // - 2
struct { };
struct DD : Bl , 2 , private { } ;

DD* = new DD ;
Bl* = ; //
* = ; // : : Bl : : B 2 : : ?
* = ; // : DD : : C -


.

1 4.. (protected)
.

.1 2.4. 1 .
Ju (virtual function) -,
-

,
.
,
, ,
. I, (override)
- .
class Shape
pulic :
virtual void draw ( ) ; 11 "vi r t ua l "
/ / " "
virtual shape ( ) { } //
11 . . .
1 206

};

class Circle : pulic Shape


puic :
void draw ( ) ; // Shape : : dra w ()
circle ( ) ; 11 Shape : : -Shape ()
11 . . .
};

. (
Shape) (
Circle).
void f ( Shape& s )
(
11 . . .
s . draw ( ) ;

void q ( )
{
Circle c { Point { 0 , 0 } , 4 } ;
f ( c) ; // dra w () Ci rcle

, f ( ) Circle:
Shape. ,
, ,
( 1 4.3).
. , . .
(, . Shape)
(. 1 7.5.2).
,
, override:
class Square : puic Shape {
pulic :
void draw ( ) override ; // Shape : : dra w ()
circle ( ) override ; // Shape : : -Shape ()
void silly ( ) override ; // : Shape
11 Shape : : si l ly ()
11 . . .
};

.1 2.,.2.
(abstract class) - ,
.
.
. 1 2. 1 207

Shape s ; 11 : Shape

class Circle puic Shape {


puic :
void draw ( ) ; // Shape : : dra w ()
11 . . .
};

Circle { , 20 } ; // : Ci rcl e


-
41111 (pure virtual function), .. , .
class Shape
pulic :
virtual void draw ( ) ; 11 = " "
11 . . .
};
(. 1 4.3.5.)
,
(protected); .
. 1 4.2. 1 .

.1 2.4..

.
.
(
).
(
).
.
( )
. "
", ..
.
.
,
.
.
struct D : Bl , 2 {
Ml ml ;
1 208

2 m2 ;
};
, 81, 82, Ml 2 ,
:
D f()
{
D d; //

D d2 = d ; //

d = D{ } ; //
,
11

return d ; //
d . f ()
// d d2

, d
( ): 81 : : 81 ( ) , 82 : : 82 ( )

Ml : : Ml ( ) 2 : : 2 ( ) .
, d . return
( ):
81 : : 81 ( ) . 82 : : 82 ( ) , Ml : : Ml ( ) 2 : :2 ( ) .
, d .
d
( ): 2 : : -2 ( ) . Ml : : -Ml ( ) . 82 : : -82 ( ) 81 : : -81 ( ) .
.
d .
,
.
. ,
.

. 1 2.5.
(itfield) -

(, ).
.
struct PPN { // R
unsigned int PFN 22 ; //
int : 3 ; / /
unsiqned int 3 ; //
bool nonreacheale : 1 ;
bool dirty : 1 ;
bool valid : 1 ;
bool qlobal : 1 ;
};
.1 . 1 209


(. 25.5.5).
: 31: 9: 6: : 2: 1: :

PPN: 1 22 l 1 l l J1 l 1
1 1 1

: PFN dirty global


h valid

. ,
.
,
.

char int .
,

( - ).
,
.

. 1 2.6.
(union) ,
-

.
.
. .
.
union U {
int ;
doue d;

U ;
. = 7 ;
int xl = . ; //
a.d = 7 . 7 ;
int 2 = . ; // !


. .

. 1 .
(template) ( ).
-

/ .
1 210

template<typenae >
class vector {
puic:
11 . . .
int size ( ) const ;
private :
int sz ;
* ;
};
template<typenae >
int vector<T> : : size ( ) const

return sz ;

class ;
typename.
-
, .

.
template<typenae , int sz>
class Fixed_array
pulic :
a [ sz] ;
// . . .
int size ( ) const { return sz ; } ;
};
Fixed_array<char , 256> xl ; //
int var 226 ;
=

Fixed_array<char , var> 2 ; // :

. 1 3.1 .
,
.
vector<int> vl ; 11
vector v2 ; 11 :
vector<int , 2> v ; 11 :
vector<2> v4 ; 11 :

.
template<typenae >
find (vector<T>& v , int i )
(
return v [i ] ;
.1 . 1 211

vector<int> vl ;
vector<doule> v2 ;
11 . . .
int xl = find (vl , 2 ) ; // - i n t
int 2 = find (v2 , 2 ) ; // - d

,
.
( ,
). .
template<typena.me , typena.e U>
* m.ake ( const U& u) { return new T (u) ;

int* pi = make<int> ( 2 ) ;
Node* pn = make<Node> (makeyair ( "hello" , 17 ) ) ;
, Node
pair<const char* , int> ( .6.3).

(
).

.1 .2.

(specialization).
m
(template instantiation). ,
,
. ,
. .
template<typena.e >
struct Compare { //
bool operator ( ) ( const & , const & ) const
{
return < ;

};
template<>
struct Compare<const char*> { // -
bool operator ( ) ( const char* , const char* ) const
{
return strcmp ( a , b) < ;

};
1 21 2

Copare<int> 2 ; //
Copare<const char*> ; 11 -

bool 2 ( 1 , 2 ) ; //
bool 2 ( "asd" , "dfq" ) ; // -

r .
teplate<typena.me > bool copare ( const & , const & )
{
return < ;

bool r ( const char* , const char* ) // -


{
return strcp (a , ==O ;

bool = r (2 , 3 ) ; 11
bool 4 = copare ( "asd" , "dfq" ) ; 11 -

(
, -
) . ,
.
.

. 1 .. -
, , ,
(, - -).
. , .
,
, , -
.
typename. .
teplate<typena.me > struct Vec (
typedef value_type ; // -
static int count ; // -
11 . . .
};

teplate<typenae > void y_fct (Vec<T>& v)


{
int = Vec<T> : : count ; //
//
v . count = 7 ; //
11 , J
. 1 4. 1 21 3

typename 11
Vec<T> : : value_type ; // typename
// . . .

1 9 .

. 1 4.
( throw) ,
,
. , d_size Vector.
struct Bad_size {
int sz ;
Bad_size (int s ) ss{s} { }
};
class Vector {
Vector (int s) { if ( s<O 1 1 maxsize<s) throw Bad_size { s } ;
11 . . .
};
, ,
.
.
void f (int )
{
try {
Vector v ( x) ; //
11 . . .

catch (Bad_size bs)


cerr << 111 ( " << bs . sz << 11 ) \n" ;
11 . . .


catch ( . . . ) :
try {
11 . . .
catch ( . . . ) { //
11 . . .

, (, , ) RAII
("Resource Acquisition ls lnltialization" - "
"), try catch (.
1 9. 5).
1 21 4

throw (. . throw ; )
. .
try {
11 . . .
catch ( Some_exception& ) {
//
throw; //

.
.
. ( .2. 1 ).
(
- . ).
.
++ tch.
. ,
try , .
, . ,
. , . . . . .
. ,
. . .
. .
.
(stack unwinding) .
,
. .
- . ,
(
, - ). ,
, .
. .
, .
. . .
: : {
if (in_a_real_mess ( ) ) throw ess ( ) ; //
// !

,
( )
, .
. .
- .

. 1 5 . 1 21 5

, . . ,

.

. 1 5 .
11 (namespace)
.
int ;

namespace Foo {
int ;
void f ( int i)
{
+= i ; //
11 Foo (Foo : : )

void f ( int) ;
int ain ( )

= 7; 11 ( : : )
f (2) ; 11 f ( : : f)
Foo : : f ( 3 ) ; 11 f Foo
: : f (4) ; 11 f ( : : f)


(. Foo : : f ( 3 ) )
: : (, : : f ( 2 ) ).
.
(. -
s td)

usinq namespace std ;
using. ,
, . ,
using .
:
usinq Foo : : q ;
q (2 ) ; 11 g Foo (Foo : : g)

8.7.
1 21 6

. 1 6.
r .
(alias); . ,
, , (
).
using Pint = int* ; // Pin t i n t

namespace
Long_library_name { / * . . . */ }
na.mespace
Lib = Long_library_name ; // Lib Long_ l ibra ry_name

int = 7 ;
int& r = ; 11 r

(. 8.5.5 .8.3) -
. using (. 20.5) namespace

. , using ,
. .
using Pchar = char* ; 11 Pcha r- cha r *
Pchar = " Idefix" ; 11 : - cha r *
char* q = ; 11 : q - cha r *
int = strlen (p) ; // : - cha r *

++ using
typedef (
27.3. 1 ):
typedef char* Pchar ; // Pch a r cha r *

. 1 7.
++ (preprocessor).
,
, , , .

, , .
, #,
.
.1 7. 1 21 7

. 1 7 . 1 . # incl ude

:
llinclude "file . h "
.
file . h .
( " . . . " ).
(< . . . >). :
tlinclude<vector>

.

.1 7.2. #define

. rwm (m substitution).
. .
tldefine FOO bar
. FOO,
bar.
int FOO = 7 ;
int FOOL = 9 ;
:
int bar = 7 ;
int FOOL = 9 ;
, ++
, FOO,
FOOL.
define ,
.
#define wt (x , y) ( ( () > () ) ? () : () )
:
int = wt (FOO+ l , 7) ;
int = (++ , 9) ;
:
int = ( ( (bar+l) > ( 7 ) ) ? (bar+l) : ( 7 ) ) ;
int = ( ( (++) > ( 9 ) ) ? (++) : ( 9) ) ;
1 21 8

, ,
FOO+l . ,
.
. ,
.
. !
, .
, ALL_CAPITAL_LETERS;

. ,
(.
max) .
(. 27.8.)


711- '
1N ..
- :Jt.J :J*. !/ ()oNt j. lt!fak.)



++ .
,


, .
1 220

. 1 . .6.3. pair tuple


. 1 . 1 . .6.4.
. 1 .2. std .6.5. -
. 1 .3.
.2 . .7. -
.2. 1 . .7. 1 . --
..
.7.2.
.3. 1 .
.7.3.
.3.2.
.7.4.
.4.
.7.5.
.4. 1 .
.7.6.
.4.2. -
.8.
.4.3. ,
.8. 1 .
.4.4. .8. 2 .
.4.5. .8.3.
.4.6. - .9.
.9. 1 .
.4.7. .9.2.
.4.8.
.4.9. .9.3.
.4.10. .9.4. valarray
.9.5.
.5.
.5 . 1 . .9.6.
. 1 0 .
.5.2. , - . 1 1 .

.5.3. . 1 1 . 1 .
.5 .4. . 1 1 . 2 .
.5 .5. printf ( )
.5.6. . 1 1 .3. -
.5.7. . 1 1 .4.
.5.8. min max .11 .5.
.6. SL .11 .6.
.6 . 1 . . 1 2 .
.6.2.

. 1 .

, .

.1. 1 221

++. , :
,
.

. , ,
,
. Stroustrup,
h ++ Programming Language. ++
ISO ++, . ,
.
, .
( , ) ?

.
, ,
.
,
. ,
: .
,
.
, ,
. , (
, ),
.
, ,
, . ,
. ,

( "") .
.
,
. , , .
, ,
. ,
, -.
, , . !

.1 . 1 .

. , ,
, , .
1 222

STL ( , )
<algorithm> ; sort ( ) find ( ) .. { 2 1 . 1 . 5)
,

<array> { 20. 9)
<itset> bool { 2 5 . 5 .2 )
<deque>
<functional> { .6 .2 )
<iterator> { .4 .4)
<list> { 2 0.4, . 4)
<forward list>
<> mul timap ( 2 1 . 6 . 1 -2 1 . 6 . 3 , . 4)
<memory>
<queue> queue priority_queue
<set> set mul tiset { 2 1 . 6 . 5 .4)
<stack> stack
<unordered_map> ( 2 1 .6 . 4)
<unordered set>
<utility> pair { . 6 .3)
<vector> vector ( ) ( 20. 8 . 4)

-
<iostream> - ( . 7)
<fstream> ( . 7 . 1 )
<sstream> { . 7 . 1 )
<iosfwd> ( ) -
<ios> -
<streamuf>
<istream> { . 7)
<ostream> ( . 7)
<iomanip> ( . 7 . 6)


<string> string ( . 8 .2)
<regex> ( 2 3)


<complex> ( . 9 . 3)
<random> ( . 9 . 6)
<valarray>
<numeric> , accumulate () ( . 9. 5)
<limi ts> ( . 9 . 1 )
.1. 1 223


<exception> ( .2. 1 )
<stdexcept> ( .2. 1 )
<locale>
<typeinfo> ( typeid)
<new>
<memory> , unique_ptr ( .6. 5)


<thread> ( )
<future> ( )
<mutex> ( )


<cstring> - ( .1 1 . 3)
<cstdio> - ( . 1 1 .2)
<ctime> clock ( ) , time ( ) .. ( . 1 1 . 5)
<cmath>
( . 9 .2 )
<cstdlib> aort ( ) , s ( ) , malloc ( ) , qsort ( ) .. ( 2 7)
<cerrno> ( 24 . 8)
<cassert> assert ( 2 7. 9)
<clocale>
<climits> ( . 9 . 1 )
<cfloat> ( . 9. 1 )
<cstddef> ; size_t .
<cstdarg>
<csetjmp> setjmp () longjmp () ( )
<csignal>
<cwchar>
<cctype> ( . 8 . 1 )
<cwctype>



h, <time . h>
.

<ctime>. ,
h ,
.

std.
- - ,
, .
1 224

, -
++ .

. 1 .2. s td

s td, , ,
, using using.
std : : string s ; 1 1

using std : : vector ; 11 using


vector<int>v ( 7 ) ;

using naespace std ; 11 using


map<string , doule> ;
s td
using. using
(. . 1 5).

.1 ..
,
, .

. .


= ( , , ) [,)
,
foo (x) foo - ,

bar (b , e , x) - [,) ?

,
, ;
- ; - ,
. ,
r , ,
, , ,
, . ,
bool,
.
.
"", "
" .. ( .3. l ) , .
.2. 1 225

. 2 .
,
.
.
.
errno
(. 24.8).
, ,
, , .
. ,
.
-
(
) (. 1 0.6 .7.2).
, vector,
string itset, .
,
" (. 1 9. 5.3). ,
, (, )

.

.2. 1 .
,
.


itset invalid_argument, out_of_range
overflow error
dynamic_ bad_cast,
cast
iostream ios_base : : failure,
new bad_alloc,
regex regex_error
string length_error ou t_of_range
typeid bad_typeid,
type_info
vector out_of_range
1 226

.
.

(, exception) - (.
main ( ) ) . , ,
,
.

, int - .
,
. . ,
exception.
class exception {
pulic :
exception ( ) ;
exception (const exception&) ;
exception& operator= ( const exception&) ;
virtual exception ( ) ;
virtual const char* what ( ) const ;
};
what ( ) ,
, .

.

runtlme_error

out_of_range overflow_error

length_error underflow_error

,
, , :
struct y_error : runti.e_error {
y_error (int ) : interesting_value { x } { }
. . 1 22 7

int interesting_value ;
const char* what ( ) const override ( return "y_error" ;
};

. .
- , r
. ,
,
(. 20.3).
sort, find , search, , " . , my_very_own_algorith m , your_code , . . .

vector, list, map, , . . . , my_container, your_container, " .

.. 1 .
- ,
(. * )
(. ++
). ,
[begin,end).

begin :
-
end : Q_


- ---J
'

.. . _ _ _ _ _ _ _

, begin
, end - ,
.
end.
begin=end. ,
[.) .
,
(,)
+ + , .

while ( ! =) ( 11 ! = , <
11 -
1 228

++ ; 11 .

, ,
,
. .
= find (v . begin ( ) , v . end ( ) , x) ; 11 . v
if ( ! = v . end ( ) ) {
11 .

else {
11 . [ v . begin () , v . end () )

(. 20.3.)
, .
, .

. .
template<typename Iter> void f ( I ter , int n)
{
while (n>O) *++ = --n ;

vector<int> v ( lO) ;
f (v . beqin ( ) , v . size ( ) ) ; 11
f (v . eqin ( ) , 1000 ) ; 11

, ..
f ( ) , :
.
.


++ :
, ("
"); p+l
++ :
, ("
") ; ( )
--
:
(" "); -1
- - :
(" "); ( )
* ( ) : * ,

.3. 1 229


[n] () : [n] ,
p+n; * (p+n)
p->m ( ) ; ( *) . m
p==q : , q
,
! =q : ! (p==q)
p<q , ,
q?
p<=q p<q 1 1 p==q
p>q , ,
q?
p>=q p>q 1 1 p==q
p+=n n : n- ,
,
p-=n -n : n- ,
,
q=p+n q n- , ,

q=p-n q n- , ,
; q+n==p
advance : p+=n; advance ( )
(p , n) ,
; n ( )
x=distance q-p distance ( ) ,
( , q) ; -
n ( )

, ( . 3 . 2)
.

..2.

( ) .


++
( * .
) (i put iterator) == ! =.
istream (. 2 1 . 7.2)
++
( * .
) (output iterator) os tream ( . 2 1 . 7.2)
1 2 30


, ++,
(forward ()
iterator) *. ,
->
- ( ++) (
- - ) , (
( id i rectional iterator) ) * .
list, map set
( ++ +=)
( -- -=) ,
( random-access ( ) * [ ] .
iterator) ,
+,
-.
,
, .

<, <=, > >=. vector

, (. 20.8).

,
,
.
, i terator_traits .
:
vector - ;
list - ;
forward_list - ;
deque - ;
i tset - ;
.4. 1 231

set - ;
mul tiset - ;
map - ;
mul timap - ;
unordered_ set - ;
unordered_mul tiset - ;
unrdrd_m - ;
unordered_multimap - .

.4.
.
, - value_type
. .


array<T , N> , N
deque<T>
list<T>
forward list<T>
vector<T>


map<K , V> v;
(, v)
multimap<K , V> v;
set<>
multiset<> ( )
unordered_map<K, V> v,
-
unordered_mul timap<K , V> v,
-;
unordered set<K> , -
unordered multiset<> , -;

(map, set ..)


r, ,
; . set<K, > .
<vector>. <list>
. (. . l . l ) . -
1 232


priority_queue<T>
queue<T> push ( ) ( )
stack<T> push ( ) ( )

,
value_type (
).
() value_type (
pair (, V) ).
set, map mul timap () .
,
unordered, . mul timap
map ,
. -
, .
, vector.
, vector.
( . 1 9.3. 7)
. :

.
new delete, ,
.
, ,
: - , -
(. 1 8 . 5).
" "
( . 20). .
- .
spl ice ( ) l i s t, :
.
,
, - .
" ". .

" "
[n] size ( ) -. -
( , ,
) vector, string array
string , ,
, (+ += ) .
string
.4. 1 233

" 1 "
valarray ,
, .
,

.4. 1 .
, ,
.

:
, ,
, egln(), end(),
regln(), rend(), ==, 1=, <, <=, >, >=,
=, swap() , slze(), max_slze(), empty(),
lnsert(), erase(), clear()

: :
asslgn(), front(), back() , key_comp(),
push_back(), pop_back(), value_comp(), flnd(),
reslze() count(), lower_ound(),
equal_range()

set, map:
multlset operator[]

multimap

llt :
push_front() , vector:
pop_front(), operator[] , deque :
spllce(), at(), operator[],
remove(), capaclty(), at(),
remove_lf(), reerve() push_front(),
unlque() ,
pop_front()
merge(),
ort(),
rvr()
1 234

array forward_l ist.


.
array ,

, .
forward_list ; ,
size ( ) .
.

.4.2. -
, .

-
value_type
size_type , ..
difference_type
iterat val ue_type*
const iterat const value_type*
reverse iterat value_type*
const reverse iterator const value_type*
reference value_type&
const reference const value_type&
pointer value_type*
const_pointer const value_type*
key_type ( )
mapped_type (
)
key_compare (
)
allocator_type

.4.. ,

. .
(, vector<douhle> map
< string , int>) .

,
; -
}
{
.4. 1 235

,
(n) ; n ,
( )
C c (n , x) ; n (
)
{ , } ; [,)
c { elems } ;
elems.
{ 2 } ; 2
... ( ) (
)
cl=c2 ; 2
cl; cl==c2
c . assign (n , x) n ( )
c . assign (b , e) [,)

,
.

.4.4.
,
,
.
( <).


= . begin ( )
= . end ( ) ,
= . rbegin ( ) , -

p=c . rend ( ) ,
,

.4.5.
.


. front ( )
. back ( )
[i ] i- ; ( )
1 236


c . at (i) i- ; (
vector deque}

- -
,
. ,
.

.4.6.
vector deque
. ,
l i s t deque
.


c . push_back (x)
. _back ( )
c . emplace_back (args) { args } ;
-


c . push_front (x) (
) ( list deque}
. pop_front ( ) (
list deque}
c . emplace_front (args) { args } (
) ;
-

, push_front ( ) push_back ( )
. ,
( ) .
, .
push_front ( ) push_back ( ) -
:
vector<pair<string , int>> v;
v . push_back (make_pair ( "Camridge " , 1209) ) ;

,
:
v . emplace_back ( " Camridge" , 1209) ;
"Emplace" " " " ".
.4. 1 237

. .
, .
. .
front ( )
back ( ) (. .4.5).
: ( ,
)
.

.4. 7.
.


q=c . ineert ( , )
q=c . insert ( , n , ) n
q=c . ineert ( , firet, laet) [firet,laet)
q=c . emplace ( , arge) { arge } ; -
-


q=c . eraee () ,
q=c . erase (fire t , laet) ,
[firet,laet)
. clear ( )

q insert ( )
. q erase ( ) .
.

.4.8.
- : -
, ,
.


= . size ( )
-

. empty ( ) ?
= . _size ( )
-

= . capaci ty ( ) , ( vector
-

string)
. reserve (n) n (
vector string)
1 238


c . resize (n) , n (
vector, strinq, list deque)


. . ( )

(.. ).

.4.9.
(. .4.3),
.


cl==c2 cl 2
?
cl ! =c2 cl 2 ,
?
cl<c2 cl 2 ?
cl<=c2 cl 2
?
cl>c2 cl 2 ?
cl>=c2 cl 2
?
swap (cl , 2 ) cl 2
cl . swap (2) cl 2


(. <).
(. <).

.4. 1 .

.


c [k] k (
)
p=c . find (k) k
p=c . lower_bound (k) k
. 5 . 1 239


= . upper_bound (k) ,
k
pair (pl , 2 ) = . equal_ [p l p2 ) k
,

ranqe (k)
r=c . key_comp ( ) r
-

r=c . value_comp ( ) r
-

, . end ( )
, equal_ranqe, lo
wer_bound, - upper_bound.
, "arian" mul timap<s tring ,
int>. :
strinq k = "arian" ;
auto = m. equal_range (k) ;
if (pp . first ! =pp . second)
cout << " ' " << k << 11 ' : \n" ;
else
cout << " ' " << k << " ' \n" ;
for (auto = pp . firs t ; p ! =pp . second ; ++)
cout << p->second << ' \n ' ;

auto = make_Pair (m. lower_bound (k) , m . upper_bound (k) ) ;
. equal_
range, lower_bound upper_bound
( .5.4). pair
.6.3.

.5 .
<algori thm> 60 .
,
( ) ( ) .
,
(,),
- 2,
, ,
; , ,
: [2,2+ (-) ) .
1 240

, sort,
, , find,
.

" ".
,
.

.5. 1 .

;
.

,
f=for_each (b , e , f) f
[,); f
p=find ( , , v) v
[,)
p=find_i f (b , e , f) [,),
f ( *)
p=find_first_ [,),
of ( , , 2 , 2 ) *p==*q
q [2,2)
p=find_first_ [,),
of (b , e , 2 , e2 , f) f (* , *q)
q [2,2)
p=adjacent_find (b , e ) [,),
*==* (p+ l )
p=adjacent_find (b , e , f) [,),
f ( * , * (p+ l ) )
equal (b , e , 2 ) [,) [2,2+ (-) )?
equal ( , , 2 , f ) [,) [2,2+ (-) )
, f (* , *q) ?
pair (pl , p2 ) = (pl , 2)
mismatch (b , e , 2 ) [,) [2,2+ (-) ),
! ( *pl==*p2 )
pair (pl , p2 ) = (pl , 2 )
mismatch (b , e , 2 , f) [,) [2,2+ (-) ),
! f ( *pl , *2 )
p=search (b , e , 2 , e2 ) " " :
* [,),
[2,2)
.5. n 1 241

,
p=search (b . e , b2 , e2 , f) " " :
* [,),
,
f ( * , *q)

p=find_end (b , e , b2 , e2 ) *
[,), - [2,2)
p=find_end ( , , 2 , 2 , f) * -
[,), f ( * , *q) ,
*q [2,2)
-

p=search_n (b , e , n , v) *
[,), , [p,p+n)
v
p=search_n (b , e , n , v , f) *
[,), , *q
[p,p+n) f ( * q , v)
x=count ( , , v) v [,)
-

x=count_if (b , e , v , f) [,),
-

f ( * , v)

,
for_each, ; .
. .
(, count =) .
.
bool odd ( int ) ( return x&l ;
int n_even (const vector<int>& v) //
11 v

return v . size ( ) - count_if (v.eqin ( ) , v . end ( ) , odd) ;

.5.2. .

, .

1,
p=transform (b, e , out , f) *p2=f ( *pl)
*pl [,),
*2
[out,out+ (-) ); p=out+ (-)
1 242

,
p=transform (b , e , b2 , out , f) *=f (*pl , *2)
*pl [,)
*2 [2,2+ (-) ),
* [out,out+ (-) ); p=out+ (-)
= ( , , out) [,) [out,p)
p=copy_backward (b , e , out) [,) [p,out),

p=unique ( , ) [,) ,
[,) (
==)
p=unique ( , , f) [,) ,
[,) (
f)
p=unique_ ( , , out) [,) [out,p),

p=unique_copy ( , , out , f) [,) [out,p),
(
f)
replace ( , , v , v2 ) *q [,),
*q==v, v2
replace ( , , f , v2 ) *q [,),
f ( *q) , v2
p=replace_copy ( , , out , v , v2) [,) [out,p),
*q [,),
*q==v, v2
p=replace_copy ( , , out , f , v2 ) [,) [out,p),
*q [,),
f ( *q) , v2
p=remove ( , , v) *q [,) ,
[,) ,
! ( *q==v)
p=remove ( , , v , f) *q [,) ,
[,) ,
! f ( *q)
p=remove_copy ( , , out , v) [,),
! ( *q==v) , [out,p)
p=remove__if ( , , out , f) [,),
! f ( * q , v) , [out,p)
reverse ( , ) [,)

p=reverse_copy ( , , out) [,) [out,p)

. 5 . 1 243

,
rotate (b , m , e) :
[,) ,
.
* *m
* (+i)
* ( (b+ (i+ (e-m) ) \ (e-b) )
p= rotate_copy (b , m , e , out) [,) [out,p)

random_shuffle (b , e) [,)


random_shuffle (b , e , f) [,) f -

,
; ,
, ""
, .
, ,
,
. , , remove,
. () ;
.
template<typename Iter>
void print_diqits (const strinq& s , I ter , Iter )
{
cout << s ;
while ( ! =) cout << * ; ++ ; )
cout << ' \n ' ;

void ff ( )
{
vector<int> v { 1 , 1 , 1 , 2 , 2 , 3 , 4 , 4 , 4 , 3 , 3 , 3 , 5 , 5 , 5 , 5 , 1 , 1 , 1 } ;
print_diqits ( "all : " , v . eqin ( ) , v . end ( ) ) ;

auto = unique ( v . beqin ( ) , v . end ( ) ) ;


print_diqits ( 11head : 11 , v . eqin ( ) , ) ;
print_diqits ( " tail : 11 , pp , v . end ( ) ) ;

pp=remove ( v . eqin ( ) , pp , 4 ) ;
print_diqits ( "head: 11 , v . beqin ( ) , ) ;
print_diqits ( " tail : 11 , pp , v . end ( ) ) ;

.
1 244

all : 1112234443335555111
head : 1234351
tail : 443335555111
head: 123351
tail : 1443335555111

.5..

. . ,
.


swap ( , )
i ter_swap ( , q) * *q
swap_ranges ( , , 2 ) [,) [2,2+ (-) )
fill ( , , v) v [,)
fill_n ( , n , v) v [,+n)
generate (b , e , f) f [,)
generate_n (b , n , f) f ( ) [,+n)
uninitialized [,) v
fill ( , , v)
uninitialized [out,out+ (-) )
( , , out) [,)

,

, , .
uninitialized_fill unini tiali z ed_copy
.

.5.4.

.
. <.
! (<) & &
! (<) , ==.


sort (b , e) [,)
sort (b , e , f) [,) ,
f ( * , *q)
. 5 . 1 245


stale_sort (b , e) [,) ,

stale_sort (b , e , f) [,),
f ( * , *q)

partial_sort (b , m , e ) [,)
[b,m) ; [m,e)

partial_sort (b , m , e , f) [,)
[b,m),
f ( * , *q) ; [m,e)

partial_sort_
( , , 2 , 2 ) [,) 2-2
[2,2 )
partial_sort_
( , , 2 , 2 , f) [,) 2-2
[2,2 );
f
nth_element (b , p , e) n- (n==p-b) [,)

nth_element (b , p , e , f) n- (n==p-b) [,)
,
f
p=lower_bound (b , e , v) v
[, )
p=lower_bound (b , e , v , f) v
[,);
f
p=upper_bound (b , e , v) ,
v [,)
p=upper_bound (b , e , v , f) ,
v [,);

f
binary_search (b , e , v) v
[,) ?
binary_search (b , e , v , f) v
[,) ?
f
pair (pl , p2 ) =equal_ [pl,p2 ) [, ),
-

range (b , e , v) v; ,
v
1 246 .n


pair (pl , p2 ) =equal_ [pl,p2 } - [,},
range (b , e , v , f) v; ,
v,
f
p=merge ( , , 2 , 2 , ou t)
[2,2 } [,} [out,p}
p=merge ( , , 2 , 2 , out , f)
[2,2 } [,} [out,p},
f
inplace_merge ( , m , )
[b,m} [m,e} [,}
inplace_merge ( , m , , f)
[b,m} [m,e} [,} ,

f
p=parti tion ( , , f) ,
f ( *pl) , [,} , - (,}
p=stale_parti tion ( , , f) ,
f ( *pl) , [,} , - [,},

. :
vector<int> v { 3 , 1 , 4 , 2 } ;
list<doue> lst { 0 . 5 , 1 . 5 , 3 , 2 . 5 } ; // l s t
sort ( v . eqin ( ) , v . end ( ) ) ; // v
vector<doue> v2 ;
merge (v. egin ( ) , v . end ( ) , ls t . egin ( ) ,
lst . end ( ) , back_inserter (v2 ) ) ;
for (auto : v2) cout << << " , " ;
.6. 1 .
:
0.5, 1, 1.5, 2, 2, 2 . 5 , 3, 4,
equal_range, lower_bound upper_ound
, ( .4. 1 ).

.5.5.

.
.
. 5 . 1 247


includes (b , e , 2 , e2 ) [2,2 )
[:) 7
includes (b , e , b2 , e2 , f) [2,2)
[,) ,
f?
p=set_union [ out,p) ,
( , , 2 , 2 , out) ,
[,) [2,2)
p=set_union [ out,p) ,
(b , e , b2 , e2 , out , f) ,
[,) [2,2 ) ,
f
p=set_intersection [ out,p)
( , , 2 , 2 , out) , [,) ,
[2,2 )
p=set_intersection [ out,p)
(b , e , 2 , e2 , out , f) , [,) ,
[2,2 ) ,
f
p=set_difference [ out,p)
( , , 2 , 2 , ut) , [,) ,
[2,2 )
p=set_difference [ out,p)
(b , e , 2 , e2 , out , f) , [,) ,
[2,2 ) ,
f
p=set_symme tric_ [out,p)
difference , [,) ,
( , , 2 , 2 , out) [2,2 ) ,
p=set_symme tric_ [ out,p)
difference (b , e , 2 , , [,) ,
2 , out , f) [2,2 ) , ,
f

.5.6.
(heap) - ,
.

.
1 248


make_heap ( , ) ,

make_heap ( , , f) ,
,
f
push_heap ( , ) ( )
push_heap ( , , f) ( ) ,
f
pop_heap (b , e) ( )
pop_heap ( , , f) ( ) ,
f
sort_heap (b , e)
sort_heap ( , , f) ,
f


.
.

. 5 . 7.

. .
, , , . .


x=nextyermutation ( , )
[,)
x=nextyermutation ( , , f)
[,),
f
x=prev_permutation ( , )
[,)
x=prevyermutation ( , , f)
[,),
f

[,)
( ) . next
permutation fal s e ;
( ).
[.) (
.5 . 1 249

), p revyermutation
fal se:
( ).

.5.8. min max


.

min
x=max (a , b) -
x=max (a , b , f) - ;
f
x=max ( { elems } ) - { elems }
x=max ( { elems } , f) - { elems } ;

f
x=min (a , b) -
x=min (a , b , f) - ;
f
x=min ( { elems } ) - { elems }
x=min ( { elems } , f) - { elems } ;

f
pair ( x , y) =minmax (a , b) min ( , ) ( , )
pair (x , y) =minmax (a , b , f) x pao min (a , b , f) ( , , f)
pair ( x , y) =minmax ( { elems } ) min ( { elems } ) ( { elems } )
pair (, ) =minmax ( { elems } , f) min ( { elems } , f) max ( { elems } , f)
p-
-max_element (b , e)
[,)
p-
-max_element (b , e , f)
[,) ;
f
p-
-min_element (b , e)
[,)
p--min
_element (b , e , f)
[,) ;
f
lexicographical_ [,)< [2,2 ) ?
compare (b , e , b2 , e2 )
lexicographical_ [,)< [2,2 ) ,
compare (b , e , b2 , e2 , f)
f?
1 250

. 6 . STL

.

.6 . 1 .
,
, , .
, ,
. :
void f (vector<int>& vi)
(
fill_n (vi . eqin ( ) , 200 , 7 ) ; 11 7 vi [ O ] . . [ 1 99}

vi 200 , .
<i terator>
,
() .
. .


r=ack_inserter (c) * r=x c . push_ack (x)
r=front_inserter (c) * r=x . push_front ()
r=inserter ( c , p) * r=x . insert (, )

inserter ( c , p) ,
. ,

.
push k ( ) , push_
fron t ( ) insert ( ) , :
void q (vector<int>& vi )
(
fill_n (back_inserter (vi ) , 200 , 7 ) ; 11 2 0 0 ,
11 7 , vi

.6.2.

( ).
.
.6. SL 1 251

, (,
bool ) .
<functional>
.


p=equal_ to<T> { } ( , ) ==,
p=not_equal_to<T>{ } ( , ) ! =,
p=greater<T> { } ( , ) >,
p=less<T> { } ( , ) <,
p=greater_equal<T> { } ( , ) >=,
p=less_equal<T>{ } ( , ) <=,
p=logical_and<T> { } ( , ) &&,
p=logical_or<T> { } ( , ) 1 1 ,
p=logical_not<T> { } () ! ,

:
vector<int> v;
11 . . .
sort (v . beqin ( ) , v . end ( ) , qreater<int> { } ) ; 11 v
11

, logical and logical_or _

( & & 1 1 - ).


f=plus<T>{ } f ( , ) +,
f=minus<T> { } f ( , ) -,
f=multiplies<T> { } f ( , ) *,
f=divides<T>{ } f ( , ) /,
f=modulus<T> { } f ( , ) %,
f=negate<T> { } f () -,

f=ind (g , args) f () q ( , args) , args -

f=mem_fun (mf) f ( , args) p->mf ( args) , args -

function<F>f { g } f (args) g (args) , args -
, F g
f=notl (g) f () ! g ()
f=not2 (g) f ( , ) ! g (, )
1 252

, function ,
function<T>
, :
int f1 (doule) ;
function<int (doule ) > fct ( fl } ; 11 fl
int = fct (2 . 3 ) ; 11 f1 (2 . 3)
function<int (doule) > fun ; 11 fun
fun = fl ; 11 i n t (doul e)

.6.. pair tuple


<utili ty>
, pair.
template <typena.e Tl , typenae 2>
struct pair (
typedef Tl first_type ;
typedef 2 second_type ;
Tl first ;
2 second;

11
1;
template <typenae Tl , typenae 2>
constexpr pair<Tl , T2> .ake_pair (Tl , 2 ) (
return pair<Tl , T2>( x ,y} ;

make_pai r ( ) . ,
, .
pair<doule , error_indicator> my_fct (doule d)
(
errno = ; 11 11
11 . . . ,
11 d, . . .
error_indicator = errno ;
errno = ; 11 11
return .ake_pair ( x , ee) ;

,
:
pair<int, error_indicator> res = my_fct ( 123 . 456) ;
if (res . second==O ) (
11 . . . res . fi rs t . . .

else (
11 l!
.6. r SL 1 253

pair ,
.
, tuple
<tuple>:
template <typename . . . >
struct tuple {
// N :
explicit constexpr tuple ( const s& . . . ) ;

11 N :
template<typename . . . Atypes>
explicit constexpr tuple ( const Atypes&& . . . ) ;

// . . . . . .
1;

// N :
template <class ... s>
constexpr tuple<ypes . . . > ake_tuple (s&&... ) ;
tuple ,
, - (
. . . ) tuple
.

, pair, :
auto tO make_tuple ( ) ; 11
auto tl make_tuple ( 123 . 456) ; // d
auto t2 make_tuple ( 123 . 456 , ' ' ) ; 11 d
11 cha r
auto t = make_tuple ( 12 , ' ' , 11
strinq { "How? " } ) ; 11 i n t , cha r s t ring

tuple ,
first second .
get:
auto d = qet<O> ( tl ) ; 11 d
auto n = qet<O> ( t ) ; 11 int
auto = qet<l> (t3) ; 11 cha r
auto s = qet<2> ( t3 ) ; 11 s t ring

get .
, tuple .
.

.6.4.
<ini tializer l i s t>
initial i zer li st:
1 254

template<typename >
class initializer list
pulic :
initializer_list ( ) noexcept;
size_t size ( ) const noexcept; 11
const * eqin ( ) const noexcept ; 11
const * end ( ) const noexcept; 11
11 . . .
1;

{ }
, initializer_li s t<X> (
1 4.2. 1 , 1 8.2). , ini tializer l is t
( [ ] ) .

.6.5.
,
, .
( 1 9. 5) .
unique_ptr shared_ptr,
<memory>:
unique_ptr ( 1 9. 5.4) :
unique_ptr, .
unique_ptr.
shared_ptr :
shared_ptr, ,
shared_ptr.

unique_ptr<p> (n1}

unique_ptr up { } ; : up nullptr
unique_ptr up { } ; up
unique_ptr up { up2 } ; : up up2; up2
nullptr
up . -unique_ptr ( ) up
p=up . get ( ) - , up
p=up . release ( ) - , up; up
nullptr
up . reset () , up; up

up=make_unique<X> (args) up new<X> (args) (++ 1 4)

unique_ptr
, , ->. = <. , unique_ptr
.7. - 1 255

. ,
delete .

shared_ptr<p> ()

sharedytr sp { } ; : sp nullptr
sharedytr { } ;
sharedytr { sp2 } ; : sp, 2 sp2
shared_ptr sp { move ( sp2 ) } ; : sp 2; 2
nullptr
sp . sharedytr ( ) , sp,
eharedytr
sp = 2 : sp
,
, ; sp,
2 sp2
sp move ( sp2 ) : sp
, ,
;
sp2, sp2 nullptr
p=sp . get ( ) - , sp
n=sp . use_count ( ) sharedytr ,
sp?
sp . reset (p) sp ,
, ;

sp=make shared<X> (args) sp new<X> (args)

sharedytr
, * , ->. == <. , sharedytr
, ,
delete.
weakytr,
shared_ptr.

. 7. -
-
-
. -
<i s tream>, <os tream> .. (. . 1 . 1 ) .
o s tream , ,
().
1 256

" - "
( 1 2,34) ostream

1 23

istream ()
, .

"- "
( 1 2,34) istream

1 23

iostream - ,
istream, ostream. ,
, (streamuf).
iostream ,
,
.
.

-
cout ( , , )
cin ( , , )
cerr ( )

. 7 1 -
. .

istream (,
), s tring. ostream
(, ),
.7. - 1 257

s tring. -
.

ofstream

,
open ( ) .


stringstream (m) m
stringstream ( s , m) , s tring s,
m
fstream ( ) ,
fstream ( s , m) s m ,

fs . open ( s , m) s m
fs
fs . is_open ( ) fs?

-.
, .


ios_base : : (.. )
ios base : : ate "ate " "at end " (..
)
ios_base : : binary . ,

ios base : : in
ios base : : out
ios base : : trunc



, .
good ( ) . .
void my_code (ostream& os) ; 11 my_ code
11
ostringstream os ; 11 " " " "
1 258

ofstream of ( "my_file " ) ;


if ( ! of) error ( " W'l' ' my_file ' " ) ;
my_code (os) ; /! s t ring
my_code ( of) ; 11

(. 1 1 .3.)

. 7 .2.
ios tream .


good ( )
eof ( ) (" d of file ")
fail ( ) - (, ,
' )
'

bad ( ) - ( ,
)

s . exceptions ( ) , ,
ios tream , good ( )
(. 1 0.6).
.
good ( ) . -
.
iostream .
(). ios tream
good ( ) .
, .
for ( buf ; cin>>buf ; ) { // " "
//
// -

// , >>
// cin

.7 ..
< i s tream>,
s tring;
< s tring>.


in >> i n
getline ( in , s ) in s
. 7. - 1 259

,
i stream, ,
cin>>x>>y ; .


x=in . qet ( ) in

in . qet () in
in . qet (p , n) n in ,

in . qet (p , n , t) n in ,
; t
in . qetline (p , n) n in ,
; in
in . qetline (p , n , t) n in ,
; t ;
in
in . read (p , n) n in ,

x=in . qcount ( ) - ,
in
in . unqet ( ) ,
- ,
in . putack (x) "" ,

qet ( ) qetl ine ( ) (


), , ; qet
l ine ( ) ( t) ,
, qet ( ) . read (p , n )
. ,

, .

. 7 4
. .

<ostream>,
s trinq;
<s trinq>.


out << out
out . put (c) out
out . write (p , n) [ ] . . [n- 1 ] out
1 260

, ostream
,
. cout << << ; .

. 7 5
. .

- ,
. (.
< locale>) .
1 0 1 1 .
(, ).
.
.

. 7 6
. .

,
.
<ios>. < i s tream>,
<os tream>, <iostream> <iomanip> ( .
).

-
s << boolalpha true
false ( )
s << noboolalpha s . unsetf (ios_base : : boolalpha)
s << showbase oct hex
s << noshowbase s . unsetf (ios_base : : showbase)
s << showpoint
s << noshowpoint s . unsetf (ios_base : : showpoint)
s << showpos +
s << noshowpos s . unsetf ( ios_base : : showpos)
s >> skipws
s >> noskipws s . unsetf ( ios_base : : skipws)
s << uppercase ,
1 . 210 12, 1 . 210 Oxla2
s << nouppercase ,
s << internal ,
s << left
s << right
s << dec 1 0
s << hex 1 6
.. 1 261

-
s << oct 8
s << fixed dddd . dd
s << scientific d . dddd.Edd
s << defaultfloat , -

s << endl \n
' '

s << ends ' \ '


s << flush
s >> ws
s << resetiosflags (f) f
s << setiosflags (f) f
s << setase ()
s << setfill ()
s << setprecision (n) n
s << setw (n) n


s . .
cout << 1234 << ' , ' << hex << 1234 << ' , ' << oct << 1234 << endl ;

:
1234 , 4d2 , 2322

.
cout << ' ( ' << setw ( 4 ) << setfill ( ' # ' ) << 12
<< " ) ( " << 12 << " ) \n" ;


(##12) ( 12)


, :
b . setf ( ios_base : : fmtflags ( O ) , ios_base : : floatfield)

(. 1 1 .)

. 8.

( <cctype>) .
( <s tring>) . (
<regex>) - ( <cs tring>) .
1 262

.. 1 .
.
.


isspace () ( 1 1 ,
1 \ t , 1 \n 1 .. ) ?
1

isalpha () ( ' ' " ' z ' , ' ' " ' z ' )? ( :
' ' . )
i sdigi t () ( 1 " 1 9 1 ) ?
isxdigi t () (.. -
' ' .. ' f ' ' ' .. ' F ' ) ?
isupper () ?
islower () ?
isalnum () ?
i scntrl () (ASCl l 0 " 3 1 1 2 7)?
ispunct () , ,
?
i sprint () (..
ASCll ' ' ' ' ) ?
isgraph () , (isalpha ( )
i sdigi t ( ) ispunct ( ) )? ( : . )

,
.


toupper (c)
tolower (c)

, Unicode,
,
.

..2.
s tring
as i c s tring char;
_

. s tring - char.


s=s2 s2 s; s2
string -
.. 1 263


s+=x s; ,
string -
s [i]
s+s2 ; ,
s, s2
s==s2 ; s s2 -
,
s ! =s2 ; s s2 -
,
s<s2 ; s s2
-,
s<=s2 ; s s2
-,
s>s2 ; s s2
-,
s>=s2 ; s s2
-,
s . size ( ) s
s . length ( ) s
s . c_str ( ) s - (
)
s . begin ( ) ,
s . end ( ) , ,
s
s . insert (pos , x) s [pos ] ;
s tring -
s . append (x) s;
s tring -
s . erase (pos) s, s [pos ] .
s pos.
s . erase (pos , n ) n s, s [pos ] .
s max (pos , size-n)
s . push_ack (c)
pos=s . find (x) s; , s tring
-; pos
-

string : : npos (,
s)
in>>s s in
1 264

. . .

<regex>. .
(searching) , ,
( ) ;
regex search ( ) .
_

(matching) (
); regex_ma tch ( ) .
(replacement of matches):
regex replace ( ) : ,
_

.
regex search ( ) regex_match ( )
_

. . smatch.
reqex row ( " " [\\w ] + ( \\d+ ) ( \ \d+) ( \ \d+) $ " ) ; 11

while (qetline (in, line) ) ( 11


smatch matches ;
if ( ! reqex_match ( line , matches , row) )
rrr ( " " , lineno ) ;

11 :
int fieldl = fro_strinq<int> (matches [ l ] ) ;
int field2 fro_strinq<int> (matches [2 ] ) ;
int field fro_strinq<int> (matches [ ] ) ;
11 . . .

.
(. 23).




(
(
)
'
*
+
? { )
{)

;
$
.8. 1 265


{n} n
{n, } n
{ n , m} n m
* , .. { , }
+ , . . { 1 , }
? ( ) , .. { , 1 }


alnwn -
alpha
lank ,
cntrl
d
digit
graph
lower
print
punct
s
space
upper
w (- )
xdigit

1JJ.


\d [ [ : digit : ] ]
\1 [ [ : lower : ] ]
\s ( , .. ) [ [ : space : ] ]
\u [ [ : upper : ] ]
\w , (_) [ [ : alnwn: ] ]
\D \d [ [ : digi t : ] ]
\L \1 [ A [ : lower : ] ]
\S \ s [ [ : space : ] ]
\U \u [ [ : upper : ] ]
\W \w [ [ : alnwn : ] ]
1 266

. 9.
++
(, ..)
.

. 9 1
. .

++ ,

. ..
<limi ts> numeric_
limi ts<T> . ,
numeric_limits<X>
. .
class nueric_limits<float> {
pulic :
static const bool i s_specialized = true ;
static constexpr int radix = 2 ; //
11 ( )
static constexpr int diqits = 2 4 ; //
//
static constexpr int diqitslO = 6 ; //
11
static constexpr bool is_siqned = true ;
static constexpr bool is_inteqer = false ;
static constexpr bool is_exact = false ;

// :
static constexpr float min ( ) { return l . 17549435E-38F ;
static constexpr float max ( ) { return 3 . 40282347E+38F ;
static cons texpr float lowes t ( ) { return - 3 . 40282347E+38F ;

static constexpr float epsilon ( ) { return 1 . 1 92 0 92 90E-0 7 F ;


static constexpr float round_error ( ) { return O . SF ; }

static constexpr float infinity ( )


{ return / * */ ;
static constexpr float quiet_NaN ( )
{ return / * */ ;
static constexpr float siqnalinq_NaN ( )
{ return / * */;
static constexpr float denorm_min ( ) { return min ( ) ; }

static constexpr int min_exponent = -125 ;


static constexpr int min_exponentlO = -37 ;
static constexpr int max_exponent = +128 ;
static cons texpr int max_exponentlO = +38 ;
. 9. 1 267

static constexpr bool has_infinity = true ;


static constexpr bool has_quiet_NaN = true ;
static constexpr bool has signalinq NaN = true ;
static constexpr float_denor:._style has_denor:.
= denor:._asent ;
static constexpr bool has_denor:._loss = false ;

11 IEC-5 5 9 :
static constexpr bool is iec559 = true ;
static constexpr bool is bounded = true ;
static constexpr bool is modulo = false ;
static constexpr bool traps = true ;
static constexpr bool tinyness_before = true ;

static constexpr float_round_style round_style


round_to_nearest;
};
<limi ts . h> <float . h> ,
,
( ).


R BIT char ( 8)
R MIN char
R char ( 1 27, char
, 2 55, char )
INT MIN int
INT int

. 9 .2.

( <cmath> <complex>).


s ()
ceil () >=
floor (x) <=
round (x) (0 . 5 )
sqrt (x) ;
cos ()
sin (x)
tan ()
acos () ;
1 268


asin () ; ,
atan ()
sinh ()
cosh ()
tanh ()
() ; (2.71828 1 828" . )
log () ( ) ;
loglO ()

. float,
doule, long doue complex.
.

,
errno.

. 9 ..

complex<float>, complex<doue> complex<long doue>. complex
<Scalar>, Scalar ,
-

, , ,
.
template<typename Scalar> class complex {
// - ,
11 - .
Scalar re , im;
pulic :
constexpr complex ( const Scalar & r , const Scalar & i )
: re { r } , im( i } { }
constexpr complex ( const Scalar&r) : re { r } , im{ Scalar { } } { }
constexpr complex ( ) : re { Scalar{ } } , im{ Scalar { } } { }
Scalar real ( ) { return re ; } //
Scalar imaq ( ) { return im; } //
11 opera tors : += -= *= /=
};
, <complex>
.


zl+z2
z l-z2
.9. 1 269


z l *z2
zl/z2
zl==z2
zl ! =z2
norm ( z ) s ( z )
conj (z) : z { re , im} , conj ( z )
- -

{ re , - im}
polar (x , y)
(rho, theta)
real ( z )
imag (z)
s ( z ) rho
arg ( z ) theta
out < < z
in >> z

,
(. .9.2). : complex
< % (. 24.9) .

. 9 4 valarray
. .

valarray
-

: (
atrix 24) (slices) (strides).

. 9 .5.
<nwneric>

.


x=accwnula te ( , , i) - i [,)
x=accwnulate ( , , i , f) , + f
x=inner - [,}
product (b , e , 2 , i ) [2,2+ (-) ), .. i ( *pl) * ( *2 )
pl [,)
2 [2,2+ (-) )
x=inner__product inner __product, + *
( , , 2 , i , f , f2 ) f f2
1 2 70


p-
--partial _ i [out,p)
sum (b , e , out) o . . i [,)
p-
--partial_ partial_sum. + f
sum (b , e , out , f)
p=adjacent_ i [out,p) * (b+i) -
difference (b , e , out) * (b+i-1 ) i>O; ->, *out *
p=adjacent_ adj acent_difference, -
difference ( , , out , f) f
i toa ( , , v) [, )
++v

:
vector<int> v (lOO) ;
iota (v . eqin ( ) , v . end ( ) , ) ; 11 v== { l , 2 , 3 , 4 , 5 . . . 1 0 0 }

.9.6.
<random>
(24. 7).
default_random_engine,
.
.


uniform int distribution<int> { low , high } [ low,high]
uniform_real_dis tribution<int>{ low , high } [ low,high)
exponential_distribution<doule>{ lamda } [ 0,)
bernoulli_distribution{ p } [ true,false]
normal_distribution<doule>{median , spread} (-00,00)

r, :
uniform real distribution<> dist;
default_random_enqine enqn ;
for (int i = ; i<lO ; ++i)
cout << dist (enqn) << ;

. 1 .
<chrono>
.
(clock ticks) now ( ) .

:
.1 1 . 1 271

system_clock:
steady_clock: ,
now ( ) c . now ( ) <=c . now ( ) ,

high_resolution_clock: ,


, seconds, milliseconds nanoseconds,
duration_cast<> ( ) :
auto t = steady_clock : : now ( ) ;
// . . . - . . .
auto d = steady_clock : : now ( ) -t ; //
11 d
cout << " "
<< duration_cast<milliseconds> (d) . count ( ) << " ms " ;


, . (. 26.6. 1 .)

. 1 1 .

++ .
,

, .
.
- .
.
.
.
.
,
;
, Kernighan and Ritchie, h Programming Lan
guage (K&R).

.1 1 . 1 .
-. <cstdio>,
. (FILE * )
, . stdin, stdout stderr.
1 272

;
.


f=fopen ( s , m) m
x=fclose (f) f;

"" ,
, , .


11 r 11
11 w 11 ( )
11 11 ( )
11 r+ 11
11 w+ " ( )
" 11 ;

(, ,
) . ,
fopen ( " foo " , " rb " ) foo
. -
s tdio ios tream (. . 7. 1 ) .

. 1 1 .2. print f ( )

-.
ios tream,
. printf ( )
( ++)
.

printf
n=printf ( fmt , args) fmt stdout,
args
n=fprintf (f , fmt , args) fmt f,
args
n=sprintf ( s , fmt , args) fmt - s,
args

n ,
-

- . .
printf ( ) , .
.1 1 . 1 273

printf ( ) :
int printf ( const char* forat . . . ) ;
. - ( ,
), ,
.
,
% ( ) %d ( ). .
int = 5 ;
cont char* = "asdf " ;
rintf ( " 1 %d 1 , ' %s ' \n" , x ,p) ;
, %, .
% (
%d ), %
(
%s ) .. ,
printf ( ) :
' 5 ' , ' adf '
.
%
, , .
.
printf ( " ' % s ' , ' %d ' \n" , , ) ; 11 !


( ).
% . .

,

+ , , ,
, + -
, ,
. -
,
# , ,
, ;
;
, -
d , .
, ,
( ,
1 274

), . ,

,

dd , , ..
f,
,
* , * .

h h, ,
d, , u short int
1 ( 1), ,
d, , u long int
L L, ,
, , g, G f long doule
% , %;
, . -

d
i



f floa t doule, [ - J ddd .
ddd. .
. ,
; , # , ,

float doule,
[ - ] d . ddde+dd [ - ] d . ddde-dd,
,
. . ,
; ,
# , ,
, ,

g float doule d, f
,

.1 1 . 1 2 75

G , g,

.
s { ),
,
, . ,
,
.

u
n ,
printf , fprintf sprintf ; int,
-


;
. .
++,

, complex, vector s tring.
stdout cout.
s tdin cin.
stderr
cerr. -
++ , -
, ++ .
,
cout stdout (
. ++).
. ,
,
stdio iostream ios_
s : : sync_wi th_stdio ( false)
-.
cstdio scanf ( ) , .. .
printf ( ) . .
int ;
char s [buf_size] ;
int i = scanf ( "x ' %d ' , s 1 %s 1 \n 11 , &x , s ) ;

scanf ( ) ,
. , s .
1 276

.
. .
" ' 123 ' , s ' s tring ' \n"
123 " string" .
. s. scanf ( )
. (i )
- (
2); EOF.
(, ,
" s tring " ?) .
scanf ( ) .
.
,
s tdio? : "
gets ( ) . "

// :
char s [uf_size] ;
char* = gets ( s ) ; 11 s

p=gets ( s ) s .
,
s .
.
NULL (.. );
s . gets ( s )
scanf ( " % s " , s ) )!
: , (
s ) ,
. , .
sprintf ( ) .

stdio
.

stdio
x=getc (st) st;
; ,
x==EOF
x=putc ( , st) st;
; , x==EOF
x=getchar ( ) stdin;
; ,
x==EOF
. 1 1 . 1 277

stdio
x=putchar (c) s tdout;
; , x==EOF
x=ungetc ( , st) s t;
; , x==EOF

,
int ( char,
EOF) .
.
int ch ; /* cha r ch ; */
while ( ( ch=qetchar ( ) ) ! =EOF) ( / * - */ }

ungetc ( ) .
, . .
s tdio,
, K&R.

. 1 1 .. -
- char,
. .
<cs tring> ( < s tring . h>; : < s tring>! )
<cstdl ib>. -
char* ( con s t char* ,
).

-
x=strlen ( s ) ( )
p=s trcpy ( s , s2 ) s2 s; [ s,s+ n) [ s2,s2 + n)
; p=s;
p=strcat ( s , s2 ) s2 s; p=s; -

x=strcmp ( s , s2 ) : s<s2, -

; s==s2, ==; s>s2,


-


p=strncpy ( s , s2 , n) strcpy; n ;
; p=s
p=strncat ( s , s2 , n) strcat; n ;
; p=s
x=strncmp ( s , s2 , n) strcmp; n
p=strchr ( s , )
s
1 278

-
p=strrchr ( s , c)
s
p=strstr ( s , s2 ) s,
, s2
p=strpbrk ( s , s2 ) s,
s2
x=atof ( s) doule s
x=atoi ( s) int s
x=atol ( s) long int s
x=strtod (s , p) doule s;
, doule
x=strtol ( s , p) long int s;
,
x=strtoul (s , p) unsigned long int s;
,

, ++ s trchr ( ) s trstr ( )
, (
con s t char* char* , ) (.
27.5).

,
11 12 4 11 11 1 4 " . ,
.

:
int = atoi ( " fortytwo " ) ; / * * /

.1 1 .4.
" " (
) void* ( con s t void*
. ).


q=memcpy ( , 2 , n) n ,
2, , (
s trcpy) ; [p,p+n) [p2,p2+n)
; q=p
q=memmove ( , 2 , n) n ,
2, , ; q=p;

.1 1 . 1 279


x=memcmp ( , 2 , n) n ,
2, n ,
( strcmp)
q=memchr ( , , n) ( unsigned char)
[ J " [n-1 J q ;
, q=O
q=memset ( , , n) ( unsigned char)
[ ] " [n-1 J ; q=p
p=calloc (n , s ) n*s ,
; n*s , =
p=malloc ( s ) s ;
s , =
q=realloc ( , s ) s ;
malloc ( ) calloc ( ) ;
, ,
; , ,
, ; s
, q=O
free () , ,
malloc ( ) , calloc ( ) realloc ( )

mal l o c ( ) .
free ( ) . ,
. , memse t ( )
. .
, mem,
<cstring>. -
<cstdlib>.
(. 27.5.2.)

.1 1 .5.
<ctime> ,
.

,
clock t (
)
time t ( )
tm ( 1 900- ) .

tm :
1 280

struct tm {
int tm_sec ; 11 [ 0 : 61 ) ; 60 61
// " "
int tm_in ; // [ 0 , 5 9 )
int tm_hour ; // [ , 2 3 }
int tm_day ; // [ 1 , 31 )
int tm_mon ; 11 [ 0 , 1 1 ) ; -
11 ( : [ 1 , 1 2) ! )
int tm_year ; 11 1 900 - ; - 1 90 0 - ,
11 1 02 - 2002-
int tm_wday ; // , [ 0 , 6) ;
// -
int tm_yday ; 11 1 [ , 3 65 ) ; - 1
int tm_isdst; 11
};
.
clock t clock ( ) ; 11
11
time t time (time_t* pt) ; 11
doue difftime (time_t t2 ,
time_t tl) ; 11 t 2 - t l
tm* localtime ( const time_t* pt) ; // *p t
tm* qmtime (const time t* pt) ; // (GMT) tm
// * t ;

time_t Illktime ( tm ptm) ; // t ime_ t *p tm


// t ime_ t ( -1 )

char* asctime (const tm* ptm) ; // *p tm


11 -
char ctime (const time_t t) { return asctime ( localtime (t) ) ;

asctime ( ) :
" Sun Sep 1 6 0 1 : 03 : 52 1 973\n " .
tm
strftime ( ) .
, .

. 1 1 .6.
<cstdlib> . .

stdlib
aort ( )
exi t (n) n; n==O

.12. 1 281

stdlib
system (s) , -
( )
qsort ( , n , s , cmp) ,
n s,
cmp
bsearch (k, , n , s , cmp) k ,
, n s,
cmp

(cmp), qsort ( ) s
arch ( ) ,
int ( *cmp) ( const void* , const void* q) ;
.
:
, :
, * , *q;
, * *q;
, * , *q.
, exi t ( ) abort ( ) .
,
(. .4.2), .

K&R ++.

. 1 2.
. , ,
. ,
, ,
++ .
. .
r .
.
.
.
L.
.
.
1 282

.
.
" .
.
. ,
.
.
V i sual Stud i o
# , ....

ak. ' ' .... t,.

ak. .
- 2'fe-. ... (/-.'l.t. .//-)

.
,
,
Microsoft Visual Studio.
1 284 Visual Studio

8. 1 . 8.3.4.
8.2. Visual Studio .3.5.

8.3.
.3.6.
.3. 1 .
.3.7.
.3.2.
std_lib_facili ties . h 8.4.
.3.3.
+ +

. 1 .
. -
(. - .
, - ).
(
- ,
++) () .
,
(. Windows Linux)
.
. ,
, .
,
- Microsoft Visual Studio.
, .
. ,
. .
, . ,
, .

. 2 . Visuai Studio
Visual Studio - (IDE -
interactive development environment) Windows.
,

Visual ++ Express - www .
microsoft . com/express/download. , ,
Visual Studio 20 1 .
.
.. 1 285

..
.
l . .
2. ++.
3. .
4. .
5. .
6. .

.3.1 .
Visual Studio ,
(
) Windows.
1 . Visual ++, Microsoft Visual Studio
20 1 0 qqisft Visual Studio
201 0qMicrosoft Visual Studio 2 0 1 0.
2. qq . . .
3 . Visual ++.
4. Wi32 .
5. , Hello ,
World !
6. .
C:\Documets d Settigs\Your m\ Documets\Visual Studio 201 \
Projects.
7. .
8. Wi32.
9. .
1 . .
1 1 . .
.

.3 . 2. s td_l ib_faci l i ties h .


std_lib_facil i ties . h.
www . stroustrup . com/Programming/std_lib_facili ties . h. -
1 286 Visual Studio

, .3. 1 . 6. (:
, L-.)
,
#include " . . / . . /std_lib_facilities . h"
./ . . /" .
.

C:\Documents and Settings\Your name\My Documents\Visual Studio 20 1 \


Projects, .
,
.

... ++

( ).
1 .
( ).
. Visual ++ .
2. ++ ( .).
(Hello , World ! )
.
, .
.

..4.
.
,
.

..5.
.
, q

.
.
,
: : l , : , : , :
.
, ,
.
.4. 1 287

,
( ).
.
, .3.6.

..6.
,
q .

.. 7.
, .
,
.

8.4.

. , .
,
" ".
Visual Studio.
F LT K

, ,
FLT .
1 290 FLK

. 1 .
.2. FLK
.3. FLK
.4. FLK
Visual Studio
.5. ,

[ 1 .
FLTK (Fast Light Tool it)
,
,
, .
. .
FLTK Microsoft Visual Studio,

. - (
).
( .) . .
. ISO
++. .
. ,
FLTK - ,
,
.
, .
.
,
, . .
FLTK Visual Studio
.

[2 . FLTK
, .
FLTK (. .5). ,
.
.. FLTK 1 291

1 . - http : //fltk . org. ( -


, -,
(www . stroustrup . com/Programmi ng/FLTK).
2. Download .
3. FLTK 1 . 1 .
Show Download Locations.
4. , ,
. zip.
zip. ,
.
.
, Windows
WinZip 7-Zip.

.. FLTK
:
. , -
( ) (
; ). ,
.
1 . , f1 tk-
1 . 1 ? . Visual ++ (, vc2005 vcnet)
.

fl tk . dsw.
, Yes to Al l .
2. .
.
(static link libraries).
FLTK
. Visual Studio.
3. FLTK lib.
( )
. lib, README.lib (
) : \Program. Files\Microsoft Visual Studio\Vc\lib.
4. FLTK
FL C : \Program Files\icrosoft Visual Studio\Vc\
include.
,
: \Program Files \
icrosoft Visual Studio\Vc\lib : \Program Files\ icrosoft Visual
1 292 FLK

Studio\Vc\include. ,
Visual Studio. ,
, .

.4. FLTK Visuai Studio


1 . Visual Studio,
: . Wi32.
Wi 32 . , :
.
.
2. Visual Studio,
q.
3. .
.
, , :
fltkd . lib wsock32 . lib comctl32 . lib fltkjpeqd . lib fl tkimaqesd . lib
( ,
.)
:
libcd . lib
4. ( ,
/MDd .)
/++.
. , .

DLL Debug DLL (/M Dd). ,
.

.5. ,

.

. .
#include <FL/Fl . h>
#include <FL/Fl_Box . h>
#include <FL/Fl_Window . h>
int main ( )
{
Fl_Window window (200 , 200 , "Window title" ) ;
Fl_Box { , 0 , 200 , 200 , " , I mean , Hello , World ! " ) ;
window . show { ) ;
return Fl : : run ( ) ;
[ 5 . . 1 293

- . .
, ,
. l ib , , ,
- .
. 3,
( lib) .
.

, ,
. h, , ,
. . 3,
( h)
.

.
,
,
.
, . . .



), /,., ' ,

, 1.
-
,L(, (l;f! F)


, Window,
Widget Vector ref . 1 6
_


,
.
1 296

. 1 .
.2. Widget
.. Window
.4. Vector_ref
.5. :
Widget

. 1 .
:
void Simple_window : : c_next (Address , Addre ss addr)
11 Simple_ window: : nex t () ,
// addr

reference_to<Simple_window> ( addr) . next ( ) ;

1 7, ,
Address void* . , reference
to<Simple_window> ( addr) -
Simple_window addr, void*.
, "
" "", 1 7.
.
. 1 7, ++
. .
typedef void* Addre ss ; // Address - voi d *

, Address void*.
, Address, .
, . void* -
.
, cb_next ( ) void*
addr - - -
Simple_window&:
reference_to<Simple_window> ( addr)

reference_to ( . 1 3).
template<typename W>
W& reference_to (Addre ss pw)
11 r W
.2. Widget 1 297

return *static_cast<W*>(pw) ;

,
. void*
Simple_window&. static_cast 1 7.8.

, addr Simple_window,
.
. . . ,
FLTK , .
. FLTK, ,
reference_to, ".
,
.
Simple_window,
- Simple_window.
( 1 6.3).
void Simple_window : : c_next (Addre ss , Addre ss pw)
// Simple_ window: : nex t () ,
11 pw

reference_to<Simple_window> (pw) . next ( ) ;

next ( ) ,
,
- next ( ) .

.2. Widget
Widget .
class Widqet {
// Wi dge t Fl_widge t ,
-

// Fl_widge t .
/! FLTK
pulic :
Widqet (Point , int w, int h , const string& , Callback )
: loc (xy) , width (w) , height (h) , lael ( s) , do_it (c)
{}
virtual Widget ( ) { } //

virtual void move (int dx , int dy)


{ hide ( ) ; pw->position (loc . x+=dx , loc . y+=dy) ; show ( ) ; }
1 298

virtual void hide ( ) pw->hide ( ) ;


virtual void show ( ) pw->show ( ) ;

virtual void attach (Window&) = ; //


// Widge t

Point loc ;
int width ;
int height;
string lael ;

Callback do_it;

protected :

Window* own ; // Wi dge t


// Window
Fl_Widget* pw ; // Widge t " "
// "" Fl_Wi dge t
};
, Widget ""
FLTK Window, .
, , ,
Widget
Window.
. (, .)
Widget (loc) .

(width height), (lael). ,


(do i t), ..
_

Widget . move ( ) ,
show ( ) , hide ( ) t tach ( ) .
Widget .
, .
. ,
"" .
Widget
,
( . 1 7. 5. 2).

.. Window
, ?
8.5.6. ,
,
.
.. Window 1 299


- Window. ,
, FLTK
.
Window . h.
class Window : puic Fl_Window
pulic :
// :
Window (int w, int h , const string& title) ;
// :
Window (Point , int w, int h, const string& title) ;

virtual Window ( )

int _m.ax ( ) const return w ;


int y_m&x ( ) const return h ;

void resize ( int ww , int hh) { w=ww, h=hh ; size (ww,hh) ;

void set_lael ( const string& s) { lael ( s . c_str ( ) ) ;

void attach ( Shape& s ) shapes . push_back ( &s) ;

void attach (Widget&) ;


void detach ( Shape& s ) ; // s
void detach (Widget& w) ; 11 w
/! ( )

void put_on_top ( Shape& ) ; 11


11
protected :
void draw ( ) ;
private :
vector<Shape*> shapes ; 11 ,
int w , h ; //

void ini t ( ) ;
};
, ,
attach ( ) , shapes , Window
.
detach ( ) ,
. , ;
Window . Window : : attach ( )
, .
, attach ( ) ;
detach ( ) . Window . , .
1 300

void Window: : detach ( Shape& s )


1 1 ,
11

for (vector<Shape*> : : size_type i = shapes . size ( ) ; O<i ; --i)


if ( shapes [i-l] ==&s)
shapes . erase ( shapes . begin ( ) + (i-1) ) ;

- erase ( ) ("") ,
( 20. 7. 1 ) .
Window , -
( 1 7. 5 . 2).

.4. Vector ref


, Vector_ref .
.
Vector_ref
, , ,
(, -
, ).
Vector_ref -
, ,
new, Vector_ref.
Vector_ref ,
, . ,
Vector_ref<Shape> Circle,
.
template<class > class Vector_ref
vector<T*> v ;
vector<T*> owned;
pulic :
Vector_ref ( ) { }
Vector_ref (T* , * = , * , * d = ) ;

vector_ref ( )
for (int i=O ; i<owned . size ( ) ; ++i)
delete owned [ i ] ;

void push_back (T& s } v . push_back ( &s ) ; }


void push_back (T* ) v . push_back (p) ; owned . push_back (p) ;

& operator [ ] (int i ) return *v [ i ] ; }


.5. : Widget 1 301

const & operator [ ] ( int i) const { return *v [ i ] ; )

int size ( ) const { return v . size ( ) ; }


};

Vector ref ,
.

.5. : Widget
.
Widget/Window.
. ,
- .
, .
, .
#include " . . /GUI . h"
usinq namespace Graph_lib ;

class W7 : pulic Window {


// ,
// : / , ,
// /
pulic :
W7 (int w, int h , const strinq& t) ;

Button* pl ; // /
Button* 2 ;
bool sh_left ;

Button* mvp ; //
bool m.v_left ;

Button* cdp ; // /
bool cd_left ;

Button* adpl ; // /
Button* adp2 ;
bool ad_left ;

void sh ( ) ; 11
void m.v ( ) ;
void cd ( ) ;
void ad ( ) ;
static void c_sh (Addres s , Address addr) //
{ reference_to<W7> ( addr) . sh ( ) ; }
static void c_mv (Address , Addre ss addr)
{ reference_to<W7 > ( addr) . mv ( ) ; }
static void c_cd (Address , Addr ess addr)
1 302

{ reference_to<W7> (addr) . cd ( ) ;
static void c_ad (Address , Address addr)
{ reference_to<W7> (addr) . ad ( ) ; }
};

W7 ( Window 7)
: .
W7 : :W7 ( int w , int h , const strinq& t)
: Window (w , h , t) ,
sh_left{ true } , mv_left{ true } , cd_left{ true } , ad_left{ true }

pl new Button { Point { l00 , 100 } , 50 , 20 , " show" , c_sh } ;


2 = new Button { Point { 200 , 100 } , 50 , 20 , "hide " , c_sh } ;

mvp new Button { Point{ l00 , 20 0 } , 50 , 20 , "move " , c_mv } ;

cdp new Button { Point { l00 , 300 } , 50 , 20 , " create " , c_cd} ;

adpl new Button( Point{ l00 , 400 } , 50 , 20 , "activate" , c_ad} ;


adp2 new Button{ Point( 200 , 400 } , B0 , 20 , "deactivate " , c_ad} ;

attach ( *pl) ;
attach ( *p2 ) ;
attach ( *mvp) ;
attach ( *cclp) ;
p2->hide ( ) ;
attach ( *aclpl } ;

.
, .
.
void W7 : : sh ( ) // ,
{
if (sh_left) (
pl->hide ( ) ;
p2->show ( ) ;

else (
pl->show ( ) ;
p2->hide ( ) ;

sh lef t ! sh_left ;

void W7 : : mv ( ) //
(
if (m.v_left) {
mvp->move ( l 0 0 , 0 ) ;
. 5 . : Widget 1 303

else {
mvp->ove (-100 , 0 ) ;

mv left ! mv_left;

void W7 : : cd ( ) //
{
cdp->hide ( ) ;
delete cdp ;
string l = "create" ;
int = 100 ;
if ( cd_left)
l = "delete" ;
= 200 ;

cdp new Button{ Point { x , 300 } , 5 0 , 20 , l , _d} ;


attach ( *cdp) ;
cd left ! cd_left;

void W7 : : ad ( ) //
//

if (ad_left)
detach ( *adpl ) ;
attach ( *adp2 ) ;

else {
detach ( *adp2 ) ;
attach ( *adpl) ;

ad lef t ! ad_left;

int main ( )

W7 w{ 400 , 500 , "move" } ;


return gui_main ( ) ;


, .

/ - , .
,
,
. ""
.
, ++, -
www . stroustrup . com/glossary . html . ,
( ).
, .
( ).
;
, (abstract)
, ,
.
I (abstract class). ,
;
.
,
.
(abstraction). ,
() (, );
.
(address). ,
.
r (algorithm). ;
,
.
r (argument). , ( ).
.
1 306

(base class). ,
.
.
(byte). .
, .
(infinite recurslon). ,
, ,
.
,
.
(infinite loop). ,
. . (iteration).
(library). , , . . ,
().
.
(it) .
. 1 .
(input). , (,
, ).
: (virtual function). -,
.
(owner) . . .
(lifetime). .
, (
,
- ).
(output). , (
, ,
).
m (executae). ,
.
(computation) . .
.
(data). , .
(handle). ,
- -. . &1D1,
. .
(destructor). ,
(, ).
.
1 307

(bug). .
(range). , ,
. , /0,5)
, 1 . 2. 3 4.
1' (unit). 1 ) , (
, ). 2) (.. ) .
r (header). , ,
.
(value). ,
.
(ideal). , .

.
RAII (Resource Acquisitlon ls lnitialization).
, .
(mutae) . ,
, ,
.
(invariant). ,
( ) ;
( )
.
(initialize). ()
.
JCacyJIJIJI (encapsulation).
.
(interface). ,
(, ).
I (source code). , (
) .
(source file). , .
(iterator). ,
.
(iteratlon). : .
.
(class). ,
-. - -.
(code). ;
, .
1 308

JI (compiler). ,
.
(trade-off).
.
(concrete class). ,
.
(constant). , (
); .
(constructor). , (
) .
, (
).
(container). , ( ).
I (concept). 1) , . 2) ,
.
(). ,
, . .
.
(correctness). ( )
().
. ,

. , ,
,
.
(literal). , ,
1 2 , "".
(array). ,
, [O,max) .
r (feature creep).
"
".
(uninitialized). ()
.
(scope). ( ).
.
(generic programming).
.
.
1 309

, .
++
.
(object). 1) .
- . 2) .
-1D1 (object-oriented prog
rammlng). .
.
(object code) . ,
, ,
. .
(object file). . .
'Ji (declaration). .
r (rounding).
.
J1 (operation). , - ,
.
1D1 (definition). ,
.
: . .
(debugging). :
. .
m (error).
(
) .
.
8.' (paradigm).
. () ,
. .
(parameter).
.
.
(overload).
, ().
(override). .

:
.
(variale) . :
, .
1 310

(move). .
, . "
". . .
(overflow). ,
.
(subtype). ; ,
. . .
(sequence). .
.
(post-condition) . ,
. . .
(pre-condition). .
. .
(approximation). (. ).
( ).
.
(application). .
.
r (programming).
.
r (software).
;
"r".
r (program code) (.
) . . .
(design). ,
, .
(derived class). .
.
(pseudo code). .
. .
(alias). ;
. .
(implementation) . 1 ) .
. 2) , .
r (regular expression).
.
(linker) . .
.
1 311

(recursion). ; . .
(resource) . ,
, , .
. , .
(system). 1 )
. 2)
" . ..
.
(word). .
.
(complexity) .
,
.
,
.
(hiding). ,
. , ()
, ()
,
.
(information hiding). ,
,

.
(state). .
(specification). ,
.
(reference). 1 ) ,
. 2) , .
(standard). -,
.
(style). .
.
,
.
(cost). (, ,
).
.
.
(strlng). .
1 31 2

(supertype). ; ,
.
(use case). ( , )
,
.
(testing). .
(type) . ,
.
(requirement). 1 )
. 2)
.
(pointer). 1 ) ,
, . 2) .
.
(truncation).
,
.
(assertion). , ,
,
.
(file). ,
.
(function). .
() ;
.
(integer). ,
42 -99.
(loop). . ; ++
, , for whlle.
(floating-point number).
, 7.93 1 0. 78-3.
8.JI8JI (pure vitual function).
, .
(template). ( ).
;
++,
.
r (programming language).
.

1 . Aho, Alfred V" Monlca S. Lam, Ravl Sethl, and Jeffrey D. Ullman. Compilers: Prin
ciples, Techiqes, and Tools. Second Editio ( "
"). Addison-Wesley, 2006. ISBN 032 1 4868 1 1 . ( - .
, . , , . . r: ,
11. - : "", 2008.)

2 . Andrews, Mike, and James . Whittaker. to Break Software: Functioal and


Secrity Testing of Web Applications and Web Services. Addlson-Wesley. 2006. ISBN
032 1 369440.

3. Bergln, Thomas J . , and RJchard G. Gibson, eds. History of Programming Laguages,


Volme 2. Addison-Wesley, 1 996. ISBN 020 1 89502 1 .

4 . Blanchette, Jasmln, and Mark Summerfleld. ++ GUI Programming ith Qt 4.


Prentice Hall, 2006. ISBN 1 3 1 872494.
5. Boost.org. " Repository for Librarles Meant to Work Well wlth the ++ Standard
Library." www . oos t. orq.
6. , Russ. "Regular Expresslon Matchlng Simple and Fast (but Is Slow in
Java, Perl, , Python, Ruby, " . ) . http : //swtch . co/rsc/reqexp/reqexpl . html.
"

7. dmoz.org. http : //dmoz . orq/Coputers/Proqrallllllinq/Lanquaqes.


8. Freeman, . L. , and . Phillips. Parallel Numerical Algorithms. Prentice Hall, 1 992.
ISBN 0 1 365 1 5975.
9. Gamma, Erich, RJchard Helm, Ralph Johnson, and John Vllssides. Desig Patterns:
Elemets qf Reusae Object-Orieted Software. Addison-Wesley, 1 994. ISBN
020 1 6336 1 2 .

1 0. Goldthwalte, Lols, ed. Technical Report ++ Perjomnce. ISO/IEC PDTR 1 80 1 5 .


www . stroustrup . com/perforanceR . pdf.
1 1 . Gullberg, Jan. Mathematics - From the Birth oj Numers. W. W. Norton, 1 996. ISBN
039304002.

1 2. Hailpern, Brent, and Barbara G. Ryder, eds. Proceedings oj the Third SIGPLAN
Cojerence the History qf Programming Laguages (L-). San Diego. .
2007. http : / /portal . ac . orq/toc . cf?id=1238844.
1 3. ISO /lEC 9899:20 1 1 . Programming Lagages - . h standard.
1 4 . ISO/IEC 1 4882 : 20 1 1 . Programming Laguages - ++. h ++ standard.
1 5. Kemighan, Brlan W. , and Dennis . Rltchle. The Programming Lagage, Second
Editio. Prentlce Hall, 1 988. ISBN 0 1 3 1 1 03628.

1 6. nuth, Donald . The Art qf Compter Programming. Volme 2: Semirmerical


Algorithms. Third Edition. Addison-Wesley, 1 997. ISBN 020 1 896842. (
- . , 2.
. 3- . . : "", 2000.)
-
1 31 4

1 7. Koenig, Andrew, and Barbara . . Accelerated ++: Practical Prograrruning


Example. Addison-Wesley, 2000. ISBN 020 1 70353. ( -
. . ++. - . :
"", 2002.)
1 8. Langer, Angelika, and aus reft. Standard ++ IOStreams and Locales: Adviced
Prograrruner's Gide and Reference. Addison-Wesley, 2000. ISBN 032 1 585585.

1 9. Lippman. Stanley . , Jos Lajoie, and Barbara . . h ++ Primer, FiJth Edition.


Addlson-Wesley, 2005. ISBN 032 1 7 1 4 1 1 3 . ( .)
20. Lockheed Martln Corporation. ':.Joint Strike Flghter Air Vehicle Coding Standards
for the System Development and Demonstration Program." Document Number
2RD U OOOO 1 Rev . December 2005. ':.JSF++." www . stroustrup .
com/JSF-AV-rules . pdf.
2 1 . Lohr, Steve. Go : h Story qf the Math Mqjors, Bridge Players, Engineers, Chess
Wizards. Maverick &ientists and Iconoclasts - h Prograrruners Who Created the
Software Revoltion. Basic Books, 2002. ISBN 978-0465042265.

22. Meyers, Scott. E;[fective SL: 50 Specifte Ways to Improve r Use qf the Standard
Template Lirary. Addison-Wesley, 200 1 . ISBN 020 1 749629.

23. Meyers, Scott. Ejfective ++: 55 Specifi Ways to lmprove r Programs and
Designs, hird Edition. Addison-Wesley, 2005. ISBN 032 1 334876.

24. Programmlng Research. High-lntegrity ++ Coding Standard l Version 2.4.


www . programmingresearch . com.
25. Richards, Martin. BCPL - h Langage and Its Compiler. Cambridge Unlverslty
Press, 1 980. ISBN 052 1 2 1 9655.
26. Ritchie, Dennls. "The Development of the Programming Language." Proceedings
of the History of Prograrruning Langages Co11ference (HOPL-2). SIGPLAN
Notices, Vol. 28 No. 3, 1 993 .

27. Salus, Peter . Qarter Centry ofUNIX. Addison-Wesley, 1 994. ISBN 020 1 547775.

28. Sammet, Jean . Prograrruning Langages: History and Fundamentals. Prentice


Hall, 1 969. ISBN 0 1 37299885.
29. Schmidt, Douglas . , and Stephen . Huston. ++ Network Prograrruning, Volme
1 : Mastering Complexity with d Patterns. Addison-Wesley, 2002. ISBN
020 1 60464 7.

30. Schmldt, Douglas . . and Stephen . Huston. ++ Network Programming, Volme


2: Systematic Rese with and Frameworks. Addlson-Wesley. 2003. ISBN
020 1 795256.

3 1 . Schwartz, Randal L. , Thm Phoenix, and Brian . F. Leaming Perl, Forth Edition.
O'Reilly, 2005. ISBN 0596 1 1 058.
32. Scott. Michael L. Prograrruning Laguage Pragmatics. Morgan Kaufmann, 2000.
ISBN 1 55860442 1 .
33. Sebesta, Robert W. Concepts qf Prograrruning Langages, Sixth Edition. Addison
Wesley, 2003. ISBN 032 1 1 93628.
1 31 5

34. Shepherd, Slmon. ''h iny Encryptlon Algor1thm ()." www tayloredqe . /
.

reference/athematic/EA-XEA . pdf http : //143 . 53 . 36 . 235 : 8080/tea . htm.


35. Stepanov, Alexander. www tepanovpapers . .
.

36. Stewart, G. W. Matrix Algorithms, Volume I: Basic Decompositions. SIAM, 1 998. ISBN
08987 1 4 1 4 1 .

37. Stone, D. Carollne Jarrett. Mark Woodroffe, and Shailey Minocha. User
Interface Design d Evaluation. Morgan aufmann, 2005. ISBN 1 20884364.

38. Stroustrup. Bjarne. " istory of C++: 1 979- 1 99 1 ." Proceedings of tf1e ACM History
o.f Programing Languages Coriference (HOPL-2). SIGPLAN Notices, Vol . 28 No.
3 , 1 993.

39. Stroustrup, Bjarne. h Design and Evolution q_f ++. Addison-Wesley. 1 994. ISBN
020 1 543303.

40. Stroustrup, Bjarne. "Learning Standard ++ as New Language." / ++ Users


Journal. 1 999.

4 1 . Stroustrup. Bjarne . " and ++: SIlings": " and ++: Case for Compat!lity":
and " and ++: Case Studles in Compatibllity." h / ++ Users Jouial, July,
Aug . , and Sept. 2002.
42. Stroustrup, Bjarne. "Evolvlng Language in and for the Real World: ++ 1 99 1 -
2006." Proceedings of the hird SIGPLAN Conference the History of
Programing Languages (HOPL-III). San Dlego, , 2007. http : / /portal . . orq/
toc . cfm?id=1238844.
4 3 . Stroustrup. Bjarne. h ++ Programming Language, Fourth Edition. Addison-
Wesley. 20 1 3 . ISBN 032 1 563840.
44. Stroustrup. Bjarne. Tour q_f C++ . Addison-Wesley, 20 1 3. ISBN 978-032 1 9583 1 .

45. Stroustrup, Bjarne. Author's home page, www stroustrup . .


.

46. Sutter. Herb. Exceptional ++ : 4 7 Engineering Puzzles, Programming Prolems, d


Solutions. Addlson-Wesley. 2000. ISBN 020 1 6 1 5622. ( - ,
. ++. - . : "", 2002.)
47. Sutter, Herb, and Andrel Alexandresc u . ++ Coding Standards: 1 0 1 Rules,
Guidelines,d Best Practices. Addison-Wesley, 2004. ISBN 032 1 1 1 3586. (
- , . npopaupo6QJ-LU
++. - . : "", 2005.)
48. Unlverslty of St. Andrews. The MacThtor History of Mathematics archive. http : / /
www-qap . dcs . st-and . ac . uk/-history.
49. Wexelat, Rlchard L. , ed. History of Programming Languages. Academic Press,
1 98 1 . ISBN 0 1 27450408.

50. Whittaker, James . to Break Sq_ftware: Practical Guide to Testig. Addison


Wesley, 2002. ISBN 020 1 796 1 98 .
5 1 . Wood, Alastalr. Introduction t o Numerical Analysis. Addison-Wesley, 2000. ISBN
020 1 3429 1 .

cin, 1 0 1
const, 1 38; 278; 308; 380; 1 1 8 1
, 1 1 77
const_cast<>. 670; 1 1 83
-, 1 1 75; 1 1 76
constexpr. 1 37; 337; 1 1 8 1 ; 1 1 92
->, 668; 1 1 76
cout, 85
! = , 1 1 76
?\, 370; 1 1 77
D
. . . , 1 1 38; 1 1 94; 1 253
. * , 1 1 76
default, 1 46
, (), 1 1 78 delete, 659; 1 1 82
[], 706; 709; 8 1 9 deque, 8 1 7
*, 709; 1 1 75; 1 1 76 dynamic_cast<>, 1 1 83
\ , 343; 373; 1 1 74; 1 1 98

&, 1 1 75; 1 1 77
&&, 699; 1 1 77 else, 1 45
%, 1 1 76 enum, 367; 1 1 96
+, 1 1 76 enum class, 367; 1 1 96
++, 1 1 74; 1 1 75 ermo, 995; 1 225
<<. 1 1 76 exception, 1 226
==, 1 1 76
explicit, 703; 1 1 8 1 ; 1 20 1
>>. 1 1 76
1 . 1 1 77
F
1 1 . 1 1 77 FLTK, 1 290
#define, 1 2 1 7 friend, 1 1 99
#ifdef, 1 1 46
#ifndef, 1 1 4 7
#include, 86; 1 2 1 7 if, 1 43
ifstream, 399
initializer_list<>. 690; 1 253
array. 8 1 7 is . . " 448; 1 262
auto, 800; 830 istrearn, 397
istringstream, 444

basic_strlng, 928
L
tset, 1 04 1 list, 792; 8 1 7
break, 1 46 lvalue, 1 35; 1 1 78


case, 1 46 rnain, 1 1 64
catch, 1 89; 1 2 1 3 rnap, 8 1 7; 846; 847
cerr. 1 94 multimap, 8 1 7; 846
rnultiset, 8 1 7; 846
1 31 7

N
namespace, 3 1 8; 1 2 1 5 template, 74 1
new, 65 1 ; 1 1 82 this, 680; 1 1 98
, 1 024 throw, 1 89; 1 1 78; 1 2 1 3; 1 2 1 4
nullptr, 657; 1 1 69 tolower, 448
numeric_limlts<>. 1 266 toupper, 448
try, 1 89; 1 2 1 3
tuple, 1 253
ofstream. 400 typedef, 1 2 1 6
operator. 1 1 95 typeid , 1 1 75
ostream, 396 typename, 74 1 ; 1 2 1 2
ostrlngstream, 444
override, 564; 1 206 u
Unicode, 928
union, 1 209
pair, 1 252 unique_ptr. 768; 1 254
private, 256; 354; 550; 564; 1 1 97 unordered_map, 8 1 7; 846; 854; 855
protected, 550; 564 unordered_multimap, 8 1 7; 846
pullc, 256; 354; 550; 565; 1 1 97 unordered_multiset, 8 1 7; 846
push_back, 737 unordered_set, 8 1 7; 846
Python, 920 using, 797; 1 2 1 5; 1 2 1 6

Q v
valarray, 8 1 9; 1 269
R vector, 1 58; 1 90; 644; 8 1 7
regex, 945 virtual, 554; 562
reinterpret_cast<>, 670; 1 025; 1 1 83 Visual Studio, 1 284
return, 1 89; 32 1 . 1 285
rvalue, 1 35; 1 1 78 vold, 1 55; 3 1 9; 669

s w
set, 8 1 7; 846; 857 weak_ptr. 1 255
shared_ptr. 769; 1 254
slzeof, 649; 1 1 82

statlc, 6 1 4 . .
statlc_cast<>, 669; 1 1 83
STL, 34; 783; 789; 9 1 7 , 1 33; 1 305
. 808 . 883
streambuf, 457 , 646; 1 305
string, 8 1 9; 926 , 828; 1 305
, 927 accumulate, 84 1 ; 1 269
struct, 356; 1 1 24; 1 1 97 adjacent_difference, 1 270
swttch, 1 46 adjacent_find, 1 240
inary_search, 865; 1 245
. 859; 1 242
copy_backward, 1 242
1 31 8

copy_if. 863 stae_partition, 1 246


count, 1 24 1 stae_sort, 1 245
count_if, 1 24 1 swap, 1 244
equal, 1 240 swap_ranges, 1 244
equal_range, 865: 866: 1 245 transform, 1 24 1
fill, 1 244 uninitialized_copy. 1 244
fill_n, 1 244 uninitialized_fill, 1 244
find, 829: 1 240 unique, 1242
find_end. 1 24 1 unique_copy. 862: 1 242
find_first_of, 1 240 upper_bound, 866: 1 245
find_if, 833: 1 240 , 1 244
for_each, 889: 1 240 , 1 24 1
generate, 1 244 . 1 240
generate_n, 1 244 , 840: 1269
includes, 1 24 7 , 7 4; 220
inner_product, 844: 1 269 . 235
inplace_merge, 1 246 , 1 3 1 ; 1 305
iter_swap, 1 244 , 582
itoa. 1 270 , 1 55
lexicographical_compare, 1 249 , 1 96
lower_bound, 866: 1 245 , 7 1 1 ; 1 1 89
max_element, 1 249
merge, 1 246 , 370
min_element, 1 249 , 1 1 80
minmax, 1 249
mismatch, 1 240
next_permutation, 1 248
. .
nth_element. 1 245
partial_sort, 1 245 , 1 036; 1 306
partial_sort_copy. 1245 , 1 1 9
partial_sum, 1 270 , 1 20
partition. 1 246 , 243
prev_permutation, 1 248 , 92; 1 34; 1 86; 54 1 ; 1 306
random_shuffle, 1 243 FL. 47 1 ; 506: 1 290
remove, 1 242 Graph_lib, 469
remove_copy. 1 242 iostream, 430: 93 1
replace, 1242 Matrix, 974
replace_copy. 1 242 SL, 783: 789: 9 1 7: 1 2 1 9
reverse, 1 242 , 828
, 1 22 1
reverse_copy. 1 242
, 1 225
rotate, 1 243
, 8 1 7
rotate_copy. 1 243
search, 1 240 , 466
search_n, 1 24 1 . 1 223: 1 2 7 1
set_difference, 1 247 , 1 1 9
set_intersection, 1 247 , 1 049: 1 208
set_symmetric_difference, 1 247 . 1 038
set_union, 1 247 . 1 52
sort, 864: 1 244
1 31 9

. 498
, 504

. 523
. 4 1 5
. 525
, 97 1 ; 1 59 , 4 79
push_back(). 1 60
, 607
size(), 1 60
. 6 1 8
. 1 60
. 626
. 1 6 1
, 609
, 1 092
, 62 1
, 1 1 70; 1 306 , 632
, 1 004 . 6 1 8
, 1 006
.
1010

. 1 007
, 1 006 -. 846; 850
. 1 008 , 850
, 1 35; 1 1 74 , 1 306
. 1 1 76 , 1 1 24
, 137; 1 1 8 1 , 662; 1 202; 1 306
. 1 1 82 , 549; 665; 1 206
, 1 1 74 , 663
. 1 1 77 . 70 1
. . . 703
, 706
, 1 1 75 , 1 60; 654; 1 307
. 3 1 5 , 970
, 1 3 1 ,
558

, 234 using, 344
, 88
, 540
. 503 , 466
. 484
, 532

. 486 , 9 1 ; 1 8 1
, 473
. 472: 477 3
. 5 1 2 , 87
. 480 . .
, 475
, 523 , 334
, 594
, 506
. . RAII
, 48 1 ; 5 1 4

. 507
, 1 1 4 7
. 484; 52 1
. 144
1 320

, 1 03; 1 1 7; 1 307 . 787; 1 227; 1 307


, 508 . 1 250
. 1 229
. 1 229
, 1 1 69 . 8 1 9; 1 230
RAII. 664; 765; 1 2 1 3 . 82 1 : 1 230
, 543 . 820: 1 229
, 1 230
. 1 1 5
, 1 230
, 1 1 6
. 1 228
, 343
, 860
. 206; 362; 1 307
. 1 097
r, 8 1 9: 1 230
, 378
, 1 49; 1 307
, 1 1 0; 656; 689; 1 307
. 340
. 3 1
, 690; 1 253 . 473
, 353; 1 1 97; 1 307
. 342 . 548: 565: 1 206: 1 305
- , 499: 1 306
, 1 24 , 3 1 7
, 1 307 , 1 202
. 1 1 3 , 564: 1 197
. 88; 1 4 1 , 354; 37 1
for, 1 52 . 548; 1 308
if. 1 43 , 359: 1 20 1
, 376: 1 202
switch, 1 46
, 375: 1 203
while, 1 1 2: 1 50
. 3 1 7
, 1 43
. 1 204
, 1 52
, 557
, 306; 354; 1 307
. 1 204; 1 3 1 0
. 540
. 354
, 37 1
. 363
, 608
. 365
. 372
-. 359
, 69 . 364
. 7 1 , 379
, 1 88; 762; 1 2 1 3 . 354
out_of_range. 1 93 , 448
ntime_error, 1 93 , 87; 1 1 25; 1 1 70
. 766 . 88
, 899; 900 . 88
. 88
, 89 1 . 88
, 88
. 88 , 3 1 4
. 88 , 86; 283; 1 1 64
1 321

, 88 , 227; 1 1 65; 1 308


, 88 . 1 1 68
, 9 1 . 944
, 1 1 2 , 1 1 68
, 1 08 . 1 167
, 608 . 1 1 69
, 1 36; 308; 380; 1 308 , 1 1 69
. 137 , 1 1 65
, 278 -, 585; 839
, 359; 1 20 1 ; 1 308 , 6 1 7
. 693 -, 585
. 703 -, 899
. 699
. 3 1 0; 376; 70 1 ; 1 202
, 1 90; 73 1 ; 8 1 8; 1 23 1 ; , l 37
1 308 , 1 1 43; 1 2 1 7
STL. . 8 1 7 . 432
, 1 232 , 1 048
. 846: 1 23 1 , 97 1 ; 646; 708; 1 1 90; 1 308
. 1 04 1 . 709
. 808 , 7 14
. 1 235 . 7 1 2
, 1 237 . 97 1
. 750 . 972
, 1 14 7 . 972
. 1 235 . 972
, 1 230 , 987
, 799 , 857
. 1 23 1
. 1 237 RGB. 506
-, 1 234
, 465
, 2 1
, 1 076
. 1 308
, 885
, 4 72
. 477
, 696; 1 203; 1 308
. 696 , 1 006
. 7 1 4 , 558; 1 204
, 696 . 567
, 859 , 1 205
, 694 . 567
, 85 , 57; 62
. .

. 3 1 3; 1 1 70; 1 308
. 227; 234 . 3 1 3; 1 1 7 1
, 1 65; , 3 1 4; 1 1 7 1
8 1 9; 1 1 33 . 3 1 4; 1 1 7 1
1 322

, 3 1 4: 1 1 7 1 , 1 9 1
, 1 1 7 1 , 92; 1 36: 1 75: 1 77; 1 97
. 995
, 7 1 . 655
, 270 . 270
, 75 . 1 225
, 1 209 , 967
, 1 00; 1 02; 1 1 7; 1 309 , 1 0 1 3
, 1 1 73 , 7 1 6
, 329: 1 1 73 , 1 258
, 1 1 70 . 1 78
, 1 1 73 . 1 87
, 1 1 73 , 1 79
, 353: 36 1
, 835: 1 250
- , 7 1 9
. . , 646; 1 278
- . 1 1 72
, 65 1
, 92: 1 1 7; 303; 1 309 , 650: 65 1 ; 1 0 16: 1 1 72
using, 344: 5 1 0: 797 , 1 1 30

, 1 06; 1 38 ; 227 . 1 0 1 6

case, 1 37 , 685
retum, 1 89 . 1 1 70
, 4 1 4 . 658
, 4 1 3 . 755
, 836 , 650
, 1 1 78 . 650
, 370 . 1 0 1 5: 1 1 72
. 1 1 78 , 650: 1 0 1 6
, 1 1 78 , 660
, 1 1 78 , 1 309
, 1 0 1 ; 1 03; 1 1 7; 305; . 1 55; 3 1 9
1 309 . 3 1 9
, 1 0 1 2 . 1 309
, 92; 20 1 ; 1 098; 1 309 . 370: 1 1 95
, 84 7 , 1 1 92
, 200 , 554; 56 1 ; 562; 1 309
. 1 232 , 1 00; 1 02; 1 1 7; 308:
, 1 309 1 309
-. 403 . 3 1 6: 34 1
, 285 , 697; 1 204; 1 3 1 0
, 954 , 968
, 92; 1 74; 1 82 , 1 248
, 92: 1 74: 1 77 , 367; 1 1 96
, 92 . 369
, 1 9 1 . 4 72
, 309 . 1 24 7
1 323

, 1 09; 1 1 0; 1 1 77
. 1 1 79 , 694
, 864 , 699
, 558; 745 . 1 04
. 745 , 84
, 338 . 1 76
, 209; 1 3 1 0 , 40; 62; 75; 84;
, 93 1 1310
cerr. 1 94: 1 256 . 888
cin, 1 0 1 : 1 256 , 1 003
cout. 85: 1 256 , 882
ifstream, 399 . 878
istream. 397: 1 256 , 880
istringstream, 444 ., 885
ofstream, 400 , 89 1
ostream, 396: 1 255 . 543: 744: 888: 1 308
ostringstream, 444 -, 558;
. 1 0 1 : 1 258 567; 888; 1 309
, 4 1 5 . 887
-. 394; 1 255 , 885
, 1 256 , 936
, 396 , 887
. 85: 1 259 . 1 1 6: 203: 1 056: 1 059: 1 1 37;
. 405 131 1
. 404: 4 1 5 'I)' , 3 1 8
. 432: 1 260 , 58
. 1 258 , 55; 57
. 403 . 73
, 444 , 73
. 399 , 73
. 833 ., 240
. 254 , 74; 220
.. 207; 1 3 1 0 , 540
. 1 40; 669 , 540
. 1 1 80 . 880
. 1 20 . 3 1 4; 3 1 8: 342;
, 1 1 80 1215
. 1 2 1 std. 1 224
. 1 2 1 , 223
, 1 1 8 1 . 224; 1 3 1 0
. 1 1 80 . 1 1 9 1 ; 1 2 1 6; 1 3 1 0
. 1 1 80
. 1 02 1
. 1 1 79
. 1 1 80

, 3 1 2; 1 2 1 6
. 1 96; 669 , 647
, 1 0 1 . 1 2 1 4
. 200 , 75; 220: 306
1 324 1

, 1 049 , 759
, 94 1 ; 947; , 1 3 1 1
1 264; 1 3 1 0 , 864
. 943; 95 1 , 1 30; 353
. 95 1 . 403
. 952 , 220; 1 3 1 1
, 1 264 , 67 4; 1 237
. 949; 953; 1 265 . 792
. 943 . 792
, 954 , 676
, 950; 1 265 . 792
, 943; 946 . 92
, 945: 1 264 , 557; 750
. 948 , 324; 67 1 ; 1 1 9 1
, 955; 1 264 , 1 3 1 1
, 948; 1 264 , 1 056; 1 059
, 94 1 , 336; 650; 1 02 1 ; 1 023; 1 232
, 9 1 , 337
, 1 3 1 . 1 2 1 4
, 243; 1 3 1 1 , 1 232
. 243; 1 306 , 1 3 1 1
. 70 1 ; 762; 1 006 , 1 262
. 1 59
, 7 1 5; 1 132; 1277
, 1 0 1 4 , 1 277
. 660 , 1 1 97
. 1 0 1 9 . 1 30; 733
. 88 1
, 697 , 492
. 697

, 1 04
, 448; 1 262 . 560
, 1 1 68 , 293
, 278 , 1 1 03
, 235 , 484; 926
, 24 1 , 88

. 986 , 87
, 1 1 66 . 1 5 1
, 844 , 240
, 980 , 75; 2 1 ; 27 1 ; 882;
, 1 036; 1 3 1 1 1 073; 1 3 1 2
, 1 1 25; 1 1 70 . 1 076
, 99 1 . 1 077
, 99 1 . 1 076
, 99 1 . 1 00; 1 02; 1 1 7; 1 3 1 2
1 325

bool , 1 03 : 1 39 . 1 1 2
char. 1 03 , 399; 438
clock_t, 1 279 , 439; 1 272
doue. 1 03 , 443
int. 1 03 . 4 1 7
string. 1 03 , 589
time_t. 1 2 79 -. 43 1
void'". 669 . 435
, 1 1 87 , 435
. 743 , 437
, 358 , 433
, 745 . 435
. 228: 352; 1 1 95 . 436
, 1 40; 669; 1 1 79 . .
, 454; 1 1 84
, 1 1 27 . 1 54; 1 1 9 1 ; 1 3 1 2
. 1 1 27 constexpr, 337
. 1 1 88 main, 87; 322; 1 1 64: 1 1 9 1
. 1 1 88 . 3 1 9
. 240 , 1 1 94
. (). 668 . 582 ; 1 1 93
, 1 1 36 . 558; 1 205; 1 306
, 1 54 : 32 1
. 382

, 646; 647; 1 1 88 ; 1 3 1 2 . 55 1

this. 680; 1 1 98 , 1 54

. 7 1 1 ; 1 1 89 - . 1 1 20

, 668 . 479

, 7 1 2 . 334

, 653 . 893

, 768; 1 2 54 . 87
shared_ptr, 769; 1 254 . 605
unlque_ptr, 768; 1 254 . 994; 1 267
. 7 1 8 , 6 1 3: 1 296
, 657; 1 1 69 , 1 58: 3 1 9
, 1 1 89 , 1 58
. 64 7 . 3 1 9
, 1 1 80
, 322 ; 328
. 1 0 1 0
, 323
, 1 1 46
. 325: 328: 672
, 3 1 5
. 605; 336
, 87: 1 55
. 6 1 4
, 398; 438: 1 27 1 : 1 3 1 2 , 87; 1 55; 1 1 9 1
, 440 , 87
. 1 1 4 1 . 980
, 87; 3 1 1 ; 1 307
, 402
1 326

. 548; 566; 1 207; Algol W. 905


1312 L, 9 1 4
. 1 6 1 , 9 1 4
BCPL. 9 1 4
. 303; 9 1 1 ; 1 1 07
#, 303: 920
, 8 5 5
++. 26; 85: 302: 9 1 5

COBOL. 202: 897


CPL, 9 1 4
, 1 50 ; 1 3 1 2
Euler. 905
for. 1 52
FLOW-MATIC . 897
for , 1 60
Fortran, 895
whHe. 1 1 2 ; 1 5 1
Java, 303: 920
. 4 1 5
Lisp, 899
, 1 50
Modula, 905
. 1 5 1
Modula-2. 905
. 1 5 1
Oberon, 905
Oberon-2. 905
Pascal, 904: 905
PERL. 920
high_resolution_clock, 1 2 7 1 . 920
steady_clock, 1 2 7 1 PL/360, 905
system_clock, 1 2 7 1 Simula. 909
Simula67. 558
, 1 1 9 Thrbo Pascal, 906
. 995; 1 268 , 89 1
, 996; 1 268 , 88
. 1 270. . . 877

. 348

lll , 74 1 ; 1 209; 1 3 1 2
. 1 2 1
. 753
, 743; 1 2 1 1
, 745
. 748
. 74 1
, 75 1
, 743; 1 2 1 1
, 745

, 8 5 ; 1 3 1 2
Ada. 907
Algol60, 90 1
Algol68. 904


. 7 , 2005. : Bjarne Stroustrup
. 7 "" , 2006. : Dept. pf Computer
Science, Thxas & Unversity
1 . 5. 1 Casio. : www . casio . com
1 . 5. 1 Casio . : www . casio . com
1 . 5.2 N 1 298. N
Burgmeister & Waine. : N Diesel A/S, Copenhagen,
Denmark
1 .5.2 Emma Maersk; ;
A rhus. Denmark. : Getty Images
1 . 5.3 . : Alamy I mages
1 . 5.3 Sony-Ericsson W-920
, .
co: www . sonyericsson . com
1 . 5.3 -
-. : Alamy Images
1 . 5.3 ,
, (Stephen G. Eick).
: S. G. Eick
1 . 5.4 . : Alamy Images
1 . 5.4 . : Da Vinci Surgical Systems,
www . intuitivesurgical . com
1 . 5.5 (
U nix, -
,
Windows). : Bjame Stroustrup
1 . 5.5 . : Istockphoto
1 . 5.6 . : NASA. www . nasa . gov
22.2. 1 EDCAS 1 949 . (Maurice
Wilkes), - (David Wheeler). :
The Cambridge University Computer Laboratory
22.2. 1 ( 1 974 ). :
The Cambridge University Computer Laboratory
22.2.2. 1 (John Backus, 1 996). Copyrtght: Louis Faan
Bachrach.
Christopher Morgan, Wizards and their onders:
portraits in computing, Press Press, 1 997. ISBN 0-8979 1 -960-2
1 328

22. 2 . 2 . 2 (Grace Murray Hopper). :


Computer istory Museum
22.2.2.2 (Grace Murray Hopper). :
Computer istory Museum
22. 2.2.3 (John . McCarty) 1 967 , .
: Stanford Unversity
22.2.2.3 1 996 . : Louis Fa.an Bachrach
22.2.3. 1 (Peter Naur),
(Brian Randell) 1 968 ,
,
.
22.2.3. 1 . , (Duo
Duo Zhuang) 1 995 .
(Erlk Fr0kjc:er)
22.2.3. 1 (Edsger Dijkstra). : Wikimedia
Commons
22.2.3.2 (Nisklaus Wirth). : N. Wirth
22.2.3.2 (Nisklaus Wirth). : N. Wlrth
22.2.3.3 (Jean Ichah). : Ada Informatlon
Clearinghouse
22.2.3.3 (Lady Lovelace). 1 838. .
: Ada lnformation Clearinghouse
22.2.4 (rlsten Nygaard) - (Ole-Johan
Dal), 1 968 . : University of Oslo
22.2.4 . 1 996 . : University of
Oslo
22.2.4 - , 2002. : University of Oslo
22.2.5 (Dennis . Ritchie) (Ken Thompson).
1 978 . Copyright: & Bell Labs
22.2.5 , 1 996. : Louls Fa.an Bachrach
22.2.5 - (Doug Mcllroy), 1 990 . :
GerardHolzmann
22.2.5 (Brian W. Kemigan). 2004 .
: B.W. Kernigan
22.2.6 , 1 996. : Bjarne Stroustrup
22.2.6 (Alex Stepanov), 2003. : Bjame
Stroustrup
25. 1 . : Mogens Hansen, N B&W,
Copenhagen
27. 1 & Bell Labs' Murray Hill Research Center, 1 990 .
: & Bell Labs

Вам также может понравиться