shmeiwseis gia th C++

  • Published on
    05-Nov-2015

  • View
    216

  • Download
    2

DESCRIPTION

C++ programming

Transcript

  • pi pi

    pi C++

  • Copyright c 2004 , stamatis@iesl.forth.gr

    pi LaTEX2.pi (c , -pi ).

    pipi 23 2006. pi pi- http://www.materials.uoc.gr/tetycpp

  • i

    1 11.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

    2 pi 52.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    2.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

    2.2 pi . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.2.1 pi . . . . . . . . . . . . . . . . . . . . . . . . . . 62.2.2 pi . . . . . . . . . . . . . . . . . . . . . . . . 72.2.3 pi . . . . . . . . . . . . . . . . . 82.2.4 pi . . . . . . . . . . . . . . . . . . . . . . . . . 92.2.5 pi . . . . . . . . . . . . . . . . . . . . . . . 102.2.6 void . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102.2.7 Enumeration . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    2.3 pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.4 . . . . . . . . . . . . . . . . . . . . 132.5 . . . . . . . . . . . . . . . . . . . . . . . . . 14

    2.5.1 typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142.5.2 pi . . . . . . . . . . . . . . . . . . . . . . . 152.5.3 pi . . . . . . . . . . . . . 152.5.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

    2.6 pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.6.2 (struct) . . . . . . . . . . . . . . . . . . . . . . . . . . 20

    2.7 . . . . . . . . . . . . . . . . . . . . . . . . . . 212.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . 24

    2.8 (namespace) . . . . . . . . . . . . . . . . . . . . . 262.9 (streams) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

    2.9.1 . . . . . . . . . . . . . . . . . . . . . . . . . . 282.9.2 Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 282.9.3 . . . . . . . . . . . . . . . . . . . 292.9.4 . . . . . . . . . . . . . . . . . . . . . . . . . . 31

    2.10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

    i

  • ii

    3 373.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

    3.1.1 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373.1.2 ?: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383.1.3 switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.1.4 goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403.1.5 assert() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

    3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413.2.1 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413.2.2 do while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413.2.3 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423.2.4 continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433.2.5 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

    3.3 . . . . . . . . . . . . . . . . . . . . . . . . . 433.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

    4 494.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514.4 . . . . . . . . . . . . . . . . . . . . 54

    4.4.1 . . . . . . . . . . . . . . . . . . . . . . 544.4.2 pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574.4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574.4.4 . . . . . . . . . . . . . . . . . . . . . . 61

    4.5 main() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634.6 overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 644.7 template . . . . . . . . . . . . . . . . . . . . . . . . . 64

    4.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 674.8 inline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 674.9 pi . . . . . . . . . . . . . . . . . . . . . . . . . . . 684.10 C++ . . . . . . . . . . . . . . . . . . . 684.11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

    4.11.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

    5 Standard Library 755.1 . . . . . . . . . . . . . . . . . . 76

    5.1.1 (Pair) . . . . . . . . . . . . . . . . . . . . . . . . . . 765.1.2 . . . . . . . . . . . . . . . . . . . . . 765.1.3 , . . . . . . . 77

    5.2 (containers) . . . . . . . . . . . . . . . . . . . . . . . . . 795.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 795.2.2 vector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 845.2.3 deque . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 895.2.4 list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 915.2.5 set multiset . . . . . . . . . . . . . . . . . . . . . . . . . 955.2.6 map multimap . . . . . . . . . . . . . . . . . . . . . . . 99

    5.3 (algorithms) . . . . . . . . . . . . . . . . . . . . . . . . 1035.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

  • iii

    6 1136.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

    6.2.1 Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206.2.2 Destructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1206.2.3 Copy constructor, assignment operator . . . . . . . . . . . . 1206.2.4 Other operators, overloading . . . . . . . . . . . . . . . . . . 120

    6.3 template . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

    pi . . . pi! 121

    Fortran C 125.1 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125.2 Fortran . . . . . . . . . . . . . . . . . . . . . . . . . . 126

    pi 129

    135

    137

    138

  • iv

  • pi pipi pi pi pi ISO C++, , pi pi.

    C++ , , pi pi pi pi pi , pi pi Fortran C. pipi C++ pi pi . pipi pi C++pi Standard 1998, , , pi pi, pi, pi pi pi pi pi pi pi pi.

    pi pi, pi C++ C. pi STL pi . pi pi pi pi C++ pi pi ( pi pi ).

    v

  • vi

  • 1

    pi pi . pi- pi, pi pi, pi pipi pi . , pi , pi pi pi pi pi pi pi , , pi .

    pi pi pi(Fortran, C, C++) pi pi, , - pi, pi pi ( pi) pi. pi pi - pi pi pi pi. pi , pi pi pi , pi , - pi pi . pi pi pi pi . pi - pi pi pi pi, ,pi pi .

    pi pi C++ pi . pi C++ pi pi pipi .

    1.1

    pi pi pi - pi : pi pi . pi pi C++ pi pipi - , :

    1

  • 2 1.

    #include

    /*main:

    Takes no arguments.Prompts for a real number and prints its square.Returns 0.

    */intmain() {

    double a; // Declare a real variable

    // Print text on screenstd::cout > a; // Get value from keyboard

    // Print text on screenstd::cout

  • 1.1. 3

    pi . pipi pi . pi pi - , pi main(). pi pi main() pi pi pi .

    double a; pi pi () 15 a (2.2.5). C++ pipi , pi pi compiler, pi pi.

    std::cin std::cout pipi pi , , standard input standard out-put . () pi std::cin ( ) pi pi pi pi . pipi pipi, pi, pi standard header (2.9). - , pi pi, C++ format / . pi - pi pi pi pipi., , pi pi.. pi pi pi (2.9.4).

    pi (") pi , C-style string. pi pi - pi \n (pi ), pi pi . pi - () pi pi (pi pi , (\)) pi pi pi (character literal, 2.2.2).

    (*) pi pipi pi pipi.

    (;) pi (, , - ). pi pipi ( pi #) ;.

    C++ pi pi , pi. pi pi - C++ . ( pi pi - ). pi, , pi pi (+=, ==,

  • 4 1.

    1.2

    1. , pi.2

    2. pipi pi pi pi . pi +,-,*,/ ( pi ). pi pi pi .

    3. pi pi C++ ;#include main(){std::cout > a >> bcout

  • 2

    pi

    2.1

    2.1.1

    C++ pi . pi compiler pi

    // , pi /* */ pi pi

    pi .

    /* */ pi pi pipi pi tab, . pi pi pi .

    :

    // This is a commentint j; // Here is a declaration/* Let us make

    an ugly multi-linecomment.

    */

    pi pi pi , , pi , pi pi.

    pipi pi- . , pipi pi : compiler pi pi- #if 0.....

    #endif

    pi pipi pi, pi (pipi ). pi, # pipi pi pi pipi-.

    5

  • 6 2.

    2.1.2

    , , pi, pi. C++ pipi (az,AZ), (09), underscore (_). pi . pi pi pi C++ , pipi pi . pi underscore (_) pi pi- pi compiler pi pi pi pi.pi, pipi pi C++ (Keywords, 2.1) , , pi pi pi-, pi.

    :

    pi :ena lathos onoma, pali_latho$, 1234qwer, delete, .onoma+

    pi :timi, value12, ena_onoma_me_poly_megalo_mikos, sqrt, Delete

    C++ Keywordsand and_eq asm auto bitand bitorbool break case catch char classcompl const const_cast continue default deletedo double dynamic_cast else enum explicitexport extern false float for friendgoto if inline int long mutablenamespace new not not_eq operator oror_eq private protected public register reinterpret_castreturn short signed sizeof static static_caststruct switch template this throw truetry typedef typeid typename union unsignedusing virtual void volatile wchar_t whilexor xor_eq

    2.1: C++.

    2.2 pi

    C++ pi pi pi pi - pi pi :

    2.2.1 pi

    pi (bool) pi pi- pi pi pi (pi.. /, /,. . . ). , pi.. a, :

    bool a;

  • 2.2. 7

    pi pi pi true false. pi pi, a pi pi pipi, pi (4.4), ( 6) namespace (2.8), pi (global) false.

    pi pi-:bool a = true;

    , ,bool a(true);

    pi bool pi pi pi, pi pi , pi, . pi , true 1 false 0. , pi true 0 false.pi, , 4.3, pi pi true false.

    2.2.2 pi

    pi (char) pi.. c, :char c;

    pi pi pi pi pi pi, pi, ASCII.

    char c = a;

    , ,char c(a);

    pi c , a. pi pi- () (") pi C-style string1 pi pi pi pi char.

    pi pi pi. 2.2, pi pi ( ) pipi. ..char newline = \n;char bell = \a;char alpha = \141; // alpha = a in ASCIIchar Alpha = \x61; // Alpha = a in ASCII

    pi pi C-style string.

    : 1pi array of const char.

  • 8 2.

    std::cout

  • 2.2. 9

    2.2.4 pi

    (pi.. pi int) :int i;

    pipi pi , namespace, i pi 0, pi. (pi.. 10) int i = 10;

    , , int i(10);

    pi pi pi pi. pi int pi 16 bi-ts, pi pi pi

    [215, 215

    )=

    [32768, 32768) . pipi pi pi std::numeric_limits::min() std::numeric_limits::max(), pi header :#include #include

    intmain() {std::cout

  • 10 2.

    pi long int pi L l :

    12L, 0xBABEL, 665l

    pi U u pi unsigned int, - pipi pipi unsigned long int.

    2.2.5 pi

    C++ pi pi : pi (float),pi (double) (long double). float double pi long double. pi float pi pi std::numeric_limits::digits10 pi std::numeric_limits::min() std::numeric_limits::max() pi header . pi. std::numeric_limits::min() pi pi pi- .

    pi pi , -pi pi pi double, pipi pi pi pi.

    , pi pi ( pi) pi pi double. pi pi pi . e E, pi, pi 10 pipipi pi e/E :

    2.034, 0.23, .44, 23., 2e 4 ( 0.0002), 2.3E2 ( 230.0) .

    F f pi float L l pi long double.

    2.2.6 void

    pi void pi pi pi- pipi. pi pi pi -, pi pi . pi void* ( void) pi. pi pi - pi .

  • 2.3. 11

    2.2.7 Enumeration

    Enumeration () pi pipi pi pi pi. ..

    enum Color {RED, GREEN, BLUE};

    pipi pi, pi Color, pi pi pi pi pi: RED, GREEN, BLUE. pi :

    Color c = RED;

    switch (3.1.3), compiler pi pi pi.pi, pi pi (4.4).

    pi : pi pi pi pi 0. pi RED 0, GREEN 1 BLUE 2. pi pi pi pipi, pi pi pi 1 pi pi :

    enum Color {RED, GREEN=5, BLUE}; // RED = 0, GREEN = 5, BLUE = 6

    pi . -, pi enum . :

    color d = 2; // Error

    2.3 pi

    pi pi C++ pi class template, pi pi .

    C++ pipi pi header

    #include

    pi pipi. (pi.. z) pi, pi

    pi double :

    std::complex z; // z = 0.0 + 0.0 i

    , double pi pi pipi pi (int, float, long double,. . . ). -, 0, pi pi ,pi pi pi pi pi STL.

    pi pi pi pi :

  • 12 2.

    std::complex z1(3.41); // or z1 = 3.41;// z1 = 3.41 + 0.0i

    std::complex z2(3.0,2.0);// z2 = 3.0 + 2.0i

    std::complex z3(z2); // or z3 = z2;// z3 = z2

    std::complex z4(std::polar(2.0));// z4 = 2.0 exp(0.0i)

    std::complex z5(std::polar(2.0,0.75));// z5 = 2.0 exp(0.75i)

    std::polar() pi (magnitude) pi (phase angle) ( rad) .

    +,-,*,/ +=,-=,*=,/= (2.7) - , pi pi pi . pi, C++ (4.1) , pi pi. std::abs() pi . pipi, std::norm() pi std::arg() z = + i

    std::abs(z) =2 + 2,

    std::norm(z) = zz? 2 + 2,std::arg(z) = arctan(/).

    , std::conj() pi . pipi pi pipi pi ,pi, pi pi pi .

    pi z pi , std::real(z) std::imag(z), std::complex: z.real() z.imag().

    pi pi pipi , pi compiler:std::complex z(3.0,1.0);// z = 3.0 + 1.0i

    z = std::complex(z.real(), 3.7);// z = 3.0 + 3.7i

    z = std::complex(2.3, std::imag(z));// z = 2.3 + 3.7i

    z = 5.0; // or z = std::complex(5.0);// z = 5.0 + 0.0i

    z = std::complex(-1.0, 2.0);// z = -1.0 + 2.0i

  • 2.4. 13

    (pi,)

    pi >> pi pi :

    (pi,)(pi)pi

    pi, , typedef (2.5.1).

    2.4

    C++ pi pi - ( 2.3). pi pi

    == != >< >= 2.0 true 2!=1+1 false. pi pi .

    , pi -, (==, ) (!=, ). pi pi , pi pi .

    C++ pi !(NOT), && (AND), || (OR).

    pi pi, pi pi:

    ! pi :

    ! (4 > 3) false. ! (4 < 3) true.

    pi && true pi true. pipi false:

    (4 > 3) && (3.0 > 2.0) true (4 < 3) && (3.0 > 2.0) false

    || pi true pi pi true, false:

    (4 > 3) || (3.0 < 2.0) true (4 < 3) || (3.0 < 2.0) false

  • 14 2.

    C++, pi pi bool:bool a = 3==2;bool b = ( (i > 0) && (i < max) );

    C++: pi pi pi pi pi (short-circuit evaluation). .. (i < 0) || (i > max) i < 0 true pi , pi pi. , (i < 0) && (i > max)

    i >= 0 false pi i > max. pi pipi pi pi pi- .

    2.5

    2.5.1 typedef

    pi , pi-. pi , pi , pi typedef. , pi pi.. std::complexpi pi pi complex pi :typedef std::complex complex;

    pi pi :complex z;

    typedef pi -pi pi pi : pi pi pi . pi double. pi pi pi pi , pipi pi double pi float. , - pi pi real pi pi typedef double real;

    pi pi float :typedef float real;

    typedef pi C++ pi pi- template functions (4.7).

  • 2.5. 15

    2.5.2 pi

    pi - pi, compiler pi pi pi , , pi pi pipi pi pi pi . pi pi pi const pi pi ( ) :

    double const pi = 3.141592653589793;int const maximum = 100;

    pi pi const pi .

    pi pi . pi pi (pi..pi pi, ) pi pipi pipi pi .

    2.5.3 pi

    pipi blockpi block pipi pi pi. pi pipi . pi pi , pi , pi . pi , pipi pi , namespace ( global) , pi 0 (pi pi). pi , static (4.9), pipi pi compilerpi pi .

    pi pi C++ pi- pi, pi pi C:

    int a = 3;

    pi 2.3, pi pi pi (pi C++) pi pi pi . C++ pi,

    std::complex z(1.0,2.0); // z = 1.0 + 2.0 i

    pi pi pi pi pi :

    int b = 4;int b(4);

  • 16 2.

    , pi pi (pi.. int) - pi (pi.. std::complex), pi -pi pi :int c = int(3); // c = 3

    std::complex z = std::complex(3.0, 2.0);// z = 3.0 + 2.0i

    int d = int();// d gets default integer value, 0.

    std::complex y = std::complex();// y gets the default value specified// in complex class, 0.0 + 0.0i.

    pi - pi pi pi pi pi templates (4.7,6.3).

    pi pi . int a;int b;

    pi int a,b;

    pi pi pi pi pi pi . .. int * a,b;

    (pi pi), a, , b. pi pi pi pi .

    2.5.4

    , , , pi pi pi- pi 2 } block pi. (global) pi, pi pi (4.4), ( 6) namespace (2.8), - pi ( pi #include). pi pi , pi. , pipi pi, pipi pi pi. pi 3 pi .

    pipi pi pi - pi block . ..

    2 pi.

  • 2.6. 17

    #include

    intmain(){ // begin block Adouble x = 3.2;

    { // begin block Bint x = 5;

    std::cout

  • 18 2.

    double temperature[365];

    int const N = 155;int telephone[N];

    telephone pi pi pi pipi .

    pi, pi.. temperature, pi 0 D1 pi D . pi, , pi 0. ..

    temperature[0] = 14.0;temperature[1] = 14.5;temperature[2] = 15.5;temperature[3] = 13.0;temperature[4] = 15.0;//.........

    int i = 3;

    std::cout

  • 2.6. 19

    pi . pi, 6 8 pi int a[6][8];

    pi.. (3,2) pi pipi a[3][2].pi , :

    int b[2][3] = { {0, 1, 2}, {3, 4, 5} };

    pi, -, pi pipi pi. pi , , , pi - pi (row-major order). Fortran pi (column-major order). pi, pi pi C C++ pi pi Fortran.

    , pi , pi -, pi column-major order :

    pi [Ai j] N1N2, C++ pi N1 N2. Ai, j a[i+ N1*j].

    pi [Ai jk] N1N2N3, pi N1 N2 N3. Ai, j,k a[i + N1 * (j + N2 * k)].

    pi . pi pi pi pi C pi pi pi pi, pi Fortran ,pi, pi .

    pi , C++ pi pipi pi pi. pipi, Standard Li-brary (STL) pi pi - pi . pi pi std::vector pi header : pi v, 30 pi pi.. double std::vector v(30);

    pi . pi , pi pi pi :

    :

    #include #include

    int main() {

  • 20 2.

    int N;

    std::cin >> N; // get dimensionstd::vector v(N);

    // v[0], v[1], ..., v[N-1]}

    pi, pi v - pi C++. pipi pi pi std::vector 5.

    2.6.2 (struct)

    pi pi pi.. . pi , pipi , , , ,pi. pi pi , pi . pi pi C++ std::string pi header . pi pi, pipi pipi , :struct ChemicalElement {

    double mass;int Z; // atomic numberstd::string name;std::string symbol;

    };

    (;) pi }. ( ) pi C++ pi };.3

    pi ChemicalElement pi- struct pi , , pi ( ) pi. pi pi . pi ChemicalElement pi pi-pi pi pi:ChemicalElement oxygen;

    pi pi pi pi pi pi pi pi , pi , pi..ChemicalElement hydrogen = {1.008, 1, "Hydrogen", "H"};

    (.) pi (.) :oxygen.name = "Oxygen";oxygen.mass = 15.99494;oxygen.Z = 8;oxygen.symbol = "O";

    3 pi .

  • 2.7. 21

    std::cout

  • 22 2.

    b = a + c;a = a-1;

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

    2.4 pi pi pi . pi, pi pi pi . pi . ( ) pi pi pi pi compiler pi . .. a--b (a--)b ( ) pi a-(-b). pi pi pi pi.

    pi pi pi - pi compiler pi ( , ) pi . pi.. pi int double -pi int double pi pi pi. pi pi pi int (pi bool char) pi int pi pi, . pi pi pi pipi pi: pi.. pi bool char;

    pi pi pi , ( - pi pi ) pi. pi pi, pi.. pi . pipi pi pi pi . pipi, pi pi pi pi pi ( pi) pi.5 int a = 3.14; // a is 3short int b = 12121212121.3; // b = ??

    pi pipi pi pi - pi. pi , (/) pi pi (/) . pi pi pi (pi) pi pi- pi /. pi pi pi static_cast:int sum = 2 + 3 + 5;int N = 3;

    // Wrong valuedouble mean1 = sum / N;

    5 compiler pipi pipi.

  • 2.7. 23

    C++pi .pi -> () pi pi []/ ( ) ++, -- sizeof pi sizeof()/ (pi ) ++, --bitwise NOT & pi * NOT ! /pi +, -pipi * ( pi) /pipi % + -pi , >>, = == !=bitwise AND &bitwise XOR bitwise OR | AND && OR || 4 ?:pi =pipi *= /=pipi %= += -=pi =bitwise AND &=bitwise XOR =bitwise OR |= ,

    2.4: pi ( -) pi . pi.

    4 pi (?:) (=) 3.1.2.

  • 24 2.

    // Correct valuedouble mean2 = static_cast(sum) / N;

    static_cast :static_cast(variable);

    pi pi pi :

    (newtype) variable newtype(variable).

    pi . pipi pi pi -

    pi overloaded , pi - pi . .. - , std::sqrt() pi pi float, double,long double int pi pi . pi 4.6, pi pi compiler C++ , pi pipi pi pi :#include

    intmain() {

    int p = 8;

    double riza = std::sqrt(p);// Error, ambiguous

    double r = std::sqrt(static_cast(p));// Correct. Calls sqrt(double).

    }

    C++ - pi pi pi pi pi , pi..int a;int b;

    b = a = 3;//First a = 3; then b = 3;

    bool cond;

    cond = b < 5;// First check b < 5; true. Then cond = true.

    2.7.1

    sizeof

    sizeof pi pi pi bytes.6 pi pi pi

    6 , byte char.

  • 2.7. 25

    sizeof:

    int a;

    std::cout

  • 26 2.

    bitwise NOT expr pi expr1 >> expr2& bitwise AND expr1 % expr2 bitwise XOR expr1 expr2| bitwise OR expr1 | expr2

    = expr2&= bitwise AND expr1 %= expr2= bitwise XOR expr1 = expr2|= bitwise OR expr1 |= expr2

    2.5: bit C++.

    pi bit - (true/false, on/off,. . . ) pi . C++ bitset std::vector bool pi pi pi, pi.

    pi pi pi pi bool, pi- pi pi , pi pi pi bit byte, (2.2.4).

    2.8 (namespace)

    pi pi - pi pi ( pi ) pi - . .. pi pi pi pi solve, - pi pi pi pi pi . C++ pi namespace ( ) pi . pi:

    namespace onoma {......

    double a;......

    }

    a ( pi , , -, pi. pi) namespace onoma. pi namespace pipi pi pi : onoma::a. namespacepi pi pi (a). pi pi- pi . , pi namespace pi

  • 2.9. (STREAMS) 27

    pi pi . namespace C++

    (2.1.2). pi pi pi pi Standard Library (STL) -

    namespace std. pi cin, cout,complex pi pi std::. pi- , pi namespace pi.. std, pi using namespace std; block pipi . pi - block pi pipi std::. ..

    #include #include

    // "std::" needed heretypedef std::complex complex;

    intmain() {using namespace std;

    // "std::" not needed herecomplex a(2.0,3.0);complex b(1.0);

    // In the following cout "std::" is not neededcout

  • 28 2.

    standard error pi . pi- pi pi pi- pi, pi pi.. pipi pi . pi pi pi pi , pi pi.. pi pi pi , - pi pi pi , pi pi.. pi . pipi streams header .

    2.9.1

    pi streams pi streams - . header pi std::ifstream std::ofstream. std::ifstream inpstr("filename");

    stream , inpstr, pi "filename". , pipi pipi., std::ofstream outstr("filename");

    stream , outstr, pi "filename". pi, .

    std::ofstream outstr("filename", std::ios_base::app);

    "filename" stream outstr .

    pi: inpstr, outstr ( pi, , pi pi) pi std::cin std::cout pi . , double a = 10.0;

    outstr > c;

    pi a pi outstr c pi inpstr.

    pi pi . pi pipi pi stream str (pi.. ) , pi : str.close();. stream str str.open("filename");.

    2.9.2 Strings

    (streams) -, . C++ strings, pi pipi.

  • 2.9. (STREAMS) 29

    pi header pi , pi std::istringstream std::ostringstream. pi - pi ostringstream C++ string -pi pi. pi - string, . pi C++ string pi pi "filename_3.dat":#include

    intmain() {std::ostringstream os;

    os j; // j = 6is >> k; // k = 7

    char ch;is >> ch; // ch = a

    }

    2.9.3

    pi pi, pi pi pi STL, (stream) standard output, standard error, string,

  • 30 2.

    pi std::cin pi >>, pi pi pi :

    double a;int b;

    std::cin >> a;std::cin >> b;

    ( ) . pi

    pi . pi..

    std::cout

  • 2.9. (STREAMS) 31

    2.9.4

    manipulators pi pi C++, boolalpha noboolalpha, pi pi pi - pi, pi bool. header pi pi

    (no)skipws: () . (no)showpos: () pi pi + . (no)showpoint: () pi 0 . scientific: pi pi d.dddddddd fixed: pi pi dddd.dd left/right: pi / pi.

    header pi pi

    setprecision() pi ( pi ) pi pi pi. pipi 6.

    setw() pi pi pi pi ( ) pi pi. pipi 0.

    setfill() pi pi - setw() pi pi . pipi .

    manipulators namespace std.

    :

    #include #include #include

    intmain() {

    double b = 3.25;

    std::cout

  • 32 2.

    std::cout

  • 2.10. 33

    int a = 2, b = 3, c = 4;double x = y = 5;bool equal = b = 3;int d = d;int e = "e";

    6. compiler; pi;

    7. pi pi pi com-piler; ;

    #include #include

    intmain() {

    int a = std::numeric_limits::min() - 10;int b = std::numeric_limits::max() + 10;std::cout

  • 34 2.

    () m%n++() m%++n() ++m=n--() m=n=3() m+=n-=2() m*=++n() m+=--n==m--

    11. pi pi :

    () (50 - 5 * 4) / 10 - 7() 12 + -5 * 2 + 6 / 3() 1 + 2 + 3 + 4 + 5 + 1 * 2 * 3 / 4 * 5() 3 > 7() (3 > 7) || (4 < 9)() 135 == 100 + 35() (true && false) || (true || false)() (3 > 7) || ((10 < 9) == (3 == 8))() false || !(3 > 7)() 3!=6

    () 6 / 4 * 5 == 6 * 5 / 4

    12. pi pi pi

    () 7 / 2() 7.0f / 2() ((7 / 2) == (7.0 / 2))() ((6 * 3) / 80) / (36 - 7 * 5)

    13. C++ pi 1 n.

    14. pi a, b pi c pi a.

    15.

    () ++x+=+a++-+b++() ++x-=-a+b++() -x---a---b--

    16. pi pi ; pi .

    17. C++ pi pi pi :

    () d.

  • 2.10. 35

    () pi r.() d h.() pi c

    a,b pi.

    18. pi pi pi :

    () a==b||a==c&&c

  • 36 2.

  • 3

    3.1

    3.1.1 if

    if pi pi-. () , pi pi. C++ - :

    if (condition) {... // block A

    } else {... // block B

    }

    (condition) pi pi , - block pi pi pi {} (block A). , block else (blockB).

    block pi pi pi , , pi . pipi pi pi pi pi ({}) pi pi. ..

    if (val > max)max = val;

    else {max = 1000.0;++i;

    }

    pipi pi else block pi pi :

    if (condition) {...

    }

    block pi pi pipi if. if (condition) {...} else {...} . block pi

    37

  • 38 3.

    () . pi pi .

    pipi if, pi pielse . else pi if block. pi, pi pi ,pi :

    if (i == 0)if (val > max)

    max = val;else

    j = 10; pi, j = 10; i 0 (val > max) (i == 0). pipi pi pi pi pi :

    if (i == 0) {if (val > max)

    max = val;}else

    j = 10;

    3.1.2 ?:

    (?:) C++.

    if (condition)val = value1;

    elseval = value2;

    val = (condition ? value1 : value2);

    ,

    (condition ? : )

    condition , .

    pi pi pi (?:) - pi pi, . pi - pi (=) pipi pi pi pi. , pi : (?:) (=) pi, pi pi pi . pi, a = b ? c : d

    a = (b ? c : d)

  • 3.1. 39

    a ? b : c = d

    a ? b : (c = d)

    3.1.3 switch

    switch pi pi pipi if. :

    switch (i) {case value1:...

    case value2:...

    ...

    ...

    case valueN:...

    default:...

    }

    i pipi pi (char, int, (short int, long int) signed unsigned pi ) enum. pi pi pi pi pipi pi pi pi pi pi pi pi pi.

    value1, value2,. . . ,valueN pipi pi enum ( pi).

    , i pi value1,value2,. . . ,valueN. pi , pi case. pi case / default pi, pi break ( pi.. goto, return, throw). break pi switch. value1, value2,. . . ,valueN block default, pi. , } switch.

    case default pi pipi.

    : pi pi

    pi pipi . pi +, -, *,/. pipi pi pi pi pi pi pi. pi

    #include int

  • 40 3.

    main() {double a, b, res;char c;

    std::cin >> a >> b;std::cin >> c;

    switch (c) {case +:

    res = a + b;break;

    case -:

    res = a - b;break;

    case *:

    res = a * b;break;

    case /:res = a / b;break;

    default:std::cerr

  • 3.2. 41

    :assert(integer_number);

    integer_number 0, pi pi, pipi NDEBUG pi pi header. pi pi, pi pi assert() .

    , assert() debugging, pi pi pi 0 false ( , 2.2.1), pi pi . pi.. pi assert(N

  • 42 3.

    , pi . pi 1 ( block).

    3.2.3 for

    pi pi forpi pi pi pi pi. C++ for pi pi . :

    for (initial_statement ; condition ; final_statement) {...

    }

    for :

    1. initial_statement. pi ( (,), 2.7.1).

    2. condition.

    , pi for. , block {}.

    pi block ( break, return, goto,throw,. . . ) final_statement.

    3. block , pi 2( ). pi pi .

    pi pi initial_statement, condition, final_statement pi pi. pi (condition) for .

    initial_statement pi , - pi for, , , }.

    : 10 pi pi a 0, 10, 20, . . . 90 pi :

    for (std::size_t i = 0; i < 10; ++i)a[i] = i*10;

    i pi pi . pi , std::size_t i; pipi pi for:

    std::size_t i;for (i = 0; i < 10; ++i)

    a[i] = i*10;

    // here i is 10

  • 3.3. 43

    3.2.4 continue

    continue pi for, while,do while. pi , pi pi .

    3.2.5 break

    break pi while, do while,for, switch. pi pi pi- , pi pi .

    3.3

    block pi pi pi , , pi . pipi pi pi pi pi ({}) pi pi.

    if while pi - ( pi) pi . pi .

    :

    if (int j = 3) max = 10 + j;

    j if. (3) true.

    3.4

    1. ()

    for (;;) {...

    }

    () pi for pi while. pi do while;

    2. pi pi:

    #include

    intmain() {int j = 3;

    if (j = 4) {std::cout

  • 44 3.

    ; ; . pi - ;

    3. C++ pi pi

    ex

    ex =

    n=0

    xn

    n!,

    sin x

    sin x =

    k=0

    (1)k x2k+1(2k + 1)! ,

    cos x

    cos x =

    k=0

    (1)k x2k(2k)! .

    pi pi-pi pi pi pipi - pi.

    4. pi pi 8 . rand() pi - pi 0 RAND_MAX. pi - RAND_MAX compiler; srand().1

    5. () 1000 [20, 20]. pi pi (pi-pi !)

    int r = -20.0 + 41.0 * (std::rand() / (RAND_MAX+1.0));

    () pi C++ pi pi pi , 0 pi.

    6. pi C++ pi

    () pi pi (n!) .() pi N pi Fibonacci2

    f (n + 2) = f (n + 1) + f (n), n 0, f (0) = 0, f (1) = 1 .

    () .() pi Lagrange3:

    pi () .

    1 UNIX man srand .2http://www.research.att.com/projects/OEIS?Anum=A0000453http://mathworld.wolfram.com/LagrangesFour-SquareTheorem.html

  • 3.4. 45

    7. pi C++ pi

    () pi.() pi pi N. (-

    : pipi 2,3,5,. . . ).() pi pi

    4, p,p+6,p+12 pi , pi .

    () pi pi pi

    1 + (p 1)!p

    (mod p) = 0 ,

    Wilson primes5.

    8. pi pi pi a, b, c pi 12 piax2 + bx + c. pi pi

    x1,2 =b b2 4ac

    2a,

    ( pi pi pi b2 4ac)

    x1,2 =2c

    b b2 4ac.

    C++ std::sqrt() . . pi pipi pi pi. pipi pipi pi .

    9. pi M N pi K pi 1 pipi 0.

    () pi ( M = 20,N = 60) pi , x o pipi.

    () pi .ppbm ( M = N = 512): pi : P1. : pi . 1 0 -

    pipi pi ( - ).

    pi pi plain pbm (portablebitmap) pi pi pi pi.

    4http://www.research.att.com/projects/OEIS?Anum=A0479485http://www.research.att.com/projects/OEIS?Anum=A047540

  • 46 3.

    10. pi pi pipi piA (M N) B (N P). pipi pi pi ;

    11. pi C++ pi pi Ax = B pi Gauss (Gauss elimination6). pi A B pi.

    12. pi C++ pi pi pi - pi A pi GaussJordan (GaussJordan elimination7).

    13. () pi pi 30 pi f (x) = x(x2 + 5 sin(x)) [5 : 5]. C++ std::sin() .

    () pi C++ pi pi pipi pi f (x) [5 : 5] pi: x1, x2 x2

    x1

    f (x)dx 12x ( f1 + f2) .

    Simpson: pi x1,x2, x3 x3

    x1

    f (x)dx 13x ( f1 + 4 f2 + f3) .

    Boole: pi pi x1, x2,x3, x4, x5 x5

    x1

    f (x)dx 245x (7 f1 + 32 f2 + 12 f3 + 32 f4 + 7 f5) .

    Durand: n pi x1, x2,. . . , xn xn

    x1

    f (x)dx x(

    25 f1 +

    1110 f2 + f3 + + fn2 +

    1110 fn1 +

    25 fn

    ).

    pipi pi fi f (xi) x pi . pi [10 sin(5) 50 cos(5)]. pi pi .

    14. Spline Fit. n (xi, yi). pipi 2(n 1) . pipi, pi pi- pi pi ( 2(n 2)

    6http://mathworld.wolfram.com/GaussianElimination.html7http://mathworld.wolfram.com/Gauss-JordanElimination.html

  • 3.4. 47

    ). pi pi pi 0 (2 ), pi , . pi pi (2). 4n 4 . pi pi pi pi pi pi pi .

    () pi pi pi spline - pi pi pipi .

    () pipi pi pi - .

    () pipi pi pi pipi pipi -.

    15. pi C++ pi pi Game of Life8 Dr. J.Conway. pi : pi M N, pi (- ). pi K -. ( ):

    () - ().

    () pi- (pi).

    () pipi pi (pi- pi (pi pipi !.

    pi pi pi- . pi pi plain pbm ( - 9) pi . pi pi pi , (, ).

    8http://www.math.com/students/wonders/life/life.html

  • 48 3.

  • 4

    4.1

    pi pi pi pi C++, pi pi-pi . , , , main(), , , -. pi pi pi pi, , . pi pi -, pi, , , pi pi pi main() -, pi . pipi pi pi pi, .

    pi pi pipi pipi pi -, pi, pi pi. pi.., pi pi, - pi , pi pipi ,pi, pi pi pi pi-. pi pi pi pi , pipi pi pi pipi pi pi pi .

    pi pi pi (structured) (procedural) pi- pi pi pi pi pi C++.

    pi pi , C++ pi .

    49

  • 50 4.

    4.2

    (reference) pi. ,pi.., a

    int a;

    pi (pi.. r) a :

    int & r = a;

    r pi pipi pi a. pi pi pi, pi &. pi ( ) pi pi ( pi,, pipi pi pi). pipi, pi pi pi pi pi .

    pi, pipi a - r :

    int a;int & r = a;

    a = 3; // r = 3r = 2; // a = 2

    int b = a; // b = 2

    int c = r--; // c = 2, a = 1

    int d;int & r = d; // Error

    const, pipi :

    int const a = 5;

    int & r1 = a; // Error.

    int const & r2 = a;// Correct. Value of a cannot change through r2.

    r1 pi, pi a, pi const, r1.

    , , pi, pi C++ pi -, , pi , . , pi- std::numeric_limits::digits10. pi pi pi pi:

    #include #include

  • 4.3. 51

    intmain() {

    int const & digits = std::numeric_limits::digits10;

    std::cout

  • 52 4.

    int * q = p; q int p. , q p , , *q *p ( a).

    pi piint a = 5;int * p = &a;void * t = p;

    void pi p, int, ,, a. (*) p pi a , (*) t pipi. , void pipi pi pi static_cast pi (pi pi pipi ) pi pi pi pi . pipi ,

    int * v = static_cast(t);

    *v = 4;

    a 4. pi pi -

    pi . *) pi. pipi- pi pi pi - pi . , pi ( ) .

    pipi pi pi 0. 0 pi , pi, pi . (null pointer) (*) pipi (pi pi). (pi.. ) 0 ( pi , 2.2.1) pipi pi pipi pipi pipi pi . pi : pi ,, pipi pi pi.

    pi . pi ( pi pi ):

    int a;

    int * p1 = &a;int const * p2 = &a;int * const p3 = &a;int const * const p4 = &a;

    p1 . p2 . pi

    pi *p2.

  • 4.3. 53

    p3 . pi -pi *p3 pipi pi pi pi pi .

    p4 . pi pi .

    pipi pi pi - : pipi -.:

    double x = 1.2;

    double * const p = &x;

    double y = 0.1;

    p = &y; // error

    double const * q = &x;

    *q -= 0.2; // error

    int const a = 2;

    int * r = &a; // error

    p pi pi pi, pi pi pi pipi *p. , p pi . pi ++,-- ( pi ) : pi T sizeof(T) pi . pi, pi pi (pi.. p+2) pipi sizeof(T). pi , , pi ( !) pi pi .2 pi pi pi void pi, pi pi, ( void *).

    pipi pi - pi . , pipi pi pi pi pi:int a[10];

    int * p = &a[3];

    int * q = p + 2; // q == &a[5]2 pi std::ptrdiff_t pi .

  • 54 4.

    , int a[10];int * p = &a[0];

    *(p+i) pi a[i] , , p+i == &a[i]. pi pi pipi pi pi pi pi pi.

    pi , pi - . pi a[i] pi *(a+i).

    : pi :

    double b[10];

    double * p = b;

    for (int i = 0; i < 10; ++i)*p++ = 1.0;

    2.6.2 pipi , pi.. member, (struct) , pi, , pi.. a, :

    a.member

    pipi pi , pa, pipi (- pi pi (*) (.), 2.4)

    (*pa).member

    pi pi C++ , pipi:

    pa->member

    4.4

    pi pi pipipi pi pi . pi pi pi pipipi pi pi , ,pi 4.7, pi pi pi. pi pi pi pi pi pi. pipipi pi pi pi , pi pi .

    4.4.1

    :

    pi_pi(pi__ , pi__ ,. . . ){

  • 4.4. 55

    // }

    pi pi pi void pi pi- . , pi pipi pi pi pi pi (pipi, , pi ). pi , , pi void. , pi, pi pi (}).

    pi pi- pipi pi pi, pi :

    pi_ _pi [ ]

    . pi, , pi pi. , int a[] int *a . pi pi pi pi, , pipi . containers STL pi 5 pi.

    pipi pi , pi . compiler pipi , (pi ) pi pi . pi -. pi pipi ( {} pi) pi (;):

    pi_pi(pi__ , pi__ ,. . . );

    pipi pi pi . pi

    . pipi pipi pi- . , pi pi headers, .h,3 pi pi pipi pi #include "name.h"

    pi name.h header, pi - (pi, pi pi path ). headers pi pi pi ("). , headers pi pi pi ().

    pi headers, compiler pi pi . ,

    3 pi compiler

  • 56 4.

    pi , pi pi , headers.

    : min/max pi pi /

    pi :

    min: < pi pi max: < pi pi

    pi utilities.h (pi.. pi float)

    // utilities.h

    // declarationsfloatfmin(float a, float b);

    floatfmax(float a, float b);

    utilities.cc

    // utilities.cc#include "utilities.h" // Not necessary but good practice

    // definitionsfloatfmin(float a, float b) { return a> b;std::cout

  • 4.4. 57

    pi - pi , linking.

    , pi pi pi (pi ), , pi pipi :int func(double a, double b = 5.0);

    func() pi pi (pi a) pi b pi pipi, 5.0. , pi pi pi pi pi- pi , pi, compiler pi pipi .

    4.4.2 pi

    pi pi

    return ;

    pi pi pi . pi main() pi return: pipi return 0;.

    pi pi ( pi void) pi pi return; ( ).

    pi return, pi pi .

    pi pi . pi pi pi pi pi pipi pi pi, pi- pi pi , .

    4.4.3

    pi , - pi pi pi pi ( pi pi ). pi pipipi , pi pipipi pipi pi pi , pi pi . pi- , pipi pi pi pi pi, pi !

    :

    #include #include

    doublefunc(double a, double b);// declaration. The definition is elsewhere.

  • 58 4.

    intread(double & a, char const fname[]) { // definition

    std::ifstream file(fname);file >> a;

    return 0; // All ok}

    voidprint(char c) { // definition

    std::cout

  • 4.4. 59

    add3() pi, pipi - pi pi : x pi z (2.0), - 5.0 pi pi , . z pi 2.0.

    pi pi pipi , pi..

    void add3(double & x) { x+=3.0; }

    , pi..

    void add3(double * x) { *x+=3.0; }

    pi -. pi pipi, pi , add3(z), pi x z pipi - z. , pi z, add3(&z). x pi z. x pi , *x pi pi .

    pipi , pi , pi, pi , pi pi .

    pi pi - ( , ), pi pipi, const. ..

    voidprint(std::size_t const N, double const a[]) {

    for (std::size_t i = 0; i < N; ++i)std::cout

  • 60 4.

    xp pi , pi *xp ( pi )

    void f(double * xp); // xp cannot change, *xp can change

    xp pi , *xp ( pi )

    void f(double const * xp); // xp cannot change, *xp cannot change

    pi a pi , pi

    void f(double a[]); // a cannot change, a[i] can change

    pi a pi ,

    void f(double const a[]); // a cannot change, a[i] cannot change

    (recursive)

    C++ pipi . , pipi pi pi , pi pi.

    : pi pi pi pi

    (recursive) pi: ,

    n! ={

    1 2 (n 1) n = (n 1)! n , n > 0 ,1 , n = 0 .

    pi, pi pi n pi pi- pi ( n 1). pi pipi pi C++

    std::size_tfactorial(std::size_t n) {

    std::size_t result;

    if (n > 0)result = n * factorial(n-1);

    if (n == 0)result = 1;

    return result;}

    pi pipi ( n pi pi pi- pi pi). factorial() factorial(n)factorial(n-1)

  • 4.4. 61

    factorial(n-2) . . . ( pi pi pi ) 0.

    pipi pi pipi (?:),3.1.2:std::size_tfactorial(std::size_t n) {

    return (n > 0 ? n * factorial(n-1) : 1);}

    4.4.4

    pi, , -.

    pi pi f (x) pi , pi (x, y). pipi #include

    double f(double x);

    intplot(double low, double high) {

    double const step = (high - low) / 100;

    for (double x = low; x < high; x+=step)std::cout

  • 62 4.

    double f(double x); // declaration of f(x)

    double (*fptr)(double x); // declaration of a pointer

    fptr = f; // assignment

    double x = 1.2;

    double y = f(x);

    double z = fptr(x); // or z = (*fptr)(x);

    // y == z

    pipi plot :

    intplot(double low, double high, double (*f)(double x)) {

    double const step = (high - low) / 100;

    for (double x = low; x < high; x+=step)std::cout

  • 4.5. MAIN() 63

    4.5 main()

    pi pi pi pi, main():

    int main() {.....}

    pi pipi pi main() pi - ( pi ) :

    int main(int argc, char* argv[]) {........}

    pi ( 4.4.3) :int main(int argc, char** argv) {........}

    pi , argc, pi 1 pi pi pi main() ( 0, pi pi ). , pi char, argc+1 pi C-style string . argv[0] pi pi pi, argv[1], argv[2],. . . pi, ,. . . , - , argv[argc], 0. UNIX ( pi pi pi ) pi pi . , a.out

    ./a.out 12 input.dat output.dat 4.5

    main(), , argc 5, argv :

    argv[0] == "./a.out";argv[1] == "12";argv[2] == "input.dat";argv[3] == "output.dat";argv[4] == "4.5";argv[5] == 0;

    1 4 pi . pi- main() pipi pi. pi pipi C++ header :

    int atoi(char const * p); // C-string to intlong atol(char const * p); // C-string to long intdouble atof(char const * p); // C-string to double

    pi strtod(). pipi namespace std. pi

    #include #include

    intmain(int argc, char *argv[]) {

    int n = std::atoi(argv[1]);// n gets the value of the first argument

  • 64 4.

    double x = std::atof(argv[4]);// x gets the value of the fourth argument

    std::ifstream filein(argv[2]);// open input file. Name is given in argv[2].

    std::ofstream fileout(argv[3]);// open output file. Name is given in argv[3].

    // ..........}

    4.6 overloading

    pipi pi pi pipi pi (), pi, pipi pi. pi pi pi- . C++ (overloading) -pi pi pi , pi pi pi , , . pi pi pi. pi C++ pi pi pi pi double, float long double.

    pi , compilerpi (pi pi) pi pi. pi, , pi pi . pi , pi pi pi (pi.. bool, char, short int int, float double,. . . ). pi , int double, double long double, void*, pi. pi pipi pi , pi pi pi pi , .

    ( pi ) pi pi pi compiler pi pi pi pi pi pi.

    4.7 template

    C++ pi - pi pi templates (pi). - pi pipi pi pi -. pi pi (swap).

  • 4.7. TEMPLATE 65

    pi ,4 (int, float,. . . ), pi pi pi pi (, 6). overloading pi pi pi . pi pi :voidswap(int & a, int & b) {

    int const temp = b;b = a;a = temp;

    }

    voidswap(float & a, float & b) {

    float const temp = b;b = a;a = temp;

    }

    voidswap(double & a, double & b) {

    double const temp = b;b = a;a = temp;

    }

    ...

    ...

    pi - pi pi pi. C++ compiler , pi pi pi (template) pi . template pi pi-:template void swap(T & a, T & b) {

    T const temp = b;b = a;a = temp;

    }

    pi template (pipi ) T (pi pi pipi pi pi) pi. pi pi , pi -, pi pi . pi pi pi pi (pi template):

    4 , std::swap() .

  • 66 4.

    template .5

    template pi pi pi template :

    double a = 2.0;double b = 3.0;

    swap(a,b);

    pipi pi pi template pi pi pi pi pi . pipi pi swap(a,b).

    pi pi, pi template pi pi (int, char, bool,. . . ) , enum.6

    , pi.., pi pipi . pi pi ( ) :

    bool mult(int a, int b) {return !(a%b);

    }

    , mult(a,b). , b , pi template:

    templatebool mult(int a) {

    return !(a%b);}

    mult(a). pi pi

    template. pi pipi pi pi-

    template pi . C++ pipi pi ( - pi pi- pi ) ( ) template pi pi export:

    // util.ccexport template T const & max(T const & a, T const & b) {

    return a>b ? a : b;}

    // util.htemplate T const & max(T const & a, T const & b);5pi Standard 1998 pipi pipi pi tem-

    plate , pi, pi.. template . pi pipi pi pipi pi .

    6 pi .

  • 4.8. INLINE 67

    pi compilers pi export pi pipi pi header - template.

    4.7.1

    pipi pi pi pipi pi template pi (pi.. pi ) pi - pi, pi pi compiler pipi pi pi pi pi. pi.. swap() pi XOR swap pi - pipi. pi pi pi swap() :

    templatevoidswap(int & x, int & y) {

    x=y;y=x;x=y;

    }

    pipi, swap(a,b) pi -, pipi pi .

    4.8 inline

    pi pi pi pi , pi -. C++ compiler pi pi , , pi pi pi pi. pi pi . - compiler pi pi inline pi pi pi. , pi pi :

    inline intmax(int a, int b) {

    return a > b ? a : b;}

    , , inline main(). inline pipi pi com-

    piler pi pi, pi pipi.pi, pipi pi header pi .

  • 68 4.

    4.9 pi

    pi . pi, pi pi . pi pi pi (0 pi ) pi pi , pipi, pi . pi pi static:

    voidfunc(double a) {

    static int howmany = 0;// .....

    ++howmany;}

    howmany pi pi . func().

    4.10 C++

    pi C++ pi . - pi header pi float, double, long double. 4.1 pi- double. , pi . - C pi pi C++ pi. namespace std.

    pi -. pi pi compiler pi pi pi pi . , pipi pi pi pi pi pi pi- , .

    pipi pi pipi pi-pi , pi errno pi pi EDOM. pi , errno ERANGE:

    #include #include #include #include

    int main() {errno = 0; // clear error. No error code is 0.

    std::sqrt(-1.0); // here errno becomes EDOM.

  • 4.10. C++ 69

    pi

    doub

    leabs

    (dou

    ble)

    pi

    .

    doub

    leceil

    (dou

    ble)

    pi

    pi

    .do

    uble

    floo

    r(do

    uble

    )

    pi

    pi

    .do

    uble

    sqrt(d

    oubl

    e)

    .

    pipi

    .

    doub

    lepow(d

    oubl

    ex,

    doub

    lea)

    ,x

    a.

    pi

    a>0

    x=0

    a

    x0

    x=0.

    doub

    lecos(d

    oubl

    e)

    .

    ra

    d.do

    uble

    sin

    (dou

    ble)

    .

    ra

    d.do

    uble

    tan(d

    oubl

    e)

    pi

    .

    ra

    d.do

    uble

    acos(d

    oubl

    e)

    .

    [1

    :1]

    ,pi

    [0:pi

    ]

    rad.

    doub

    leasin

    (dou

    ble)

    .

    [1

    :1]

    ,pi

    [pi/2

    :pi/2

    ]

    rad.

    doub

    leatan(d

    oubl

    e)

    pi

    .

    pi

    [pi/2

    :pi/2

    ]

    rad.

    doub

    leatan2(

    doub

    lex,

    doub

    ley)

    pi

    arct

    an(x/

    y).

    pi

    .

    pi

    [pi

    :pi

    ]

    rad.

    doub

    lecosh(

    doub

    le)

    pi

    .

    doub

    lesin

    h(do

    uble

    )pi

    .

    doub

    letanh(

    doub

    le)

    pi

    pi

    .

    doub

    leexp(d

    oubl

    e)

    .do

    uble

    log(

    doub

    le)

    .

    pi

    .do

    uble

    log1

    0(do

    uble

    )

    .

    pi

    .do

    uble

    modf

    (dou

    ble

    d,do

    uble

    *p)

    d.

    *p.

    doub

    lefr

    exp(

    doub

    led,

    int

    *p)

    x

    [0.5,1

    )

    y

    d

    =x

    2y.

    pi

    x.

    y

    *p.

    doub

    lefm

    od(d

    oubl

    ed,

    doub

    lem)

    pi

    pi

    pi

    d/m.

    pi

    pi

    d.

    doub

    leld

    exp(

    doub

    led,

    int

    i)d2

    i .

    int

    abs

    (int

    )pi

    .

    o

    verlo

    aded

    lo

    ngint.

    div_

    tdi

    v(in

    tn,

    int

    d)

    pi

    pi

    n/d

    .pi

    quot,rem

    pi

    pidiv_t.

    ldiv

    _tdi

    v(lo

    ngin

    tn,

    long

    int

    d)

    pi

    pi

    n/d

    .pi

    quot,rem

    pi

    pildiv_t

    .

    4.

    1:

    .

  • 70 4.

    if (errno == EDOM)std::cerr

  • 4.11. 71

    6. pi ( )

    n`m(r, , ) = Rn`(r)Y`m(, ) .

    ,

    Y`m(, ) =

    2` + 14pi

    (` m)!(` + m)! P

    m` (cos )eim .

    pi Legendre, Pm` (x), pi ` = m

    Pm` (x) = (1)m 1 3 5 (2m 1) (1 x2)m/2 ,

    ` = m + 1Pm` (x) = x(2m + 1)Pmm(x) ,

    pipi pi

    (` m)Pm` (x) = x(2` 1)Pm`1(x) (l + m 1)Pm`2(x) .

    [0, pi] [0, 2pi) . pi 50 100 pipi pi- pi Y`m(, ). pi sin cos , sin sin , cos , Y`m(, )Y`m(, ) (, , x, y, z, ) , ` = 2, m = 0 (, pi d-).

    7. C++ pi ( pi) - f (x) = 0 [xa, xb] pipi f (x). pi :

    () 10. - pi pi , pi pi pi pi .

    () 11. , pi pi pi , pi pi pi pi pi pi f (x). pi pi .

    () Brent12. , pi pi pi pi pi , , . pi pi .

    10http://mathworld.wolfram.com/Bisection.html11http://mathworld.wolfram.com/MethodofFalsePosition.html12http://mathworld.wolfram.com/BrentsMethod.html

  • 72 4.

    8. pi C++ pi pi pi . pi, pi pi pi3 3 , , N N, (pi.. x o) pi pi (, , N-) , , . pipi pi , pi pi. pi pi pi pipi. pi pipi pi pi pi pi. , pipi pi pi , , , , pi . pi pi pi pi

    x|o|x-----

    | |o-----

    o|x|, pipi, pi pi ( ) pi pi.

    9. pi C++ pi pi four-in-a-row pi . pi, pi pi pi M N. pi pi pi. pi pi , . pi , pi.

    4.11.1

    1. C++ pi pi . pi pi . pi, pi pi pi .

    2. pi std::swap() std::min(), std::max() STL, 5.1.3.

    3. pipi pi pi std::vector (pi ) , , pi-pi pi (

  • 4.11. 73

    Bubble sort

    pi pi . O(n2) :1. pi pi ,

    . pi pi , .

    2. pi pi . - , pi pi , .

    3. pi - pi pi .

    , pi pi .

    pi pi , pipi pi ,pi !

    Insertion sort

    pi pi. O(n2) ( pipi ) O(n). :

    1. pi , pi.

    2. pi , ,. . . pi pi (pi ), pi pi.

    Quicksort

    , pi , pi pi . - O(n2) ( pipi ) () O(n log n). pi pi pi , :

    1. pi .

    2. , , , pi pi pipi . pi . .

    3. pi- pi pi- . ,pi.

  • 74 4.

  • 5

    Standard Library

    C++ , pi pi pi pipi, pi pi pi pipi . pi ( 6) pi (templates, 4.7, 6.3), pi Standard Library (STL).

    STL :

    containers, pi , . pi- pi pi pi- pi . picontainers pi pi (pi.. set, map) - (pi.. vector,deque) pipi pi (pi.. map).

    iterators, container. iterators containers pi pi , pi . pi container pipi pi pi pipi .

    pi pi pi pi pi pi , pi container pi pi pi. pi iterators.

    pipi, STL pi pi (adapters) (function objects).

    pi STL pi pi pi headers (pi.. / , - , , pi, ..pi.) pi . pi pi pi C, pi. pi pi STL.

    , pi containers - pi pi .

    75

  • 76 5. STANDARD LIBRARY

    5.1

    5.1.1 (Pair)

    STL pi containers pi pi pi pi pi. pi , header pi, , std::pair. - template pi pi T1,T2 . first second. (pi.. T1 int T2 double) pi pi pi :

    std::pair p1; // p1 == (0, 0.0)std::pair p2(3, 2.0);

    pi pair :

    std::pair p(3, 2.0);

    std::cout

  • 5.1. 77

    pi negate() pi1plus() pi1 + pi2minus() pi1 pi2multiplies() pi1 * pi2divides() pi1 / pi2modulus() pi1 % pi2equal_to() pi1 == pi2not_equal_to() pi1 != pi2less() pi1 < pi2greater() pi1 > pi2less_equal() pi1 = pi2logical_not() !pi1logical_and() pi1&&pi2logical_or() pi1||pi2

    5.1: .

    pi pi pipi pi (adapters), 5.2. .. plus() pi . pi- pipi pi , pi..constval pi pi std::bind1st() :std::bind1st(plus(), constval)

    , pi pi ( pi) pi (!) pi .

    pi bind1st(functor,value) functor(value,parameter)bind2nd(functor,value) functor(parameter,value)not1(functor) !functor(parameter)not2(functor) !functor(parameter1, parameter2)ptr_fun(function) function functor

    5.2: .

    pi pi ( pi pi pi) ptr_fun().

    5.1.3 ,

    header std::min(), std::max() std::swap() templates. :namespace std {// minimum of a, b

  • 78 5. STANDARD LIBRARY

    template T const & min(T const & a, T const & b);

    // maximum of a, btemplate T const & max(T const & a, T const & b);

    // b atemplate void swap(T& a, T& b);

    }

    pi pipi min/max - (

  • 5.2. (CONTAINERS) 79

    5.2 (containers)

    5.2.1

    containers pi pi pi pi. container pi , pi , , pi pi C, pi.

    containers :

    sequence containers pi - pi pipi. pi . .. container pi- , pi container pi . STL pi pi sequence containers: vector, deque list. pipi, string pi pi pi- pi containers pi pipi pi.

    associative containers pi pi pi pi . STL pi pi associativecontainers: set, multiset, map multimap.

    pi pi associative container containers . pi sequence containers .

    containers pi class template (6.3). - container pi pi pi pi pi pi. map multimap pipi pi pi pi. associative containers pi pi- pi . pi pi pi pi- pi (true/false) pi pi . pi pi std::less (5.1.2) pi (

  • 80 5. STANDARD LIBRARY

    std::set c;

    std::vector v;// use c, v ........

    > v >>.

    containers - , . pi, pi pi-, pi pi , pipi, pi pi .

    container

    pi pi , containers: cntr pipi pi container (vector, list, map,. . . ).

    pi , pipi pi- template container (pi , , pi.).

    cntr c1;

    c1 cntr.

    cntr c2;

    // fill c2 ....

    cntr c3(c2);

    c2 cntr, ( pi pi) c3 c2, pi. c2, c3 pipi pi. pi pi pi template.

    pi pi iterator. , pi.. beg, end iterators () container beg end, pi container ( pi pi), [beg,end):cntr c4;

    // fill c4 ...

    // set beg, end on c4

    cntr c5(beg,end);// creates c5 by copying the elements of c4// from beg to (one before) end.

  • 5.2. (CONTAINERS) 81

    pi , T1, T2 pi pi pi pi T1 pi T2. pi.. T2double, pi T1int ( piint double) T1std::complex.

    pipi pi containerspi pi iterators pi . pi pi.. vector pi pi -pi :

    #include

    double a[5] = {0.1, 0.2, 0.5, 0.3, 7.2};

    std::vector v(a, a+5);

    (4.3) pi pi pi n n . , a a[0] a+5 pi a[4].

    pipi pipi, pi pi - pi , container.

    containers

    containers pi . pi pi 5.3. reverseiterator rbegin() pi pi reverse iterator rend() - container .

    pi size() pi empty() true/false container

    ( size()==0 pi pi )max_size() pi

    ( pi pi)begin() iterator pi end() iterator rbegin() reverse iterator rend() reverse iterator pi pi

    5.3: containers STL.

    container (.) container ( ). pi.., pi vector pi pi pi pi:

    void f(std::vector & c) {std::cout

  • 82 5. STANDARD LIBRARY

    pi container ==,!=, , = . container pi , . :

    1. - . pi containers.

    2. , container -.

    3. containers .

    pipi container

    pipi pipi container :

    pi container, pi:c1 = c2;

    c1 c2. c1 pi c2. pi pi .

    container pi pi - std::swap() :

    std::swap(c1,c2);

    swap(), pi, pi, pi :

    c1.swap(c2);

    - insert():c.insert(pos, elem);

    elem pi (sequence containers) pi ( associative containers) pi iterator pos. associative containers pi pi , pi, pi pi pi pi . pi iterator . insert() .

    :

    erase():c.erase(beg,end);

  • 5.2. (CONTAINERS) 83

    pipi iterators - [beg,end) (pi, , ). c sequence container pi iterator pi associative containers pi pi.

    clear(). container. pi void:

    c.clear();

    pi , container pi pipi- .

    Iterators

    pi pi pipi, pi iterators pi pi pi containers.

    pi iterator pi pi pi . iterator pi pi pi pi, pi (*) iterator pi pi pi . , pi *it containerpi iterator it.

    pointers iterators. , , iterator pi (&) . pi pi STL containers. pi, pi..it, pi iterator container (pi.. vector) :

    std::vector::iterator it;

    Iterator pi pi pi :

    std::vector::const_iterator it;

    pipi , it pi pipi pi const. pipi rbegin() rend() con-

    tainer. pi (reverse) iterator. pi (pi pi pi) pi pi container pi containers. pi -pi pi iterator. iterator pi.. vector, :

    std::vector v(10);

    std::vector::reverse_iterator rit = v.rbegin();

    pipi, pi pi const_reverse_iterator ite-rators pi pi pi .

    iterators :

  • 84 5. STANDARD LIBRARY

    random () iterators, pi pi , : pi - ( 2.3), ++it pi it pi , pi iterator pi, random iterators pi container pi , . iterators vector deque .

    bidirectional ( ) iterators pi- :

    1. pi (==) (!=).

    2. pi pi pi , pi-pi ++it --it (, , it++ it--). pi bidirectional iterator pi , pi . iterators list as-sociative containers .

    pi , pi con-tainers, STL pi

    void advance(Iterator it, Difference_type n);

    pi n pi pi.1 iterator it random, - it += n;. bidirectional n ++it ( n>0) --it ( n

  • 5.2. (CONTAINERS) 85

    pi . pi pipi vector pi pi . pi pi pi pi vector pi pipi, , pi, , . pipi, , iterators pi pi .

    pi, pi vector pi pi - .

    std::vector pipi pi header .

    vector pi pi pi . pi pi pi pi .

    std::vector v;

    vector.

    std::vector v(N);

    v vector N pi T. pi- T(), pi pi T. T pi pi 0 (2.2). std::vector v(N, elem);

    v vector N elem. pi, , elem pi pi pi T.

    std::vector v1(v2);

    v1 v2.

    std::vector v(beg,end);

    v pi iteratorsbeg end.

    vector pi - pi pi vector pipi size(), empty() max_size() pi pi :

  • 86 5. STANDARD LIBRARY

    capacity() pi pi pi pi pi pi vector , , - .

    reserve() . pi pi vector . vector. , pi, pi N pi Tpi vector , pi std::vector v;v.reserve(N);

    N push_back(), pi std::vector v(N);

    v[0], v[1], . . . , v[N-1].

    pi pi pipi container . pi- pi vector.

    v1 = v2;

    v2 v1 .

    v.assign(N,elem);

    v N elem -pi , , pi v.

    v.assign(beg, end);

    v container( pi) iterators beg pi end. pi pipi beg end container, pi pi pi pi v beg end.

    v1.swap(v2);

  • 5.2. (CONTAINERS) 87

    std::swap(v1,v2);

    v1, v2.

    v.insert(pos, elem);

    pi pi pi iterator pos elem pi iterator .

    v.insert(pos, N, elem);

    pi pi pi iterator pos N elem pi pi. pi pi pi pi-pi pi.

    pi pi pi insert() iterators:

    v.insert(pos, beg, end);

    , pi pi iterator pos vector v iterators beg (pi), end (pi).

    v.push_back(elem);

    pi pi pi -pi elem pi vector. pi .

    resize() pi . pi pi.

    v.resize(N);

    pi v N pi pi . pipi pi pi pi . , .

    v.resize(N,elem);

    elem.

  • 88 5. STANDARD LIBRARY

    vector :

    resize() pi pi pipi. clear(). erase(). pi container

    pi, pi piiteratorsc.erase(beg,end);

    sequence containers, pi - pi pi iterator, pos:v.erase(pos);

    pipi pi iterator pi.

    push_back() - pop_back(), pi , :v.pop_back();

    , iterators pi pipi.pipi, pi vector , , iterators.

    pi

    pi pi, vector :

    ([]) pi pi :std::vector v(3);

    v[0] = 1.0;v[1] = 3.0;v[2] = v[1] + 5.0;

    at() . pi-, , ,. . . vector v v.at[0], v.at[1],v.at[2],. . . . pi pi pi pipi pi [0:v.size()-1] -pi pi.2 pi pi pi pi pi .

    2 pi pi at().

  • 5.2. (CONTAINERS) 89

    front() back(). pi pi , , pi .

    (*) iterator. piconst_iterator pi pi .

    :, pipi vector pi -

    :#include #include #include

    intmain() {

    std::vector v(10);// v = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}

    // assign valuesfor (std::size_t i = 0; i < v.size(); ++i)

    v[i] = 4.0 * i*i;

    std::vector v2(v);// v2 is a copy of v

    // append more values to v2std::size_t const size = v2.size();for (std::size_t i = size; i < 2*size; ++i)

    v2.push_back(4.0*i*i);

    // print v2std::cout

  • 90 5. STANDARD LIBRARY

    pipi pi pi. , vector pi pipi , deque pi . pi , pi.

    std::deque pipi pi header . deque pi

    pi . pi pi pi pi- pi , vector pi pi pi . pipi, pipi pi , pi vector, . deque pi pi . pi, pi pi pi , - iterator pi , pi .

    deque pipi pi vector. pi capacity() reserve() pi vector. pi pi ( pi) pipi pipi deque , iterators . pi ( iterators).

    capacity() reserve(), pi vector pi pi deque. pipi, pi pi push_front() ( ) pop_front() ( pi ), pi push_back() pop_back().

    : pi deque .

    . pi iterator .

    #include #include

    intmain() {

    std::deque d; // d is empty

    for (int i = 0; i != 10; ++i)d.push_front(i);

    // d is : {9, 8, ..., 0}

    int & r = d.front();std::cout

  • 5.2. (CONTAINERS) 91

  • 92 5. STANDARD LIBRARY

    list pi pi seque-nce containers. .

    std::list c;: .

    std::list c1(c2);: , .

    std::list c(N);: N pi- .

    std::list c(N, elem);: pi N elem.

    std::list c(beg,end);: pi - [beg,end).

    pi list pi pi- :

    c1 = c2;: c2 c1 .

    c.assign(N,elem);: c N - elem.

    c.assign(beg,end);: c - iterators [beg,end).

    c1.swap(c2);: c1, c2.

    std::swap(c1,c2);: swap() pi .

    c.insert(pos,elem);: pi pi iterator pos elem. pi iterator .

    c.insert(pos,N,elem);: pi pi iterator posN elem. pi pi.

    c.insert(pos,beg,end);: pi pi iteratorpos [beg,end). pi pi.

    c.push_back(elem);: elem c.

    c.push_front(elem);: elem c.

    c.resize(N);: N pi c. , pi pi . pi pi.

    c.resize(N,elem);: , pi pipi elem.

  • 5.2. (CONTAINERS) 93

    pi list c, :

    c.pop_back();: . c.pop_front();: pi . c.erase(pos);: iterator pos pi-

    iterator pi.

    c.erase(beg,end);: [beg,end) pi iterator pi.

    c.clear();: c. pi resize().

    pi

    pi list :

    front() back(). pi pi , , pi.

    (*) iterator. piconst_iterator pi pi .

    pipi

    list, pi containers, pi size(), empty() max_size() pi pi pi (5.2.1), pi pi- iterators, pi , pi capacity() reserve().

    list pi pipi pi - sequence containers:

    c.remove(val);

    c val.

    c.remove_if(func);

    pi pi pi , true false. remove_if() list pi func() true.

    unique() pi pi pi.

  • 94 5. STANDARD LIBRARY

    unique(func) pi func() pi true.

    c1.splice(pos, c2);

    pi iterator pos c2, pi c2. c1, c2 pipi list.

    c1.splice(c1pos, c2, c2pos);

    pi iterator c1pos c2 iteratorc2pos. c1, c2 pi .

    c1.splice(c1pos, c2, c2beg, c2end);

    pi iterator c1pos c2 [c2beg, c2end). c1, c2 pi .

    sort() (

  • 5.2. (CONTAINERS) 95

    #include #include

    inline boollessthan10(double a) {return a < 10.0;

    }

    int main() {std::list c; // creates empty list

    for (int i = 0; i != 10; ++i)c.push_back(2.0 * i);

    // c: {0.0, 2.0, 4.0, ..., 18.0}

    c.remove(18.0);// any element with value equal to 18.0 is erased.

    c.remove_if(lessthan10);// all elements with value less than 10 are erased.

    std::cout

  • 96 5. STANDARD LIBRARY

    pi containers, pi , sequence containers - pi pi . (binary search) pi pi pi associative containers O(log n) - pi pipi sequence containers O(n).

    std::set std::multiset pipi pi .

    pi set pi T pi pi pi.3 5.2.1 pi- pipi:

    std::set c;

    set.

    std::set c1(c2);

    set .

    std::set c(beg,end);

    set pi pi container, pi - pi, iterators pi [beg,end).

    pipi pi , std::less pi (

  • 5.2. (CONTAINERS) 97

    set pi :

    c1 = c2;: c2 c1 .

    c1.swap(c2);: c1, c2.

    std::swap(c1,c2);: swap() pi .

    c.insert(elem);: set multiset c elem. c multiset pi iterator . c set pi (pair, 5.1.1), pi pi iterator pi bool pi pi .

    c.insert(pos,elem);: pi elem, - pi pi iterator pos, pi iterator pi .

    c.insert(beg,end);: c [beg,end). pi pi.

    pi set c pi :

    c.erase(elem);: elem pi pi .

    c.erase(pos);: pos. pi pi.

    c.erase(beg,end);: iterators [beg,end). pi pi.

    c.clear();: c.

    pi

    pi pi set pi pi . pi pipi . , pi- pi pipi . iterator pi pi .

    pipi pi pipi pi pipi pi .

  • 98 5. STANDARD LIBRARY

    pipi

    set multiset, pi containers, pi size(), empty() max_size() pi pi pi (5.2.1),pi pi pi iterators. pi- , pi pi - pi pi pi pi pi . :

    count(elem): pi pi elem. find(elem): pi iterator elem ,

    pi, end().

    lower_bound(elem): pi pi pi pi elem.

    upper_bound(elem): pi pi pi pi elem.

    equal_range(elem): pi, , iterators pi pi- pi elem.

    :

    #include #include #include

    intmain() {

    typedef std::set Set;

    // define empty set.Set c;

    // insert values in random order.

    c.insert(5);c.insert(12);c.insert(3);c.insert(6);c.insert(7);c.insert(1);c.insert(9);

    // print setstd::cout

  • 5.2. (CONTAINERS) 99

    // remove elements with value 4, 6 and print informationint howmany = c.erase(4);std::cout

  • 100 5. STANDARD LIBRARY

    pi.4

    std::map c;

    map.

    std::map c1(c2);

    map , pi.

    std::map c(beg,end);

    map pi pi map, pi - pi, iterators pi [beg,end).

    pipi pi -, std::less pi (

  • 5.2. (CONTAINERS) 101

    pi map c pi :

    c.erase(elem);: elem pi pi .

    c.erase(pos);: pos. pi pi.

    c.erase(beg,end);: iterators [beg,end). pi pi.

    c.clear();: c.

    pi

    pi pi map pi pi . pi pi-pi . , pi pipi pi.

    pi iterator. it iterator map (*it).first (, , it->first) (*it).second (, , it->second) pi.

    pipi pi pipi pi - pipi pi .

    map (, multimap) pi set pi pi pi ( []) pi pi. , pi, pi pi pi pi . :

    c map pi pi key c[key] .

    c map pi pi key c[key] = value;

    c make_pair(key,value). , , -pi pipi , pi..

    std::cout

  • 102 5. STANDARD LIBRARY

    #include #include #include #include

    intmain() {

    typedef std::map Map;

    Map birthyear; // Empty map.

    // insert a few pairs.birthyear.insert(std::make_pair("John", 1940));birthyear.insert(std::make_pair("Paul", 1942));birthyear.insert(std::make_pair("George", 1943));

    std::cout

  • 5.3. (ALGORITHMS) 103

    5.3 (algorithms)

    STL pi pi pi pipi . pi , - pi : pipi , , , , pi.

    pi containers. pi pi pi pi iterators , pi, -, containers pi pi pi . , pi: - container , pi , pipi pi . pipi - containers pi: pi , pi.. , pi random iterators (5.2.1). pi pi pi containers. pipi pi pi pi .

    pi pi header . pi pi pi . std.

    pi iterators pi - container pi . pipi pi iterator pi , pi, . pi pi pi iterator pi pi . pi pi container. pipi pi iterator pi pipi container . pipi, - pi pi pipi pi pi , ( pi - pi UnaryFunctor) ( pi BinaryFunctor).

    pi pi :

    accumulate()

    Type accumulate(Iterator beg, Iterator end, Type value)

    Type accumulate(Iterator beg, Iterator end, Type value,BinaryFunctor op)

    pi pi [beg,end) value. {a1, a2, a3, . . . } pi

    value + a1 + a2 + a3 + ...

    pipi pi pi.

    .

    :

  • 104 5. STANDARD LIBRARY

    std::vector v pi pi- :

    double sum = 0.0;for (std::size_t i = 0; i < v.size(); ++i)

    sum += v[i];

    accumulate, pipi pipi

    sum = std::accumulate(v.begin(), v.end(), 0.0);

    inner_product()

    Typeinner_product(Iterator beg1, Iterator end1,

    Iterator beg2, Type value)

    Typeinner_product(Iterator beg1, Iterator end1,

    Iterator beg2, Type value,BinaryFunctor op1, BinaryFunctor op2)

    pi pi value - [beg1,end1) beg2. {a1, a2, a3, . . . } {b1, b2, b3, . . . } pi

    value + a1 * b1 + a2 * b2 + a3 * b3 + ...

    pi pipi , op1(), op2(). pi

    value op1 (a1 op2 b1) op1 (a2 op2 b2) op1 (a3 op2 b3) + ...

    .

    for_each()

    UnaryFunctorfor_each(Iterator beg, Iterator end, Unaryfunctor op)

    op() [beg,end). pi op(). pi op() (pi pi pipi).

    count()

    Difference_typecount(Iterator beg, Iterator end, Type const & value)

    Difference_typecount_if(Iterator beg, Iterator end, UnaryFunctor op)

  • 5.3. (ALGORITHMS) 105

    pi pi [beg,end) pi value. associative containers pi .

    pi pi op() pi true.

    pi Difference_type .1

    min_element()

    Iteratormin_element(Iterator beg, Iterator end)

    Iteratormin_element(Iterator beg, Iterator end, BinaryFunctor op)

    pi iterator ( pi pi ) [beg,end). pi (

  • 106 5. STANDARD LIBRARY

    equal()

    boolequal(Iterator beg1, Iterator end1, Iterator beg2)

    boolequal(Iterator beg1, Iterator end1, Iterator beg2,

    BinaryFunctor op)

    pi pi true false [beg1,end1) pi pi beg2.

    op(), pi pi .

    copy()

    Iteratorcopy(Iterator beg1, Iterator end1, Iterator beg2)

    [beg1, end1) pi beg2. pi iterator pi pi pi .

    beg2 pi container pipi [beg1, end1).

    copy_backward()

    Iteratorcopy_backward(Iterator beg1, Iterator end1, Iterator end2)

    [beg1, end1) pi - end2, pi . pi iterator pi pi pi .

    end2 pi container pipi [beg1, end1).

    transform()

    Iteratortransform(Iterator beg1, Iterator end1, Iterator beg2,

    UnaryFunctor op)

    Iteratortransform(Iterator beg1, Iterator end1, Iterator beg2,

    Iterator beg3,BinaryFunctor op)

    pi op() [beg1,end1) pi pi beg2. beg1, beg2 pi .

    op() [beg1,end1) pi

  • 5.3. (ALGORITHMS) 107

    beg2 pi pi beg3. beg1, beg2, beg3 pi .

    pi iterator pi pi - pi .

    fill()

    voidfill(Iterator beg, Iterator end, Type const & value)

    voidfill_n(Iterator beg, Size N, Type const & value)

    pi value [beg,end). value N pi beg.

    generate()

    voidgenerate(Iterator beg, Iterator end, Functor op)

    voidgenerate_n(Iterator beg, Size N, Functor op)

    pi pi op() ( pi ) [beg,end). pi op() N - pi beg.

    replace()

    voidreplace(Iterator beg, Iterator end,

    Type const & oldvalue, Type const & newvalue)

    voidreplace_if(Iterator beg, Iterator end,

    UnaryFunctor op, Type const & newvalue)

    pi newvalue [beg,end) pi oldvalue. pi - pi op pi true.

    replace_copy()

    voidreplace_copy(Iterator beg1, Iterator end1, Iterator beg2,

    Type const & oldvalue, Type const & newvalue)

    voidreplace_copy_if(Iterator beg1, Iterator end1, Iterator beg2,

    UnaryFunctor op, Type const & newvalue)

  • 108 5. STANDARD LIBRARY

    pi [beg1,end1) pi beg2 pi newvalue oldvalue. pi pi op pi true.

    remove()Iteratorremove(Iterator beg, Iterator end, Type const & value)

    Iteratorremove_if(Iterator beg, Iterator end, UnaryFunctor op)

    pi [beg,end) value. pi pi op pi true. pi pi .

    pi iterator pi - pi . pi, pi beg piiterator pi .

    , pi pi associative containers. pi pi (erase()).

    list pi (remove()) pi -.

    reverse()voidreverse(Iterator beg, Iterator end)

    [beg,end). list pi .

    reverse_copy()Iteratorreverse(Iterator beg1, Iterator end1, Iterator beg2)

    [beg1,end1) pi beg2. pi pi container.

    merge()Iteratormerge(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2, Iterator beg3)

    Iteratormerge(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2, Iterator beg3,BinaryFunctor op)

  • 5.3. (ALGORITHMS) 109

    pi [beg1,end1) [beg2,end2) pi beg3, pi . pi op() pi true false pi pi .

    pi pi container.

    list pi .

    set_union()Iteratorset_union(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2, Iterator beg3)

    Iteratorset_union(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2, Iterator beg3,BinaryFunctor op)

    merge() pi . pi pi, pi .

    set_intersection()Iteratorset_intersection(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2,Iterator beg3)

    Iteratorset_intersection(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2,Iterator beg3, BinaryFunctor op)

    merge() - .

    set_difference()Iteratorset_difference(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2,Iterator beg3)

    Iteratorset_difference(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2,Iterator beg3, BinaryFunctor op)

    merge() - pi pi pi .

  • 110 5. STANDARD LIBRARY

    set_symmetric_difference()

    Iteratorset_symmetric_difference(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2,Iterator beg3)

    Iteratorset_symmetric_difference(Iterator beg1, Iterator end1,

    Iterator beg2, Iterator end2,Iterator beg3, BinaryFunctor op)

    merge() - pi pi .

    binary_search()

    boolbinary_search(Iterator beg, Iterator end, Type const & value)

    boolbinary_search(Iterator beg, Iterator end, Type const & value,

    BinaryFunctor op)

    pi pi true false - [beg,end) pi value.

    , op(), pi - pi , pi pi .

    sort()

    voidsort(RandomIterator beg, RandomIterator end)

    voidsort(RandomIterator beg, RandomIterator end,

    BinaryFunctor op)

    [beg,end). pi, (

  • 5.4. 111

    voidstable_sort(RandomIterator beg, RandomIterator end,

    BinaryFunctor op)

    sort() .

    partial_sort()

    voidpartial_sort(RandomIterator beg, RandomIterator sortEnd,

    RandomIterator end)

    voidpartial_sort(RandomIterator beg, RandomIterator sortEnd,

    RandomIterator end, BinaryFunctor op)

    sort() pi [beg,sortEnd) .

    random_shuffle()

    voidrandom_shuffle(RandomIterator beg, RandomIterator end)

    pi [beg,end). pi random iterators pi pi

    list associative containers.

    5.4

    1. 1000 .

    () pi pi pi -. pi container(pi pi pi) pi .

    () (pi clock()5 pi ). pi;

    () pi pi .

    () pi .() pi pi pi 0.() pi 0.() pi pi pi .

    2. pi pi list: unique(), splice(), merge(), reverse().

    5 Unix man 3 clock .

  • 112 5. STANDARD LIBRARY

    3. pi pi STL. - . pi;

    4. pi . pi pipi ( ) ( pi, , pipi pi).

    5. pi : pi , pi, ( ), -, pi, pipi. pi pi , pi pi .

  • 6

    6.1

    pi pi pi C++ pi pi (object based) pi pi (object orientedprogramming). , , pi, pi,pi pi pi pi pi pi pi pi pi.

    pi pi pi pi pi-, pipi pi, pi , pi pi pi, pi. , pi-, (pi pi , pi , , ,pi.), pi (pi pi , -, , , pi.), pi pi . pi ( pi ) -pi pi pi pi pi pi . .. C++

    std::size_t const N = 50; // number of studentsstd::string studentName[N];int studentAM[N];int studentYear[N];// .......

    std::size_t const M = 500; // number of booksstd::string bookTitle[M];std::string bookAythor[M];int bookYear[M];// .......

    pi pipi pi ( ) pi pi, pipi , pi pi pi

    113

  • 114 6.

    pi . - pi pi , compiler pi . pi pipi pi ( , , pi. , , pi , -, , pi.) , , , , pi. , pi, pipi , pi pi pi pi pi pi.

    , pi pi pi pi- pi pi pi -. pi pi pi pi: pi, - pi pi ! pi pi pi C++ struct, 2.6.2, pi struct Student {

    std::string name;int AM;int Year;

    // ......};

    struct Book {std::string title;std::string author;int year;

    // ......};

    pi -pi pi pi pi, - pi pi . pi pi :std::size_t const N = 50;std::size_t const M = 500;

    Student students[N];Book books[M];

    pi : , - , pi pi pi (pi.. pi ) pi Student pi pi Book.

    int printStudent(Student const & s);int printBook(Book const & c);

    , pi, pi . pi - pi,

  • 6.1. 115

    pi pi pi pi pi . pi..,pi pi , pi ( pi ) (,pi, pi) pi , pi-pi , pi pipi . pi pi pi pi- pi pi pi pi - . C++ pi pi (constructor), pi- pi.

    pi pi pi pipi pi pipi pi . pi pi pipi pi pi , pipi pi pi pipi pi - pipi pi ( pi , pi pi pi, pi.). pi pi , pi pipipi . pi pipi pi pi (encap-sulation).

    C++ pi struct pi pi C pipi pi . pipi, class struct , pi : pi , , .1 pi pi pipi pi pi pipiclass Student {

    std::string name;int AM;int Year;

    // ......

    public:int print() {....}// former int printStudent(Student const & s);std::string getName() {....}int getAM() {....}int getYear() {....}

    // ......};

    class Book {

    1 struct pi pi pi .

  • 116 6.

    public:int print() {....} // former int printBook(Book const & c);std::string getTitle() {....}std::string getAuthor() {....}int getYear() {....}

    // ......

    private:std::string title;std::string author;int year;

    // ......};

    pi . ( , pi ) printStudent() printBook() pipi ( pi ) pi ( pi - pi ). struct pi (.). . , print() pi pi Student :Student s;s.print();

    pi s print() pi- pi . , public: pi ( pi ) pipi ( pi ) pi pipi pi . , pi { pi private: pi pi pi .

    pipi, pi pi pi pipi pi , pi pi pi . pi ( pi ) , pi..class Student {

    std::string name;// ......public:

    std::string getName() {return name;}// ......};

    pi pi - pi C++ pi pi .

    pi pi pi pi- pipi pipi pipi pi pi ,

  • 6.2. 117

    pipi, , , . pi . pi, , pi pi, , pi pi pi . pi pi pi pipi pi pi- .

    pi pi pi pi - pipi pi pi pi pi . pi,pi, pi ,, pipi, (, inheritance) pi, pi, pi pi pi . pi pi , pi, pipi pipi pi pi pi . pi , pi pi , pi ( -), , pipi, pi pi- pi . pi pi pi pi , , , pi - pi , pi pi . pi (polymorphism).

    pi , pi pi pi- pi - pi pi. pi - pi , pi pi pi pi . pi - , pi ( pi pi pi ) pi pi pi . pi pi- pi pi C++ pi pipipi.

    6.2

    pi, pi pipi, Student pi .

    class Student {public:

    Student(std::string const & onoma, int am, int y): name(onoma), AM(am), Year(y) {}

    Student() {}Student(Student const & other);

    Student & operator=(Student const &other);

  • 118 6.

    int print() const {std::cout

  • 6.2. 119

    s Student, pi

    Student & s1 = s;Student const & s2 = s;

    s1.setYear(2000); // corrects2.setYear(2000); // error

    setYear() s1 pi s2 pi-pi.

    pi , , const pi, pi pi pipipi const.

    pi pi (Student(), Student(), operator=()) pi .

    pi pi pi , pi pi pi pi , private pi, , public .pi , ( pi) (, , public) pi . pi , pi . pi, Student pi print() setYear() pi :

    class Student {public:// ............

    int print() const;void setYear(int y);

    // ............private:

    std::string name;int AM;int Year;

    };

    intStudent::print() const {

    std::cout

  • 120 6.

    pi pipi pi pi ( Student::). pi inline, 4.8 , pipi pi inline .

    , , pipi header (pi.. Stu-dent.h) pi ( inline, 4.8) (pi.. Student.cc) pi . -, pi pi pi ( #include "Student.h") header pi pi - .

    6.2.1 Constructor

    6.2.2 Destructor

    6.2.3 Copy constructor, assignment operator

    6.2.4 Other operators, overloading

    6.3 template

  • pi. . . pi!

    pi pi, , pi Cpi . C++ pi pipi pi pi . - pi 1. pi

    Raymond Cheong 2001, .1. pi - ( ).

    Michael Savastio 1995, .2. pi pi- 429539.

    Ken Huffman 1996, .3. pi Braille .

    #include int l;int main(int o,char **O,int I){char c,*D=O[1];if(o>0){for(l=0;D[l ];D[l++]-=10){D [l++]-=120;D[l]-=110;while (!main(0,O,l))D[l]+= 20; putchar((D[l]+1032)/20 ) ;}putchar(10);}else{c=o+ (D[I]+82)%10-(I>l/2)*(D[I-l+I]+72)/10-9;D[I]+=I

  • 122 . . . . !

    #include

    #define l11l 0xFFFF#define ll1 for#define ll111 if#define l1l1 unsigned#define l111 struct#define lll11 short#define ll11l long#define ll1ll putchar#define l1l1l(l) l=malloc(sizeof(l111 llll1));l->lll1l=1-1;l->ll1l1=1-1;#define l1ll1 *lllll++=l1ll%10000;l1ll/=10000;#define l1lll ll111(!l1->lll1l){l1l1l(l1->lll1l);l1->lll1l->ll1l1=l1;}\lllll=(l1=l1->lll1l)->lll;ll=1-1;#define llll 1000

    l111 llll1 {l111 llll1 *

    lll1l,*ll1l1 ;l1l1 lll11 lll [llll];};main (){l111 llll1 *ll11,*l1l,*l1, *ll1l, * malloc ( ) ; l1l1 ll11l l1ll ;ll11l l11,ll ,l;l1l1 lll11 *lll1,* lllll; ll1(l=1-1 ;l< 14; ll1ll("\t\"8)>l\"9!.)>vl" [l]L),++l);scanf("%d",&l);l1l1l(l1l) l1l1l(ll11 ) (l1=l1l)->lll[l1l->lll[1-1] =1]=l11l;ll1(l11 =1+1;l11lll; lllll =( l1l=l1)->lll; ll=(l1ll=1-1);ll1(;ll1l-> lll1l||l11l!= *lll1;){l1ll+=l11**lll1++ ;l1ll1 ll111 (++ll>llll){l1lll lll1=( ll1l =ll1l-> lll1l)->lll;}}ll1(;l1ll; ){l1ll1 ll111 (++ll>=llll){ l1lll} } * lllll=l11l;}ll1(l=(ll=1- 1);(llll[ l] !=l11l);++l); ll1 (;l1;l1=l1->ll1l1,l= llll){ll1(--l ;l>=1-1;--l,++ll)printf( (ll)?((ll%19) ?"%04d":(ll=19,"\n%04d") ):"%4d",l1-> lll[l] ) ; }

    ll1ll(10); }

    A.2: savastio.c

  • 123

    #define x char#define z else#define w gets#define r if#define u int#define s main#define v putchar#define y while#define t " A?B?K?L?CIF?MSP?E?H?O?R?DJG?NTQ?????U?V?????X???????Z????W??Y??"s ( ) { x* c , b[ 5 * 72 ]; u a, e , d [ 9*9 *9 ] ; y (w ( b) ){ r ( 0 [ b] -7 *5 ) { c = b ; y ( (* c - 6* 7 )* * c )c = c+ 1 ; r (( -0 ) [ c ] && w ( b +8 * 5* 3 ) && w (b +8 *5 * 6) ){ a = 0; y ( a [ b ]) { a [ d ] = ! ! ( a[ b] - 4 *8 ) ; a = a +1; } y (a < 8 * 5 * 3 ) d [ ( a ++ )] = 0 ; a = 0; y( b [a + 8 *3 * 5 ] ) { d [ a ] = a [ d] + ! !( b[a+ 40 * 3 ] -4 * 8) * 2 ; ++ a ; } a =0 ; y ( a [ b + 6 * 40 ]) {a [d ] += ! ! ( b[ a + 5 * 6 * 8 ] - 4 *8 ) * 4; a = a + 1 ;} a= 0; y (a 2 ? 2 :a [ d ] ] :6 * 8 +( t [ d[a ]/ 8 /8 ] -4 ) %( 10 ) ) ; r( ! (2 [a+ d] + 3 [d + a ] ) ) v ( 4 * 8 ); } a = a + 1 ; } v (5 * 2 ) ; } z {c =b ; e= 0 ; y (* c ) { * c += - (* c > 8 * 12

    ) * 32 ; a = 8 * 8 ; r (* c>= 48 &&* c0 &&*c- a[ t] ); d[ (e ++ )] =4 ;(* ( d + (e ++) ))= 07 ;}z y(a -- >1&&* c- t[a ]); d[ ( e = e + 1) -1] =a %8 ;y (a /= 8) d[ (e ++) ] = a% 8 ; ++ c ; * ( e ++ +d ) =0;} -- e ;r ( e >0 ) {a = 1 ;y ( a< 8 ) { c = b ;y( c

  • 124 . . . . !

  • Fortran C

    C++ pi pi. - pi pi compilers pi pi, pipi pi . Fortran 77 C pi - pi C. compilers Fortran 90/95 pi pipi pi . Fortran 77 compiler pi pi pi pi1.

    pi, pi (main()) compiler C++, , , pi pi-pi pi main() ( C) PROGRAM( Fortran).

    .1 C

    pipi pi pi pi C pi-, , compiler C++. pipi pipi C pi pipi pi C++. pi, pi - pi 4.

    pipi pi pipi pi compiler C - pi pi objectfile library ( ) :pi -pi header . pi compiler C++ pipi C pi extern "C".

    1 pi, Fortran compiler NAG pi pipi CHARACTER .

    125

  • 126 . FORTRAN C

    : f,g

    int f(double a) {.....

    }

    void g(int a) {.....

    }

    compiler C. C++ pi extern "C"int f(double a);

    extern "C"void g(int a);

    , , extern "C" {

    int f(double a);

    void g(int a);}

    pipi C++. linking pipi pi object library pi pi . pi.. UNIX compilers GNU Project, pi pi pi prog.cc fg.c,

    gcc -c fg.cg++ prog.cc fg.o

    pi pi pi object, .o, pi C++ pi . compilers .

    .2 Fortran

    pi Fortran 77 pi. - pi pipi pi . pi:

    Fortran, blas lapack. pi pi pi, (pipi pi, pipi, pi.). pi pi pi , pi. pi . pi dlasrt() pi pi pi pi . pi pi lapack2

    2 UNIX man dlasrt pi pi .

  • .2. FORTRAN 127

    SUBROUTINE DLASRT( ID, N, D, INFO )

    CHARACTER IDINTEGER INFO, NDOUBLE PRECISION D( * )

    pi

    ID (input) CHARACTER*1= I: sort D in increasing order;= D: sort D in decreasing order.

    N (input) INTEGERThe length of the array D.

    D (input/output) DOUBLE PRECISION array, dimension (N)On entry, the array to be sorted.On exit, D has been sorted into increasing order(D(1) = D(N) ), depending on ID.

    INFO (output) INTEGER= 0: successful exit< 0: if INFO = -i, the i-th argument had

    an illegal value

    C++ (pi , pi pi compilers):

    Fortran C++INTEGER int

    REAL float

    DOUBLE PRECISION double

    LOGICAL bool

    CHARACTER char

    COMPLEX std::complex

    dlasrt SUBROUTINE pi C++ void. void dlasrt(char id, int n, double d[], int info);

    pi . - pi pi . pi, C++ pi- column-major order ( pi ). 2.6.1 pi pi pi. pipi pi pi std::vector pi pipi pi pi , , v std::vector pi &v[0].

    pi pi pi pi , id,n, pi , pipipi . , pi, pi const.

  • 128 . FORTRAN C

    Fortran pi pi-pi pi . pi pi C++ (4.2) ( pi pi , pi ). pipi void dlasrt(char const & id, int const & n, double d[], int & info);

    compiler Fortran pipi C pipi pi extern "C". compiler pi pi pi- pi .

    GNU compilers pi pipi : pi pi pi _ pi, pi. pipi compilers extern "C"void dlasrt_(char const & id, int const & n, double d[], int & info);

    pipi , pi pi pi. (pi lapack blas library) ( UNIX)

    g++ prog.cc -llapack -lblas

    pi, Fortran 2003 pi C (, pi, C++).

    pipi pipi pi - compilers pi pi.

  • pi

    pi pi pi pi -.

    3

    4. pi pi 8 .#include #include

    intmain() {

    std::cout

  • 130 .

    #include

    intmain(){

    std::ifstream random("rndnumbers");

    int r;int neg = 0;int zer = 0;int pos = 0;

    while (random >> r) { // when read fails, random is falseif (r < 0) ++neg;if (r == 0) ++zer;if (r > 0) ++pos;

    }std::cout

  • 131

    6.3. pi C++ pi - .#include

    // greatest common divisor, Euclids algorithm: gcd(N,M) = gcd(M, N mod M)intmain(){

    std::cout > m >> n;

    while (n != 0) {const int temp = m;m = n;n = temp % n;

    }

    std::cout

  • 132 .

    double a,b,c;std::cin >> a >> b >> c;

    if (a == 0.0) {std::cerr large * 4.0 * a * c) && (c != 0.0) ) // b2 >> 4acif (b > 0.0) // then x1 is not accurate

    x1 = 2.0 * c / (-b - sqrtD);else // x2 is not accurate

    x2 = 2.0 * c / (-b + sqrtD);

    std::cout

  • 133

    if (generation == maxgen-1) break;// In the last generation dont calculate the new one.

    // new generation

    int newgen[M][N];

    for (int i = 0; i < M; ++i) {for (int j = 0; j < N; ++j) {

    int neigh = 0;

    if (i-1 >= 0)neigh += gen[i-1][j];

    if (i+1 < M)neigh += gen[i+1][j];

    if (j-1 >= 0)neigh += gen[i][j-1];

    if (j+1 < N)neigh += gen[i][j+1];

    if ((i-1 >= 0) && (j-1 >= 0))neigh += gen[i-1][j-1];

    if ((i-1 >= 0) && (j+1 < N))neigh += gen[i-1][j+1];

    if ((i+1 < M) && (j-1 >= 0))neigh += gen[i+1][j-1];

    if ((i+1 < M) && (j+1 < N))neigh += gen[i+1][j+1];

    if ((neigh == 3) || (gen[i][j] && (neigh == 2)))newgen[i][j] = 1;

    elsenewgen[i][j] = 0;

    }}

    // copy new generation to oldfor (int i = 0; i < M; ++i) {

    for (int j = 0; j < N; ++j) {gen[i][j] = newgen[i][j];

    }}

    }}

    4

    4. pi pi .#include

    intcopy(std::vector const & A, int N, std::vector & Aij,

    int row, int col);

    doubledeterminant(std::vector const & A, int N) {

    if (N==1) return A[0];

    std::vector Aij((N-1)*(N-1));

    int const col = 0;

  • 134 .

    int sign = 1; // (-1)(0+col)

    double sum = 0.0;for (int i = 0; i < N; ++i) {

    copy(A, N, Aij, i, col);

    sum += sign * A[i+col*N] * determinant(Aij, N-1);

    sign = -sign;}

    return sum;}

    intcopy(std::vector const & A, int N, std::vector & Aij,

    int row, int col) {int in = 0;int jn = 0;

    for (int i = 0; i < N; ++i) {if (i == row) continue;for (int j = 0; j < N; ++j) {

    if (j == col) continue;

    Aij[in+jn*(N-1)] = A[i+j*N];++jn;

    }++in;

    }

    return 0;}

  • [1] Nicolai M. Josuttis. The C++ Standard Library: A Tutorial and Reference.Addison Wesley, Reading, MA, USA, September 1999.

    [2] Herb Sutter. More Exceptional C++: 40 More Engineering Puzzles, Pro-gramming Problems, and Solutions. C++ in Depth Series. Addison Wesley,Reading, MA, USA, January 2002.

    [3] Scott Meyers. Effective STL: 50 Specific Ways to Improve the Use of theStandard Template Library. Professional Computing Series. Addison We-sley, Reading, MA, USA, July 2001.

    [4] Andrei Alexandrescu. Modern C++ Design: Applied Generic and Design Pat-terns. C++ In-Depth Series. Addison Wesley, Reading, MA, USA, January2001.

    [5] Matthew H. Austern. Generic Programming and the STL: Using and Exten-ding the C++ Standard Template Library. Professional Computing Series.Addison Wesley, Reading, MA, USA, January 1999.

    [6] John J. Barton and Lee R. Nackman. Engineering and Scientific C++: AnIntroduction with Advanced Techniques and Examples. Addison Wesley,Reading, MA, USA, 1994.

    [7] Dov Bulka and David Mayhew. Efficient C++ Performance ProgrammingTechniques. Addison Wesley, Reading, MA, USA, November 1999.

    [8] Bruce Eckel. Thinking in C++. Introduction to Standard C++, volume 1.Prentice Hall, second edition, 2000. Also available as electronic book.

    [9] Bruce Eckel and Chuck Allison. Thinking in C++. Practical Programming,volume 2. Prentice Hall, second edition, 2003. Also available as electronicbook.

    [10] John R. Hubbard. Programming with C++. Schaums Outline Series.McGrawHill, second edition, June 2000.

    [11] John R. Hubbard. Fundamentals of Computing with C++. Schaums Out-line Series. McGrawHill, May 1998.

    [12] Nicolai M. Josuttis. Object-Oriented Programming in C++. John Wiley andSons Ltd, November 2002.

    135

  • 136

    [13] Andrew Koenig and Barbara E. Moo. Accelerated C++: practical program-ming by example. C++ In-Depth Series. Addison Wesley, Reading, MA,USA, 2000.

    [14] Stanley B. Lippman. Essential C++. C++ In-Depth Series. Addison Wesley,Reading, MA, USA, 2000.

    [15] Stanley B. Lippman, Josee Lajoie, and Barbara E. Moo. C++ Primer. Addi-son Wesley, Reading, MA, USA, fourth edition, February 2005.

    [16] Scott Meyers. Effective C++: 55 Specific Ways to Improve Your Programsand Designs. Professional Computing Series. Addison Wesley, Reading,MA, USA, third edition, June 2005.

    [17] Scott Meyers. More Effective C++: 35 New Ways to Improve Your Programsand Designs. Professional Computing Series. Addison Wesley, Reading,MA, USA, March 1996.

    [18] Trevor Misfeldt, Gregory Bumgardner, and Andrew Gray. The Elements ofC++ Style. SIGS Reference Library. Cambridge University Press, 2002.

    [19] Bjarne Stroustrup. The C++ Programming Language. Addison Wesley,Reading, MA, USA, third edition, 1997.

    [20] Bjarne Stroustrup. Learning Standard C++ as a New Language. The C/C++Users Journal, May 1999. Also available in CVU, Vol. 12, No. 1, January2000.

    [21] Herb Sutter. Exceptional C++ Style: 40 New Engineering Puzzles, Pro-gramming Problems and Solutions. C++ In-Depth Series. Addison Wesley,Reading, MA, USA, August 2004.

    [22] Herb Sutter and Andrei Alexandrescu. C++ Coding Standards: Rules, Gui-delines, and Best Practices. C++ In-Depth Series. Addison Wesley, Reading,MA, USA, November 2004.

    [23] Herb Sutter. Exceptional C++. C++ In-Depth Series. Addison Wesley, Rea-ding, MA, USA, October 1999.

    [24] David Vandevoorde and Nicolai M. Josuttis. C++ Templates: The CompleteGuide. Addison Wesley, Reading, MA, USA, December 2002.

    [25] David Vandevoorde. C++ Solutions: Companion to the C++ ProgrammingLanguage. Addison Wesley, Reading, MA, USA, August 1998.

    [26] D. Yang. C++ and Object-Oriented Numeric Computing for Scientists andEngineers. Springer-Verlag, New York, January 2001.

  • 2.1 C++. . . . . . . . . . . . . . . . . . . . . 62.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.3 C++. . . . . . . . . . . . . . . . . . . . . . . . 132.4 pi . . . . . . . . . . . . . . . . . . . . 232.5 bit C++. . . . . . . . . . . . . . . . . . . . . . . . . . . 26

    4.1 . . . . . . . . . . . . . . . 69

    5.1 . . . . . . . . . . . . . . . 775.2 . . . . . . . . . . . . . . 775.3 containers STL. . . . . . . . . . . 81

    137

  • !, 13

  • 139

    adaptersbind1st(), 77bind2nd(), 77not1(), 77not2(), 77ptr_fun(), 77

    caststatic_cast, 22

    double, pi pi pifloat, pi pi pifunction object

    pidivides(), 76equal_to(), 76greater(), 76greater_equal(), 76less(), 76less_equal(), 76logical_and(), 76logical_not(), 76logical_or(), 76minus(), 76modulus(), 76multiplies(), 76negate(), 76not_equal_to(), 76plus(), 76

    functor, pi function objectiterators

    bidirectional, 84random, 84

    long double, pi pi pi

    piint, 9long int, 9short int, 9

    Fibonacci, 44

    bubble sort, 73insertion sort, 73quicksort, 73

    , pi reference, pi function

    object , 21

    , 7, pi struct

    , 115

    , 117

    , 45

    Boole, 46Durand, 46Simpson, 46pi, 46

    , 117

    , 82

    Brent, 71, 71 , 71

    pi, pi cast

    pi, 117pi pi

    double, 10float, 10long double, 10

    , 35

    , 54pi, 57, 57

    , 60, 54

    bit, 25, 25&, 25, 25|, 25

Recommended

View more >