Académique Documents
Professionnel Documents
Culture Documents
2013-01-25 Fri
Outline
Introduction
Auto-Type Inference
Lambda Functions
Threading
Compiling
C++11
int x = 5 ;
auto x = 4 ;
I Very helpful when working with templates and iterators:
int
vector <
> vec ;
i t r = vec . i t e r a t o r ( ) ;
auto
//
instead
of
v e c t o r <i n t > : : i t e r a t o r
itr
Study Case
t e m p l a t e <typename B u i l t T y p e , typename B u i l d e r >
void
const
makeAndProcessObject (
B u i l d e r& b u i l d e r )
{
B u i l t T y p e v a l = b u i l d e r . makeObject ( ) ;
//
do
stuff
with
val
}
two necessary template parameters
MyObjBuilder b u i l d e r ;
ma keA ndP roc ess Ob ject <MyObj>( b u i l d e r ) ;
t e m p l a t e <typename B u i l d e r >
void
const
makeAndProcessObject (
B u i l d e r& b u i l d e r )
{
v a l = b u i l d e r . makeObject ( ) ;
auto
//
do
stuff
with
val
}
MyObjBuilder b u i l d e r ;
makeAndProcessObject ( b u i l d e r ) ;
Lambda Functions
return
compiler
[ ] () {
knows
this
returns
an
integer
1; }
now
int
we ' r e
[ ] ( ) >
return
telling
the
compiler
1; }
what
we
want
// D e f i n e a c l a s s f o r a c i t y
class City {
public :
int
int
int
};
int
population ;
foundation_year ;
surface_area ;
main ( ) {
s t d : : l i s t <C i t y > c i t i e s ;
// F i l l t h e l i s t w i t h d a t a
}
b o o l compare ( C i t y a , C i t y b ) {
a . population < b . population ;
}
...
c i t i e s . s o r t ( compare ) ;
return
c i t i e s . s o r t ( [ ] ( C i t y a , C i t y b)> b o o l
{
a . pupulation < b . population ; } ) ;
return
Multithreading
I C++11 threads are more convenient than directly using
Posix threads.
// D e c l a r i n g t h r e a d s
std : : thread
t;
// A s s i g n a f u n c t i o n
t = std : : thread ( function ,
arguments ) ;
// F i n a l i z i n g
t . join ();
...
}
-pthread
double
double
double
int
int
index of array
of elements
double
operator
}
};
int
int
f ,
l ){
length = l ;
()
() {
i = f i r s t ; i < f i r s t + length ;
d o t p r o d u c t += x [ i ] + y [ i ] ;
for ( int
double
i ++) {
no r e m a i n d e r
subsize );
// Compute
for ( int
i = 0;
i < nthreads ;
i ++) t [ i ] . j o i n ( ) ;
// Accumulate t h e p r o d u c t
double r e s u l t =
for ( int i = 0 ;
0;
i < n t h r e a d s ; i ++)
r e s u l t += dp [ i ] . d o t p r o d u c t ;
Compiling C++11
-std=c++11 <args>
g++