If you can't read please download the document
Upload
builien
View
271
Download
7
Embed Size (px)
Citation preview
i
C++ ([email protected])
C++............................................................................................................ 1 2.1 C++.............................................................................................................. 1 2.2 C++.............................................................................................................. 2 2.3 C++ ...................................................................................................... 4
............................................................................................................... 5 3.1 ................................................................................................. 5 3.2 ..................................................................................................................... 5 3.3 ..................................................................................................................... 6
........................................................................................................................... 8 4.1 ......................................................................................... 8 4.2 ..................................................................................................... 8 4.3 ................................................................................................. 8 4.4 ............................................................................. 9 4.5 ............................................................................................................. 9 4.6 ....................................................................................... 10
......................................................................................................................... 12 5.1 ................................................................................................................... 12 5.2 .................................................................................................................. 12 5.3 ................................................................................................... 12
................................................................................................. 13 6.1 ................................................................................................... 13 6.2 ....................................................................................................... 14 6.3 ....................................................................................................... 14 6.4 ............................................................................................................... 14 6.5 ............................................................................................................... 14 6.6 ................................................................................................... 15 6.7 ............................................................................................... 16
......................................................................................................... 17 7.1 I/O ........................................................................................................................ 17 7.2 C ........................................................................................... 17 7.3 C ...................................................................................................... 18
................................................................................................. 19 8.1 ................................................................................................................... 19 8.2 ................................................................................................................... 19 8.3 ................................................................................................................... 19 8.4 ................................................................................................................... 19 8.5 ................................................................................................................... 20
I ............................................................................... 21 9.1 ........................................................................................................... 21
ii
9.2 ............................................................................................... 22 9.3 ................................................................................................................... 23 9.4 ....................................................................................................... 23 9.5 UML ...................................................................................................... 24
II .............................................................................. 25 10.1 ......................................................................................................... 25 10.2 ................................................................................................................. 25 10.3 ................................................................................................................. 25 10.4 ..................................................................................................... 26 10.5 ............................................................................................. 27 10.6 vector ....................................................................................................... 27 10.7 string .................................................................................................... 27
......................................................................................................... 28 11.1 ............................................................................................. 28 11.2 ............................................................................................. 28 11.3 ............................................................................. 28 11.4 ......................................................................... 29 11.5 ................................................................................................. 29 11.6 ................................................................................................................. 29
......................................................................................................... 30 12.1 ............................................................................................................. 30 12.2 ......................................................................................................... 30 12.3 ............................................................................................. 31 12.4 ..................................................................................... 32 12.5 ......................................................................................................... 32 12.6 ..................................................................................................................... 32 12.7 ......................................................................................................................... 33
..................................................................................... 34 13.1 ............................................................................................................. 34 13.2 ......................................................................................... 34 13.3 ................................................................................................. 35 13.4 ..................................................................... 35 13.5 > .................................................................. 36
..................................................................................................................... 37
1
C++ 2.1 C++ C++
C++ C C 1980 Bjarne Stroustrup 1983 C++1989 C++ 1994 ANSI C++ 1998 ISO C++98 2011 8 C++11
http://isocpp.org/std/status
C++ C++ main main main
C++ ;} // /* */
{ } TAB
C++ Visual C++ GNU C++ Intel C++ IDE ()Visual StudioDev C++Code::Blocks
2
2.2 C++ C++
52 0 9 10 + - * / = ! # % ^ & ( ) [ ] { } _ ~ < > \ ' " : ; . , ?
C++ http://en.cppreference.com/w/cpp/keyword
C++ C++ 32
C++( ){ } C++
// /* */
C++ charbool int, short, long, unsigned int, unsigned short, unsigned long float, double, long double / C++
C++
/ char short int /
C++ C
ASCII ASCII
C++
3
;
C++ l L u U f F l L long double true false
C++ const =;
typedef enum enum {}; 0, 1, 2, ... enum weekday {sun=7,mon=1,tue,wed,thu,fri,sat}
C++ +-*/%++ ()-- () =+=-=*=/=%= &=|=^=>>=
4
C++
= ++, -- ++-- C/C++ +=-=*=/=%=
C++ 1 2 1 2 2
sizeof() sizeof() cmath : #include
abs(x) sqrt(x) exp(x) power(x,y) xy log(x) ln log10(x) ceil, floor, round, trunk sin, cos, tan, asin, acos, atan sinh, cosh, tanh, asinh, acosh, atanh
2.3 C++ C++/cin cout \n \t \\ \ \ >>
5
3.1 >=
6
case n: default: }
case case break case case { }
3.3 whiledo while for while
while() { }
(1) (2) ; (3)
do { } while();
(1) (2) ;
while for
for(; 1; 2) { }
(1) (2) 1 (3) 2
1 2 1 2 2 1 while
7
for for(; ; ) { }
break // continue // goto //
breakcontinue goto if
8
4.1 C++ main
// { }
void return return
, , ... ...
/
4.2
()
&
4.3
inline
9
switch
4.4
4.5 C++
for for
extern ;
::
namespace
10
C++ namespace { }
using std
0
4.6
#include // #include "" //
#if
// #endif
#if
// #else
// #endif
#if 1
// 1
11
#elif 2 // 2
#elif 3 // 3
... ... #else
// #endif
#ifdef
// #define #else
// #endif
#ifndef
// #else
// #endif
/
extern extern ;
extern extern ;
C++ http://www. cppreference.com
C++
12
5.1
[n] // n
n
[k] // k 0 n-1
0
5.2
[m][n] // m x n
[i][j] //
C++
[n1][n2][n3]
5.3
13
6.1
*
& *
(1) 0NULL 0/NULL (2) (3)
void void *
void void void
const *
const const const
* const
const * const
k
k 0
14
6.2
a[0] *(a+1) int * pa=a; *pa
a a&a[0] a+i a[i]a[i]*(a+i) pa a *(pa+i) pa[i] pa++ ++pa a++ a[i] pa[i] *(pa+i) *(a+i)
* [n]
6.3 C++ A AA[0]
(1) A[0] A[0]* A[0]*A[0] A[0][0]
(2) A A+i i A[i] A* A[0]*A A[0]
int * p pp intp=A[0] p=A
pa=&A[0][0] A[i][j] *(pa+n*i+j) // A n
6.4
6.5
15
* { }
(* )()
6.6
free storeheap
px = new ; px = new ;
px 0 NULL
new delete px;
px new
px = new []; px = new [](); // 0
0(C++11 )
px = new [n1][n2]...[nm];
delete[] px;
16
6.7 "\0"
cout > ; // getchar(); // cin.getline(str,N,);
cin.getline N-1 '\n'
cstring cstdlib strlen strlen(str) strcat strcat(dest,src) strcpy strcpy(dest,src) strcmp strcmp(str1,str2) atoi atoi(str) atol long atol(str) atof double atof(str) itoa itoa(int,str,raidx)
C++ cctype
isdigit isdigit('3') isalpha isalpha('a') isalnum isalnum('c') islower islower('b') isupper isupper('B') isspace isspace(' ') isprint isprint('A') isgraph isgraph('a') ispunct ispunct('*') iscntrl iscntrl('\n') tolower tolower('A') toupper toupper('a')
ASCII
17
7.1 I/O iostream
cin cout cerr clog cerr
iomanip endl setw(n) cout.width(n) setfill() cout.fill() left right fixed scientific setprecision(n) fixed scientific showpoint
setw cout.width
7.2 C printf(); // cstdio
% %[flag][][.]
c g d o e s f x/X
\b \t \f \\ \n \" \r %%
18
7.3 C FILE *pf;
pf=fopen();
rtwtatrbwbabrt+wt+at+rb+wb+ab+ rw+tb
(NULL) fclose(pf);
0; 0
fprintf(pf, "");
fprintf printf
fscanf(pf, "");
fwrite(buffer, sizecountpf);
count size pf buffer
fread(buffer, sizecountpf);
pf count size buffer
int A[3][3]={{11,12,13},{21,22,23},{31,32,33}}; FILE *pf; pf=fopen("data1.dat","wb"); fwrite(A,sizeof(int),9,pf); // fwrite(A,sizeof(A),1,pf); fclose(pf); int B[3][3]; pf=fopen("data1.dat","rb"); fread(B,sizeof(int),9,pf); fclose(pf);
19
8.1
8.2 k k+1 k k+1
k+2
k+1
8.3 D. Shell 1959
gap 1
gap n/2
8.4
1 2
2 3
n-1 n-1
20
n-1 n-2 n-1
100 10 90 10 10
8.5
pivot
a i1=1 a
i2 i1 a
i3 i2 a
i4 i3 a
i5
21
I 9.1
class { public: private: protected: };
publicprivateprotected ( private) //
. .()
:: // { ; }
::
22
. . .
inline
9.2
(); // (); //
23
(1) (2)
~() { } //
~
9.3
::(): 1(), 2(), ... { }
1(), 2(), ...
(1) (2)
9.4 C
struct {
24
public: private: protected: };
C++ C C
union Mark { char grade; // bool pass; // int score; // };
9.5 UML UMLUnified Modeling Language
UML
: [ = ] () [ : ] + public- private# protected
25
II 10.1
10.2
static
0
. ::
static::
static static
10.3
26
friend
friend
10.4
const ; const ;
() const;
const & ;
27
10.5
[n] [k].
* -> (*). const *
this this
this
() this
this this
::* // (::*)() //
=&:: .*->* =&:: (.*)()(->*)()
newdelete *=new () // *=new () //
delete delete
10.6 vector
10.7 string
28
11.1 +-*/>
29
::operator(int dummy) { ; }
11.4
operator(const &, const &);
::operator(const & p1, const & p2) { ; }
11.5 /
operator ();
::operator () { };
11.6 []++ -- ()
()
30
12.1
12.2 class : 1, 2, ... { ; };
publicprotectedprivate +
- -
public - -
private - -
protected - -
31
- - + - +
- +
12.3
(): 1(),..., n(), 1(), ..., m()
{ ; };
+ +
- - -
32
- -
-
12.4
+ :: :: // ::() //
12.5
12.6
33
class : virtual { ... ... };
12.7
34
13.1 C++
13.2 C++ I/O
#include
ofstream ifstream fstream
fstream fstrm; // fstream fstrm(fname); // fname fstream fstrm(fname, mode); //
fstream ifstream ofstream ifstream ofstream
ios iostream
istream ostream iostream
iostream
ifstream ofstream fstream
fstream
istrstream ostrstream strstream
strstream
35
13.3
fstrm.open(fname) // fstrm fname fstrm.close() // fstrm fstrm.is_open() //
close
ios::in // ios::out // ios::app // ios::ate // ios::Trunc // ios::binary //
ios | ios::out|ios::binary ios::app ios::out
ifstream in in ofstream out out fstream in / out fstream
13.4
> getline cin cout
getline ostream write istream read write(const char* buf, int n); read(char* buf, int n); // buf ,n
.write(buf,50); // buf 50 .read(buf,30); // 30 ( buf
36
13.5 >
class Rational { public: Rational() { x=0; y=1; } Rational(int x, int y) { this->x=x; this->y=y; } friend ostream & operator(istream&, Rational&); void display() { cout
37
etime(0) + 8 clock()CLOCKS_PER_SEC srand(seed)rand()RAND_MAX emirp Fibonacci 3n+1 C
Shell *
ClockCircleRectangleComplexPointDateEmployee Point, Line
const :: binary scope resolution operator . dot operator
C++2.1 C++2.2 C++2.3 C++
3.13.23.3
4.14.24.34.44.54.6
5.15.2 5.3
6.16.26.36.46.56.66.7
7.1 I/O7.2 C7.3 C
8.18.28.38.48.5
I9.19.29.39.49.5 UML
II10.110.210.310.410.510.6vector10.7string
11.111.211.311.411.511.6
12.112.212.312.412.512.612.7
13.113.213.313.413.5 >