++

         

string - 4


3.12

:

(a) char ch = "The long and winding road";

(b) int ival = &ch;

(c) char *pc = &ival;

(d) string st( &ch );

(e) pc = 0; (i) pc = '0';

(f) st = pc; (j) st = &ival;

(g) ch = pc[0]; (k) ch = *pc;

(h) pc = st; (l) *pc = ival;

3.13

:

while ( st++ )

++cnt;

while ( *st++ )

++cnt;

3.14

. ,   string:

// ***** C- *****

#include <iostream>

#include <cstring>

int main()

{

int errors = 0;

const char *pc = "a very long literal string";

for ( int ix = 0; ix < 1000000; ++ix )

{

int len = strlen( pc );

char *pc2 = new char[ len + 1 ];

strcpy( pc2, pc );

if ( strcmp( pc2, pc ))

++errors;

delete [] pc2;

}

cout << "C-: "

<< errors << " .\n";

}

// ***** string *****

#include <iostream>

#include <string>

int main()

{

int errors = 0;

string str( "a very long literal string" );

for ( int ix = 0; ix < 1000000; ++ix )

{

int len = str.size();

string str2 = str;

if ( str != str2 )

}

cout << " string: "

<< errors << " .\n;

}

?

, . ? ?

3.15

- string, ? .