.


:




:

































 

 

 

 





string . :

string();

string(const char *);

string(const char *, int n);

string(string &);

string, - string -, - string n , . , , .

string :

string& operator=(const string& str);

string& operator=(const char* s);

string& operator=(char c);

, string, - , :

string s1;

string s2("");

string s3(s2);

s1 = 'X';

s1 = "";

s2 = s3;

, .

1.6 string :

1.6 - string.

= >
+ >=
== [ ]
!= <<
< >>
<= +=

 

string , . (, , ) .

, (, , , ) string , .

assign:

assign(const string& str);

assign(const string& str, size_type pos, size_type n);

assign(const char* s, size_type n);

str ( ):

string s1(""), s2;

s2.assign(s1); // s2 = s1;

n str, pos. pos + n , , . pos , out_of_range.

n C- s.

append:

append(const string& str);

append(const string& str, size_type pos, size_type n);

append(const char* s, size_type n);

str ( +).

n str, pos. pos + n , , . pos , out_of_range. , length_error.

n C- s.

insert:

insert(size_type pos1, const string& str);

insert(size_type posl, const string& str, size_type pos2, size_type n);

insert(size_type pos, const char* s, size_type n);

, posl, str. , , pos1 , str , .

pos1 , out_of_range. , length_error.

, posl, n str, pos2. , , pos1 , n str, pos2, .

n str, pos2 .. posl pos2 , out_of_range. , length_error.

, pos, n - s.

erase:

erase(size_type pos = 0, size_type n = npos);

n , pos. pos , . n, .

npos string size_type.

clear:

void clear();

replace:

replace(size_type pos1, size_type n1, const string& str);

replace(size_type pos1, size_type n1, const string& str, size_type pos2, size_type n2);

replace(size_type pos1, size_type n1, const char* s, size_type n2);

n1 , pos1, str .

n1 , pos1, n2 str, pos2.

pos1 pos2 , out_of_range. , length_error.

nl n2 - s.

: .

#include <string.h>

#include <iostream.h>

int main (){

string s1(" "), s2("e"), s3("opoa");

cout << "s1= " << s1 << end;

cout << "s2= " << s2 << end;

cout << "s3= " << s3 << end;

cout << " insert:" << end;

cout << "s3= " << s3.insert(4, s2) << end;

cout << "s3= " << s3.insert(7, "") << end;

s1.erase(0,3);

cout << " erase:" << end;

cout << "s1= " << s1.erase(12,2) << end;

cout << " replace:" << end;

cout << "s1= " << s1.replace(0,3, s3, 4, 2) << end;

return 0; }

:

s1=

s2=

s3=

insert:

s3=

s3=

erase:

s1=

replace:

s1=

swap;

swap(string& s);

s.

substr:

string substr(size_type pos = 0, size_type n = npos) const;

n, pos. pos , out_of_range. n , .

string C- :

const char* c_str() const;

const char* data() const;

size_type (char* s, size_type n, size_type pos = 0) const;

-, string, -. , .. , , -.

-.

s n , pos. - . .

string , -.

: - string.

#include <string.h>

#include <iostream.h>

int main (){

string s1("hello");

const char *pc = ", ";

string s2("world");

string s3 = s1 + pc + s2 + "\n";

return 0;

}

- string. - string:

string s1;

const char *pc = "a character array";

s1 = pc;

, , . - string :

char *str = s1;

, c_str():

char *str = s1.c_str(); //

const char *str = s1.c_str(); //

c_str() , string , -.

( char *) , .. c_str() . ( const char *) .

. :

size_type find(const string& str, size_type pos = 0) const;

str , pos, npos, .

size_type find(char , size_type pos = 0) const;

, pos, npos, .

size_type rfind(const str1ng& str, size_type pos = npos) const;

str , pos, npos, .

size_type rfind(char , size_type pos = npos) const;

, pos, npos, .

size_type find_first_of(const string& str, size_type pos = 0) const;

str , pos, npos, .

size_type find_first_of(char , size_type pos = 0) const;

, pos, npos, .

size_type find_last_of(const string& str, size_type pos = npos) const;

str , pos, npos, .

size_type find_last_of(char , size_type pos = npos) const;

, pos, npos, .

size_type find_first_not_of(const string& str, size_type pos = 0) const;

, pos, str .

size_type find_first_not_of(char , size_type pos = 0) const;

, pos, .

size_type find_last_not_of(const string& str, size_type pos = npos) const;

pos, str .

size_type find_last_not_of(char , size_type pos = npos) const;

pos, .

, -.

: .

#include <string.h>

#include <iostream.h>

int main (){

string s1("eca "), s2("e");

cout << "s1= " << s1 << end;

cout << "s2= " << s2 << end;

int i = s1.find(s2);

int j = sl.rfind(s2);

cout << " s2 s1 " << i << end;

cout << " s2 s1" << j << end;

cout << " s1 " << s1.find ('o') << end;

cout << " s1 " << s1.rfind ('') << end;

cout << " s1 " << s1.find_first_of("ae") << end;

cout << " s1 " << s1.find_last_of("ae") << end;

}

:

s1=

s2=

s2 s1 0

s2 s1 11

s1 8

s1 10

s1 1

s1 14

, , compare:

int compare(const string& str) const;

int compare(size_type pos1, size_type n1, const string& str) const;

int compare(size_type pos1, size_type n1,const string& str, size_type pos2. size_type n2) const;

, 0, str, - , - . strstr .

str n1 , pos1.

n1 , pos1, str n2 , pos2.

string -.

: .

#include <string.h>

#include <iostream.h>

int main (){

string s1(" "), s2("e"), s3("");

cout << "s1= " << s1 << end;

cout << "s2= " << s2 << end;

cout << "s3= " << s3 << end;

if (s2.compare(s3) > 0) cout << "s2 > s3 " << end;

if (s1.compare(7, 4, s3) < 0) cout << "s1[7-10] < s3 " << end;

if (s1.compare(7, 4, s3, 0, 4) == 0) cout << "s1[7-10] << s3[0-3] " << end;

}

 

:

s1=

s2=

s3=

s2 > s3

s1[7-10] < s3

s1[7-10] == s3[0-3]

( , ):

size_type size() const;

size_type length() const;

size_type max_size() const; //

size_type capacity() const;

bool empty() const;

, lengh -, size . , .

empty , .

: .

#include <string.h>

int main () {

string st(" ");

cout << " \"" << st << "\": " << st.size()

return 0;

}

: .

#include <string>

int main () {

string st(" \n");

cout << " : " << st.size() <<endl;

cout << " : " << st.lenght();

cout << " : " << st.capacity() << " ";

return 0;

}

:

: 17

: 18

: 18

: , .

#include <string.h>

int main () {

string st2; //

if (!st.size()) cout << ""; // ,

if (st.empty()) cout << ""; //

return 0;

}

string .

: .

#include <string.h>

int main () {

string str("fa.disney.com");

int size = str.size();

for (int i = 0; i < size; ++i)

if (str[ i ] == '.') str[ i ] = '_';

return 0;

}

 

replace:

replace(str.begin(), str.end(), '.', '_');

, , , . . begin end .

string , .. , -, .. &s[0] s.

, string at:

string s("Bac");

cout << s.at(1); //

, out_of_range.

 

1.20

 

/ . (, ).

/ C++ , , C++. sync_with_stdio().

/, . , . / , - .

4 / ( ). / . C++ .

/ <stdio.h>.

. / .

:

FILE* fopen(const char* filename, const char* mode);

- -, - -, .

:

"r" - ;

"w" - ( , );

"" - ;

"r+" - ( );

"w+" - ( , );

"+"- .

t ( ) b ( ). .

. (0x13 0x10) ( ). .

fopen FILE, , NULL .

:

FILE * f = fopen("d:\\cpp\\data", "rb+");

f . / .

:

stdin,

stdout,

stderr,

stdaux,

stdprn.

. / , .

/

, . . , . , , .

setvbuf setbuf .

.

/ : , . .

/ , . ( ) , /.

ftel1 fgetpos fseek fsetpos. .

, :

int fclose(FILE*);

. , , .

, , , , .

, , feof (, ) terror (, /).

- ( stdio.h)

void clearerr(FILE *f);

f ( , learerr, fseek, fsetpos rewind).

/

int fclose (FILE *f);

/ f.

int feof(FILE * f);

EOF , 0, , 0.

int ferror (FILE *f);

, ; 0 .

int fflush (FILE * f);

. 0, EOF.

int fgetc(FILE * f);

int f. , EOF.

int fgetpos(FILE *f, fpos_t *pos);

, f, pos. fsetpos. fpos_t.

n

char *fgets(char *s, int n, FILE * f);

n-1 f s, . , . ('\0'). NULL, s.

/

FILE *fopen(const char *fname, const char *mode);

fname , mode. ( , ), NULL.

int fprintf (FILE *f, const char *fmt,...);

f , (...), , fmt. .

int fputc(int ch, FILE *f);

ch f. EOF, .

int fputs(const char *s,FILE *f);

s f. . EOF, .

size_t fread (void *buffer, size_t size, size_t count, FILE *stream);

connt size , buffer, stream. , count, .

/

FILE *freopen(const char *fname, const char *mode, FILE * f);

fopen, f, .

int fscanf (FILE *f, const char *fmt [,par1, par2,...]);

par1, r2 . . , fmt, f. , .

int fseek (FILE *f, long off, int org);

, f, off, org, , stdi.h:

SEEK_CUR ,

SEEK_END ,

SEEK_SET .

int fsetpos(FILE *f, const fpos_t *pos);

, f, *pos, fgetpos.

long int ftell(FILE *f);

, f, .

size_t fwrite (const void *p, size_t size, size_t n, FILE * f);

n size , , f. .

int getc(FILE *f);

int f. , EOF.

void perror(const char *s);

"s: error ". s NULL ( ), . C errno. stderr.

int printf (const char *fmt,...);

, , , , fmt.

int putc(int ch, FILE * f);

ch f. , - EOF. fputc.

int remove(const char *filename);

. , .

int rename(const char *oldname, const char *newname);

. , .

void rewind(FILE *f);

f .

int scanf(onst char *fmt [,par1, par2,...]);

par1, r2, , fmt, . , .

/

void setbuf(FILE *f, char *p);

f /, . BUFSIZ. nul1, .

void setvbuf (FILE *f, char *p, int mode, size_t size);

f /, . mode , size . , . mode , : _IOFBF ( ), _IONBF ( ) _IOLBF ( , .. ).

int sprint(char *buffer, const char *format[, argument,... ]);

buffer , , , , format.

int sscanf(const char *buf, const char *format [,par1, par2,...]);

scanf , , .. buf , , format , . , .

/

FILE *tmpfile(vold);

/ .

char *tmpnam(char *s);

, . _ ( stdio. h 65535). s - 0, , , L_tmpnam (L_tmpnam stdio.h). s 0, . S 0, , s.

int ungetc (int ch, FILE *f);

ch f. ch, EOF.

int vfprintf(FILE *stream, char *format, va_list arglist);

fprintf, , .

int vprintf(const char *format, va_list arglist);

printf, , .

int vsprintf(char *string, const char *format, va_list arglist);

sprintf, , .

, (, Unicode) fgetwc, fgetws, fputwc, fputws, fwprintf, fwscanf, getwc, putwc, swprintf, swscanf, ungetwc, vfwprintf, vswprintf, vwprintf, printf, wscanf.

, wint_t. ( wctype.h).

, fgetwc , fgetc, :

wint_t fgetwc(FILE * f);

wint_t f. , WEOF.

:

. , . : 20 , 5 , , 40 .

s, mon . .

#include <iostream.h>

#inclucle <stdio.h>

#include <stdlib.h>

#include <string.h>

int main(){

FILE *fi, *fo;

if ((fi = fopen("d:\\c\\file.txt", "r")) == 0){

cout << " ";

return 1;

}

if ((fo = fopen("d:\\c\\binfile.out", "w+b")) == 0){

cout << " ";

return 1;

}

const int dl = 80;

char s[dl];

struct {

char type[20];

int opt, rozn;

char comm[40];

} mon;

int kol = 0; //

while (fgets(s, dl,fi)){ //

strncpy(mon.type, s, 19);

mon.type[19]='\0';

mon.opt = atoi(&s[20]);

mon.rozn = atoi(&s[25]);

strncpy(mon.comm, &s[30], 40);

fwrite(&mon, sizeof mon, 1, fo);

kol++;

}

fclose(fi);

int i;

cin >> i; //

if (i >= kol){

cout << " ";

return 1;

}

fseek(fo, (sizeof mon)*i, SEEK_SET); //

// i

fread(&mon, sizeof mon, 1, fo);

cout << "type " << mon.type << " opt " << mon.opt << " rozn " << mon.rozn << endl;

fclose(fo); return 0; }

1.21





:


: 2016-11-12; !; : 1038 |


:

:

, .
==> ...

1361 - | 1219 -


© 2015-2024 lektsii.org - -

: 0.282 .