0% found this document useful (0 votes)
549 views8 pages

C++ Quick Reference

The document provides a quick reference for C++ syntax including preprocessor directives, literals, declarations, storage classes, statements, functions, and expressions. It defines common elements such as #include, data types, if/else statements, function definitions, and operators. Examples are given for declaring variables, defining constants, writing expressions, and memory management using new and delete.

Uploaded by

Balan Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
549 views8 pages

C++ Quick Reference

The document provides a quick reference for C++ syntax including preprocessor directives, literals, declarations, storage classes, statements, functions, and expressions. It defines common elements such as #include, data types, if/else statements, function definitions, and operators. Examples are given for declaring variables, defining constants, writing expressions, and memory management using new and delete.

Uploaded by

Balan Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

4/25/2014

C++ QUICK REFERENCE

C++ QUICK REFERENCE


Matt Mahoney, [email protected]

PREPROCESSOR
/ /C o m m e n tt oe n do fl i n e / *M u l t i l i n ec o m m e n t* / / /I n s e r ts t a n d a r dh e a d e rf i l e / /I n s e r tf i l ei nc u r r e n td i r e c t o r y / /R e p l a c eXw i t hs o m et e x t / /R e p l a c eF ( 1 , 2 )w i t h1 + 2 / /L i n ec o n t i n u a t i o n / /R e m o v ed e f i n i t i o n / /C o n d i o n a lc o m p i l a t i o n( # i f d e fX ) / /O p t i o n a l( # i f n d e fXo r# i f! d e f i n e d ( X ) ) / /R e q u i r e da f t e r# i f ,# i f d e f

# i n c l u d e< s t d i o . h > # i n c l u d e" m y f i l e . h " # d e f i n eXs o m et e x t # d e f i n eF ( a , b )a + b # d e f i n eX\ s o m et e x t # u n d e fX # i fd e f i n e d ( X ) # e l s e # e n d i f

LITERALS

2 5 5 ,0 3 7 7 ,0 x f f 2 1 4 7 4 8 3 6 4 7 L ,0 x 7 f f f f f f f l 1 2 3 . 0 ,1 . 2 3 e 2 ' a ' ,' \ 1 4 1 ' ,' \ x 6 1 ' ' \ n ' ,' \ \ ' ,' \ ' ' ,' \ " ' " s t r i n g \ n " " h e l l o "" w o r l d " t r u e ,f a l s e

/ /I n t e g e r s( d e c i m a l ,o c t a l ,h e x ) / /L o n g( 3 2 b i t )i n t e g e r s / /d o u b l e( r e a l )n u m b e r s / /C h a r a c t e r( l i t e r a l ,o c t a l ,h e x ) / /N e w l i n e ,b a c k s l a s h ,s i n g l eq u o t e ,d o u b l eq u o t e / /A r r a yo fc h a r a c t e r se n d i n gw i t hn e w l i n ea n d\ 0 / /C o n c a t e n a t e ds t r i n g s / /b o o lc o n s t a n t s1a n d0

DECLARATIONS
i n tx ; / /D e c l a r ext ob ea ni n t e g e r( v a l u eu n d e f i n e d ) i n tx = 2 5 5 ; / /D e c l a r ea n di n i t i a l i z ext o2 5 5 s h o r ts ;l o n gl ; / /U s u a l l y1 6o r3 2b i ti n t e g e r( i n tm a yb ee i t h e r ) c h a rc = ' a ' ; / /U s u a l l y8b i tc h a r a c t e r u n s i g n e dc h a ru = 2 5 5 ;s i g n e dc h a rs = 1 ; / /c h a rm i g h tb ee i t h e r u n s i g n e dl o n gx = 0 x f f f f f f f f L ; / /s h o r t ,i n t ,l o n ga r es i g n e d f l o a tf ;d o u b l ed ; / /S i n g l eo rd o u b l ep r e c i s i o nr e a l( n e v e ru n s i g n e d ) b o o lb = t r u e ; / /t r u eo rf a l s e ,m a ya l s ou s ei n t( 1o r0 ) i n ta ,b ,c ; / /M u l t i p l ed e c l a r a t i o n s i n ta [ 1 0 ] ; / /A r r a yo f1 0i n t s( a [ 0 ]t h r o u g ha [ 9 ] ) i n ta [ ] = { 0 , 1 , 2 } ; / /I n i t i a l i z e da r r a y( o ra [ 3 ] = { 0 , 1 , 2 } ;) i n ta [ 2 ] [ 3 ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; / /A r r a yo fa r r a yo fi n t s c h a rs [ ] = " h e l l o " ; / /S t r i n g( 6e l e m e n t si n c l u d i n g' \ 0 ' ) i n t *p ; / /pi sap o i n t e rt o( a d d r e s so f )i n t c h a r *s = " h e l l o " ; / /sp o i n t st ou n n a m e da r r a yc o n t a i n i n g" h e l l o " v o i d *p = N U L L ; / /A d d r e s so fu n t y p e dm e m o r y( N U L Li s0 ) i n t &r = x ; / /ri sar e f e r e n c et o( a l i a so f )i n tx e n u mw e e k e n d{ S A T , S U N } ; / /w e e k e n di sat y p ew i t hv a l u e sS A Ta n dS U N e n u mw e e k e n dd a y ; / /d a yi sav a r i a b l eo ft y p ew e e k e n d e n u mw e e k e n d{ S A T = 0 , S U N = 1 } ; / /E x p l i c i tr e p r e s e n t a t i o na si n t e n u m{ S A T , S U N }d a y ; / /A n o n y m o u se n u m t y p e d e fS t r i n gc h a r * ; / /S t r i n gs ;m e a n sc h a r *s ;
http://www.sourcepole.com/sources/programming/cpp/cppqref.html 1/8

4/25/2014

C++ QUICK REFERENCE

c o n s ti n tc = 3 ; c o n s ti n t *p = a ; i n t *c o n s tp = a ; c o n s ti n t *c o n s tp = a ; c o n s ti n t &c r = x ;

/ /C o n s t a n t sm u s tb ei n i t i a l i z e d ,c a n n o ta s s i g nt o / /C o n t e n t so fp( e l e m e n t so fa )a r ec o n s t a n t / /p( b u tn o tc o n t e n t s )a r ec o n s t a n t / /B o t hpa n di t sc o n t e n t sa r ec o n s t a n t / /c rc a n n o tb ea s s i g n e dt oc h a n g ex

STORAGE CLASSES
i n tx ; s t a t i ci n tx ; e x t e r ni n tx ; / /A u t o( m e m o r ye x i s t so n l yw h i l ei ns c o p e ) / /G l o b a ll i f e t i m ee v e ni fl o c a ls c o p e / /I n f o r m a t i o no n l y ,d e c l a r e de l s e w h e r e

STATEMENTS
x = y ; i n tx ; ; { i n tx ; a ; } i f( x )a ; e l s ei f( y )b ; e l s ec ; w h i l e( x )a ; f o r( x ;y ;z )a ; d oa ;w h i l e( x ) ; s w i t c h( x ){ c a s eX 1 :a ; c a s eX 2 :b ; d e f a u l t :c ; } b r e a k ; c o n t i n u e ; r e t u r nx ; t r y{a ;} c a t c h( Tt ){b ;} c a t c h( . . . ){c ;} / /I fxi st r u e( n o t0 ) ,e v a l u a t ea / /I fn o txa n dy( o p t i o n a l ,m a yb er e p e a t e d ) / /I fn o txa n dn o ty( o p t i o n a l ) / /R e p e a t0o rm o r et i m e sw h i l exi st r u e / /E q u i v a l e n tt o :x ;w h i l e ( y ){ a ;z ; } / /E q u i v a l e n tt o :a ;w h i l e ( x )a ; / /xm u s tb ei n t / /I fx= =X 1( m u s tb eac o n s t ) ,j u m ph e r e / /E l s ei fx= =X 2 ,j u m ph e r e / /E l s ej u m ph e r e( o p t i o n a l ) / /E v e r ye x p r e s s i o ni sas t a t e m e n t / /D e c l a r a t i o n sa r es t a t e m e n t s / /E m p t ys t a t e m e n t / /Ab l o c ki sas i n g l es t a t e m e n t / /S c o p eo fxi sf r o md e c l a r a t i o nt oe n do fb l o c k / /I nC ,d e c l a r a t i o n sm u s tp r e c e d es t a t e m e n t s

/ /J u m po u to fw h i l e ,d o ,o rf o rl o o p ,o rs w i t c h / /J u m pt ob o t t o mo fw h i l e ,d o ,o rf o rl o o p / /R e t u r nxf r o mf u n c t i o nt oc a l l e r

/ /I fat h r o w saT ,t h e nj u m ph e r e / /I fat h r o w ss o m e t h i n ge l s e ,j u m ph e r e

FUNCTIONS
i n tf ( i n tx ,i n t ) ; v o i df ( ) ; v o i df ( i n ta = 0 ) ; f ( ) ; i n l i n ef ( ) ; f ( ){s t a t e m e n t s ;} To p e r a t o r + ( Tx ,Ty ) ; To p e r a t o r ( Tx ) ; To p e r a t o r + + ( i n t ) ; / /fi saf u n c t i o nt a k i n g2i n t sa n dr e t u r n i n gi n t / /fi sap r o c e d u r et a k i n gn oa r g u m e n t s / /f ( )i se q u i v a l e n tt of ( 0 ) / /D e f a u l tr e t u r nt y p ei si n t / /O p t i m i z ef o rs p e e d / /F u n c t i o nd e f i n i t i o n( m u s tb eg l o b a l ) / /a + b( i ft y p eT )c a l l so p e r a t o r + ( a ,b ) / /ac a l l sf u n c t i o no p e r a t o r ( a ) / /p o s t f i x+ +o r-( p a r a m e t e ri g n o r e d )
2/8

http://www.sourcepole.com/sources/programming/cpp/cppqref.html

4/25/2014

C++ QUICK REFERENCE

e x t e r n" C "{ v o i df ( ) ; }

/ /f ( )w a sc o m p i l e di nC

Function parameters and return values may be of any type. A function must either be declared or defined before it is used. It may be declared first and defined later. Every program consists of a set of a set of global variable declarations and a set of function definitions (possibly in separate files), one of which must be:
i n tm a i n ( ) {s t a t e m e n t s . . .} o r i n tm a i n ( i n ta r g c ,c h a r *a r g v [ ] ){s t a t e m e n t s . . .}

argv is an array of argc strings from the command line. By convention, main returns status 0 if successful, 1 or higher for errors. Functions with different parameters may have the same name (overloading). Operators except :: . .* ?: may be overloaded. Precedence order is not affected. New operators may not be created.

EXPRESSIONS
Operators are grouped by precedence, highest first. Unary operators and assignment evaluate right to left. All others are left to right. Precedence does not affect order of evaluation, which is undefined. There are no run time checks for arrays out of bounds, invalid pointers, etc.
T : : X N : : X : : X t . x p > x a [ i ] f ( x , y ) T ( x , y ) x + + x t y p e i d ( x ) t y p e i d ( T ) d y n a m i c _ c a s t < T > ( x ) s t a t i c _ c a s t < T > ( x ) r e i n t e r p r e t _ c a s t < T > ( x ) c o n s t _ c a s t < T > ( x ) s i z e o fx s i z e o f ( T ) + + x x ~ x ! x x + x & x * p n e wT n e wT ( x ,y ) n e wT [ x ] d e l e t ep d e l e t e [ ]p ( T )x / /N a m eXd e f i n e di nc l a s sT / /N a m eXd e f i n e di nn a m e s p a c eN / /G l o b a ln a m eX / /M e m b e rxo fs t r u c to rc l a s st / /M e m b e rxo fs t r u c to rc l a s sp o i n t e dt ob yp / /i ' t he l e m e n to fa r r a ya / /C a l lt of u n c t i o nfw i t ha r g u m e n t sxa n dy / /O b j e c to fc l a s sTi n i t i a l i z e dw i t hxa n dy / /A d d1t ox ,e v a l u a t e st oo r i g i n a lx( p o s t f i x ) / /S u b t r a c t1f r o mx ,e v a l u a t e st oo r i g i n a lx / /T y p eo fx / /E q u a l st y p e i d ( x )i fxi saT / /C o n v e r t sxt oaT ,c h e c k e da tr u nt i m e / /C o n v e r t sxt oaT ,n o tc h e c k e d / /I n t e r p r e tb i t so fxa saT / /C o n v e r t sxt os a m et y p eTb u tn o tc o n s t / /N u m b e ro fb y t e su s e dt or e p r e s e n to b j e c tx / /N u m b e ro fb y t e st or e p r e s e n tt y p eT / /A d d1t ox ,e v a l u a t e st on e wv a l u e( p r e f i x ) / /S u b t r a c t1f r o mx ,e v a l u a t e st on e wv a l u e / /B i t w i s ec o m p l e m e n to fx / /t r u ei fxi s0 ,e l s ef a l s e( 1o r0i nC ) / /U n a r ym i n u s / /U n a r yp l u s( d e f a u l t ) / /A d d r e s so fx / /C o n t e n t so fa d d r e s sp( * & xe q u a l sx ) / /A d d r e s so fn e w l ya l l o c a t e dTo b j e c t / /A d d r e s so faTi n i t i a l i z e dw i t hx ,y / /A d d r e s so fa l l o c a t e dn e l e m e n ta r r a yo fT / /D e s t r o ya n df r e eo b j e c ta ta d d r e s sp / /D e s t r o ya n df r e ea r r a yo fo b j e c t sa tp / /C o n v e r txt oT( o b s o l e t e ,u s e. . _ c a s t < T > ( x ) )

http://www.sourcepole.com/sources/programming/cpp/cppqref.html

3/8

4/25/2014

C++ QUICK REFERENCE

x*y x/y x%y x+y x-y x< <y x> >y x<y x< =y x>y x> =y x= =y x! =y x&y x^y x|y x& &y x| |y x=y x+ =y x?y:z t h r o wx x,y

/ /M u l t i p l y / /D i v i d e( i n t e g e r sr o u n dt o w a r d0 ) / /M o d u l o( r e s u l th a ss i g no fx ) / /A d d ,o r& x [ y ] / /S u b t r a c t ,o rn u m b e ro fe l e m e n t sf r o m* xt o* y / /xs h i f t e dyb i t st ol e f t( x*p o w ( 2 ,y ) ) / /xs h i f t e dyb i t st or i g h t( x/p o w ( 2 ,y ) ) / /L e s st h a n / /L e s st h a no re q u a lt o / /G r e a t e rt h a n / /G r e a t e rt h a no re q u a lt o / /E q u a l s / /N o te q u a l s / /B i t w i s ea n d( 3&6i s2 ) / /B i t w i s ee x c l u s i v eo r( 3^6i s5 ) / /B i t w i s eo r( 3|6i s7 ) / /xa n dt h e ny( e v a l u a t e syo n l yi fx( n o t0 ) ) / /xo re l s ey( e v a l u a t e syo n l yi fxi sf a l s e( 0 ) ) / /A s s i g nyt ox ,r e t u r n sn e wv a l u eo fx / /x=x+y ,a l s o=* =/ =< < => > =& =| =^ = / /yi fxi st r u e( n o n z e r o ) ,e l s ez / /T h r o we x c e p t i o n ,a b o r t si fn o tc a u g h t / /e v a l u a t e sxa n dy ,r e t u r n sy( s e l d o mu s e d )

CLASSES
c l a s sT{ / /An e wt y p e p r i v a t e : / /S e c t i o na c c e s s i b l eo n l yt oT ' sm e m b e rf u n c t i o n s p r o t e c t e d : / /A l s oa c c e s s a b l et oc l a s s e sd e r i v e df r o mT p u b l i c : / /A c c e s s a b l et oa l l i n tx ; / /M e m b e rd a t a v o i df ( ) ; / /M e m b e rf u n c t i o n v o i dg ( ){ r e t u r n ; } / /I n l i n em e m b e rf u n c t i o n v o i dh ( )c o n s t ; / /D o e sn o tm o d i f ya n yd a t am e m b e r s i n to p e r a t o r + ( i n ty ) ; / /t + ym e a n st . o p e r a t o r + ( y ) i n to p e r a t o r ( ) ; / /tm e a n st . o p e r a t o r ( ) T ( ) :x ( 1 ){ } / /C o n s t r u c t o rw i t hi n i t i a l i z a t i o nl i s t T ( c o n s tT &t ) :x ( t . x ){ } / /C o p yc o n s t r u c t o r T &o p e r a t o r = ( c o n s tT &t ){ x = t . x ;r e t u r n* t h i s ;} / /A s s i g n m e n to p e r a t o r ~ T ( ) ; / /D e s t r u c t o r( a u t o m a t i cc l e a n u pr o u t i n e ) e x p l i c i tT ( i n ta ) ; / /A l l o wt = T ( 3 )b u tn o tt = 3 o p e r a t o ri n t ( )c o n s t{ r e t u r nx ; } / /A l l o w si n t ( t ) f r i e n dv o i di ( ) ; / /G l o b a lf u n c t i o ni ( )h a sp r i v a t ea c c e s s f r i e n dc l a s sU ; / /M e m b e r so fc l a s sUh a v ep r i v a t ea c c e s s s t a t i ci n ty ; / /D a t as h a r e db ya l lTo b j e c t s s t a t i cv o i dl ( ) ; / /S h a r e dc o d e . M a ya c c e s syb u tn o tx c l a s sZ{ } ; / /N e s t e dc l a s sT : : Z
http://www.sourcepole.com/sources/programming/cpp/cppqref.html 4/8

4/25/2014

C++ QUICK REFERENCE

t y p e d e fi n tV ; / /T : : Vm e a n si n t } ; v o i dT : : f ( ){ / /C o d ef o rm e m b e rf u n c t i o nfo fc l a s sT t h i s > x=x ; } / /t h i si sa d d r e s so fs e l f( m e a n sx = x ; ) i n tT : : y=2 ; / /I n i t i a l i z a t i o no fs t a t i cm e m b e r( r e q u i r e d ) T : : l ( ) ; / /C a l lt os t a t i cm e m b e r s t r u c tT{ / /E q u i v a l e n tt o :c l a s sT{p u b l i c : v i r t u a lv o i df ( ) ; / /M a yb eo v e r r i d d e na tr u nt i m eb yd e r i v e dc l a s s v i r t u a lv o i dg ( ) = 0 ;} ; / /M u s tb eo v e r r i d d e n( p u r ev i r t u a l ) c l a s sU :p u b l i cT{ } ; / /D e r i v e dc l a s sUi n h e r i t sa l lm e m b e r so fb a s eT c l a s sV :p r i v a t eT{ } ; / /I n h e r i t e dm e m b e r so fTb e c o m ep r i v a t e c l a s sW :p u b l i cT ,p u b l i cU{ } ; / /M u l t i p l ei n h e r i t a n c e c l a s sX :p u b l i cv i r t u a lT{ } ;/ /C l a s s e sd e r i v e df r o mXh a v eb a s eTd i r e c t l y

All classes have a default copy constructor, assignment operator, and destructor, which perform the corresponding operations on each data member and each base class as shown above. There is also a default no-argument constructor (required to create arrays) if the class has no constructors. Constructors, assignment, and destructors do not inherit.

TEMPLATES
t e m p l a t e< c l a s sT >Tf ( Tt ) ; / /O v e r l o a dff o ra l lt y p e s t e m p l a t e< c l a s sT >c l a s sX{ / /C l a s sw i t ht y p ep a r a m e t e rT X ( Tt ) ;} ; / /Ac o n s t r u c t o r t e m p l a t e< c l a s sT >X < T > : : X ( Tt ){ } / /D e f i n i t i o no fc o n s t r u c t o r X < i n t >x ( 3 ) ; / /A no b j e c to ft y p e" Xo fi n t " t e m p l a t e< c l a s sT ,c l a s sU = T ,i n tn = 0 > / /T e m p l a t ew i t hd e f a u l tp a r a m e t e r s

NAMESPACES

n a m e s p a c eN{ c l a s sT{ } ; }/ /H i d en a m eT N : : Tt ; / /U s en a m eTi nn a m e s p a c eN u s i n gn a m e s p a c eN ; / /M a k eTv i s i b l ew i t h o u tN : :

C/C++ STANDARD LIBRARY


Only the most commonly used functions are listed. Header files without .h are in namespace std. File names are actually lower case. STDIO.H, CSTDIO (Input/output)
F I L E *f = f o p e n ( " f i l e n a m e " ," r " ) ; / /O p e nf o rr e a d i n g ,N U L L( 0 )i fe r r o r / /M o d em a ya l s ob e" w "( w r i t e )" a "a p p e n d ," a + "u p d a t e ," r b "b i n a r y f c l o s e ( f ) ; / /C l o s ef i l ef f p r i n t f ( f ," x = % d " ,3 ) ; / /P r i n t" x = 3 " O t h e rc o n v e r s i o n s : " % 5 d% u% 8 l d " / /i n tw i d t h5 ,u n s i g n e di n t ,l o n gl e f tj u s t . " % o% x% X% l x " / /o c t a l ,h e x ,H E X ,l o n gh e x " % f% 5 . 1 f " / /f l o a to rd o u b l e :1 2 3 . 0 0 0 0 0 0 ,1 2 3 . 0 " % e% g " / /1 . 2 3 e 2 ,u s ee i t h e rfo rg " % c% s " / /c h a r ,c h a r * " % % " / /% s p r i n t f ( s ," x = % d " ,3 ) ; / /P r i n tt oa r r a yo fc h a rs p r i n t f ( " x = % d ,3 ) ; / /P r i n tt os t d o u t( s c r e e nu n l e s sr e d i r e c t e d )
http://www.sourcepole.com/sources/programming/cpp/cppqref.html 5/8

4/25/2014

C++ QUICK REFERENCE

f p r i n t f ( s t d e r r ,. . . g e t c ( f ) ; u n g e t c ( c ,f ) ; g e t c h a r ( ) ; p u t c ( c ,f ) p u t c h a r ( c ) ; f g e t s ( s ,n ,f ) ; g e t s ( s ) f r e a d ( s ,n ,1 ,f ) ; f w r i t e ( s ,n ,1 ,f ) ; f f l u s h ( f ) ; f s e e k ( f ,n ,S E E K _ S E T ) ; f t e l l ( f ) ; r e w i n d ( f ) ; f e o f ( f ) ; f e r r o r ( f ) ; p e r r o r ( s ) ; c l e a r e r r ( f ) ; r e m o v e ( " f i l e n a m e " ) ; r e n a m e ( " o l d " ," n e w " ) ; f=t m p f i l e ( ) ; t m p n a m ( s ) ;

/ /P r i n tt os t a n d a r de r r o r( n o tr e d i r e c t e d ) / /R e a do n ec h a r( a sa ni n t )o rE O Ff r o mf / /P u tb a c ko n ect of / /g e t c ( s t d i n ) ; / /f p r i n t f ( f ," % c " ,c ) ; / /p u t c ( c ,s t d o u t ) ; / /R e a dl i n ei n t oc h a rs [ n ]f r o mf . N U L Li fE O F / /f g e t s ( s ,I N T _ M A X ,f ) ;n ob o u n d sc h e c k / /R e a dnb y t e sf r o mft os ,r e t u r nn u m b e rr e a d / /W r i t enb y t e so fst of ,r e t u r nn u m b e rw r i t t e n / /F o r c eb u f f e r e dw r i t e st of / /P o s i t i o nb i n a r yf i l efa tn / /P o s i t i o ni nf ,1 Li fe r r o r / /f s e e k ( f ,0 L ,S E E K _ S E T ) ;c l e a r e r r ( f ) ; / /I sfa te n do ff i l e ? / /E r r o ri nf ? / /P r i n tc h a r *sa n de r r o rm e s s a g e / /C l e a re r r o rc o d ef o rf / /D e l e t ef i l e ,r e t u r n0i fO K / /R e n a m ef i l e ,r e t u r n0i fO K / /C r e a t et e m p o r a r yf i l ei nm o d e" w b + " / /P u tau n i q u ef i l en a m ei nc h a rs [ L _ t m p n a m ]

STDLIB.H, CSTDLIB (Misc. functions)


a t o f ( s ) ;a t o l ( s ) ;a t o i ( s ) ; / /C o n v e r tc h a r *st of l o a t ,l o n g ,i n t r a n d ( ) ,s r a n d ( s e e d ) ; / /R a n d o mi n t0t oR A N D _ M A X ,r e s e tr a n d ( ) v o i d *p=m a l l o c ( n ) ; / /A l l o c a t enb y t e s . O b s o l e t e :u s en e w f r e e ( p ) ; / /F r e em e m o r y . O b s o l e t e :u s ed e l e t e e x i t ( n ) ; / /K i l lp r o g r a m ,r e t u r ns t a t u sn s y s t e m ( s ) ; / /E x e c u t eO Sc o m m a n ds( s y s t e md e p e n d e n t ) g e t e n v ( " P A T H " ) ; / /E n v i r o n m e n tv a r i a b l eo r0( s y s t e md e p e n d e n t ) a b s ( n ) ;l a b s ( l n ) ; / /A b s o l u t ev a l u ea si n t ,l o n g

STRING.H, CSTRING (Character array handling functions) Strings are type char[] with a '\0' in the last element used.
s t r c p y ( d s t ,s r c ) ; / /C o p ys t r i n g .N o tb o u n d sc h e c k e d s t r c a t ( d s t ,s r c ) ; / /C o n c a t e n a t et od s t .N o tb o u n d sc h e c k e d s t r c m p ( s 1 ,s 2 ) ; / /C o m p a r e ,< 0i fs 1 < s 2 ,0i fs 1 = = s 2 ,> 0i fs 1 > s 2 s t r n c p y ( d s t ,s r c ,n ) ; / /C o p yu pt onc h a r s ,a l s os t r n c a t ( ) ,s t r n c m p ( ) s t r l e n ( s ) ; / /L e n g t ho fsn o tc o u n t i n g\ 0 s t r c h r ( s , c ) ;s t r r c h r ( s , c ) ; / /A d d r e s so ff i r s t / l a s tc h a rci nso r0 s t r s t r ( s ,s u b ) ; / /A d d r e s so ff i r s ts u b s t r i n gi nso r0 / /m e m . . .f u n c t i o n sa r ef o ra n yp o i n t e rt y p e s( v o i d * ) ,l e n g t hnb y t e s m e m m o v e ( d s t ,s r c ,n ) ; / /C o p ynb y t e sf r o ms r ct od s t m e m c m p ( s 1 ,s 2 ,n ) ; / /C o m p a r enb y t e sa si ns t r c m p m e m c h r ( s ,c ,n ) ; / /F i n df i r s tb y t eci ns ,r e t u r na d d r e s so r0 m e m s e t ( s ,c ,n ) ; / /S e tnb y t e so fst oc

CTYPE.H, CCTYPE (Character types)


i s a l n u m ( c ) ; i s a l p h a ( c ) ;i s d i g i t ( c ) ; i s l o w e r ( c ) ;i s u p p e r ( c ) ; t o l o w e r ( c ) ;t o u p p e r ( c ) ; / /I scal e t t e ro rd i g i t ? / /I scal e t t e r ? D i g i t ? / /I scl o w e rc a s e ? U p p e rc a s e ? / /C o n v e r tct ol o w e r / u p p e rc a s e

MATH.H, CMATH (Floating point math)


http://www.sourcepole.com/sources/programming/cpp/cppqref.html 6/8

4/25/2014

C++ QUICK REFERENCE

s i n ( x ) ;c o s ( x ) ;t a n ( x ) ; / /T r i gf u n c t i o n s ,x( d o u b l e )i si nr a d i a n s a s i n ( x ) ;a c o s ( x ) ;a t a n ( x ) ; / /I n v e r s e s a t a n 2 ( y ,x ) ; / /a t a n ( y / x ) s i n h ( x ) ;c o s h ( x ) ;t a n h ( x ) ; / /H y p e r b o l i c e x p ( x ) ;l o g ( x ) ;l o g 1 0 ( x ) ;/ /et ot h ex ,l o gb a s ee ,l o gb a s e1 0 p o w ( x ,y ) ;s q r t ( x ) ; / /xt ot h ey ,s q u a r er o o t c e i l ( x ) ;f l o o r ( x ) ; / /R o u n du po rd o w n( a sad o u b l e ) f a b s ( x ) ;f m o d ( x ,y ) ; / /A b s o l u t ev a l u e ,xm o dy

TIME.H, CTIME (Clock)


c l o c k ( ) / C L O C K S _ P E R _ S E C ; / /T i m ei ns e c o n d ss i n c ep r o g r a ms t a r t e d t i m e _ tt = t i m e ( 0 ) ; / /A b s o l u t et i m ei ns e c o n d so r1i fu n k n o w n t m *p = g m t i m e ( & t ) ; / /0i fU C Tu n a v a i l a b l e ,e l s ep > t m _ Xw h e r eXi s : s e c ,m i n ,h o u r ,m d a y ,m o n( 0 1 1 ) ,y e a r( 1 9 0 0 ) ,w d a y ,y d a y ,i s d s t a s c t i m e ( p ) ; / /" D a yM o nd dh h : m m : s sy y y y \ n " a s c t i m e ( l o c a l t i m e ( & t ) ) ; / /S a m ef o r m a t ,l o c a lt i m e

ASSERT.H, CASSERT (Debugging aid)


a s s e r t ( e ) ; # d e f i n eN D E B U G / /I fei sf a l s e ,p r i n tm e s s a g ea n da b o r t / /( b e f o r e# i n c l u d e< a s s e r t . h > ) ,t u r no f fa s s e r t

NEW.H, NEW (Out of memory handler)


s e t _ n e w _ h a n d l e r ( h a n d l e r ) ;/ /C h a n g eb e h a v i o rw h e no u to fm e m o r y v o i dh a n d l e r ( v o i d ){ t h r o wb a d _ a l l o c ( ) ; } / /D e f a u l t

IOSTREAM.H, IOSTREAM (Replaces stdio.h)


c i n> >x> >y ; c o u t< <" x = "< <3< <e n d l ; c e r r< <x< <y< <f l u s h ; c=c i n . g e t ( ) ; c i n . g e t ( c ) ; c i n . g e t l i n e ( s ,n ,' \ n ' ) ; i f( c i n ) / /R e a dw o r d sxa n dy( a n yt y p e )f r o ms t d i n / /W r i t el i n et os t d o u t / /W r i t et os t d e r ra n df l u s h / /c=g e t c h a r ( ) ; / /R e a dc h a r / /R e a dl i n ei n t oc h a rs [ n ]t o' \ n '( d e f a u l t ) / /G o o ds t a t e( n o tE O F ) ? / /T or e a d / w r i t ea n yt y p eT : i s t r e a m &o p e r a t o r > > ( i s t r e a m &i ,T &x ){ i> >. . . ;x = . . . ;r e t u r ni ; } o s t r e a m &o p e r a t o r < < ( o s t r e a m &o ,c o n s tT &x ){ r e t u r no< <. . . ; }

FSTREAM.H, FSTREAM (File I/O works like cin, cout as above)


i f s t r e a mf 1 ( " f i l e n a m e " ) ; i f( f 1 ) f 1> >x ; f 1 . g e t ( s ) ; f 1 . g e t l i n e ( s ,n ) ; o f s t r e a mf 2 ( " f i l e n a m e " ) ; i f( f 2 )f 2< <x ; / /O p e nt e x tf i l ef o rr e a d i n g / /T e s ti fo p e na n di n p u ta v a i l a b l e / /R e a do b j e c tf r o mf i l e / /R e a dc h a ro rl i n e / /R e a dl i n ei n t os t r i n gs [ n ] / /O p e nf i l ef o rw r i t i n g / /W r i t et of i l e

IOMANIP.H, IOMANIP (Output formatting)


c o u t< <s e t w ( 6 )< <s e t p r e c i s i o n ( 2 )< <s e t f i l l ( ' 0 ' )< <3 . 1 ;/ /p r i n t" 0 0 3 . 1 0 "

STRING (Variable sized character array)


s t r i n gs 1 ,s 2 = " h e l l o " ; s 1 . s i z e ( ) ,s 2 . s i z e ( ) ; / /C r e a t es t r i n g s / /N u m b e ro fc h a r a c t e r s :0 ,5
7/8

http://www.sourcepole.com/sources/programming/cpp/cppqref.html

4/25/2014

C++ QUICK REFERENCE

s 1+ =s 2+''+" w o r l d " ;/ /C o n c a t e n a t i o n s 1= =" h e l l ow o r l d " / /C o m p a r i s o n ,a l s o< ,> ,! = ,e t c . s 1 [ 0 ] ; / /' h ' s 1 . s u b s t r ( m ,n ) ; / /S u b s t r i n go fs i z ens t a r t i n ga ts 1 [ m ] s 1 . c _ s t r ( ) ; / /C o n v e r tt oc o n s tc h a r * g e t l i n e ( c i n ,s ) ; / /R e a dl i n ee n d i n gi n' \ n '

VECTOR (Variable sized array/stack with built in memory allocation)


v e c t o r < i n t >a ( 1 0 ) ; / /a [ 0 ] . . a [ 9 ]a r ei n t( d e f a u l ts i z ei s0 ) a . s i z e ( ) ; / /N u m b e ro fe l e m e n t s( 1 0 ) a . p u s h _ b a c k ( 3 ) ; / /I n c r e a s es i z et o1 1 ,a [ 1 0 ] = 3 a . b a c k ( ) = 4 ; / /a [ 1 0 ] = 4 ; a . p o p _ b a c k ( ) ; / /D e c r e a s es i z eb y1 a . f r o n t ( ) ; / /a [ 0 ] ; a [ 2 0 ] = 1 ; / /C r a s h :n o tb o u n d sc h e c k e d a . a t ( 2 0 ) = 1 ; / /L i k ea [ 2 0 ]b u tt h r o w so u t _ o f _ r a n g e ( ) f o r( v e c t o r < i n t > : : i t e r a t o rp = a . b e g i n ( ) ;p ! = a . e n d ( ) ;+ + p ) * p = 0 ; / /S e ta l le l e m e n t so fat o0 v e c t o r < i n t >b ( a . b e g i n ( ) ,a . e n d ( ) ) ; / /bi sc o p yo fa v e c t o r < T >c ( n ,x ) ; / /c [ 0 ] . . c [ n 1 ]i n i tt ox Td [ 1 0 ] ;v e c t o r < T >e ( d ,d + 1 0 ) ; / /ei si n i t i a l i z e df r o md

DEQUE (array/stack/queue) deque<T> is like vector<T>, but also supports:


a . p u s h _ f r o n t ( x ) ; a . p o p _ f r o n t ( ) ; / /P u t sxa ta [ 0 ] ,s h i f t se l e m e n t st o w a r db a c k / /R e m o v e sa [ 0 ] ,s h i f t st o w a r df r o n t

UTILITY (Pair)
p a i r < s t r i n g ,i n t >a ( " h e l l o " ,3 ) ; / /A2 e l e m e n ts t r u c t a . f i r s t ; / /" h e l l o " a . s e c o n d ; / /3

MAP (associative array)


m a p < s t r i n g ,i n t >a ; / /M a pf r o ms t r i n gt oi n t a [ " h e l l o " ] = 3 ; / /A d do rr e p l a c ee l e m e n ta [ " h e l l o " ] f o r( m a p < s t r i n g ,i n t > : : i t e r a t o rp = a . b e g i n ( ) ;p ! = a . e n d ( ) ;+ + p ) c o u t< <( * p ) . f i r s t< <( * p ) . s e c o n d ; / /P r i n t sh e l l o ,3 a . s i z e ( ) ; / /1

ALGORITHM (A collection of 60 algorithms on sequences with iterators)


m i n ( x ,y ) ;m a x ( x ,y ) ; / /S m a l l e r / l a r g e ro fx ,y( a n yt y p ed e f i n i n g< ) s w a p ( x ,y ) ; / /E x c h a n g ev a l u e so fv a r i a b l e sxa n dy s o r t ( a ,a + n ) ; / /S o r ta r r a ya [ 0 ] . . a [ n 1 ]b y< s o r t ( a . b e g i n ( ) ,a . e n d ( ) ) ;/ /S o r tv e c t o ro rd e q u e

http://www.sourcepole.com/sources/programming/cpp/cppqref.html

8/8

You might also like