d05_stl
TRANSCRIPT
-
7/30/2019 D05_STL
1/12
2003 Prentice Hall, Inc. All rights reserved.
1
Th vin chun C++Standard Template Library (STL)
2003 Prentice Hall, Inc. All rights reserved.
2
Tng quan
Th vin chun C++ bao gm 32 header file, trong ta lm quen vi mt s file (t nht n mtmc no )
2003 Prentice Hall, Inc. All rights reserved.
3
Th vin khun mu chun - STL
Th vin chun C++ gm 2 phn: Lp string
Th vin khun mu chun STL
Ngoi tr lp string,tt c cc thnh phn cn li ca th vinu l cc khun mu
Tc giu tin ca STL l Alexander Stepanov, mc ch cang l xy dng mt cch th hin t tng lp trnh tng qut
Cc khi nim trong STL c pht trin c lp vi C++ Do , ban u, STL khng phi l mt th vin C++, m n c
chuyn i thnh th vin C++
Nhiu t tng dn n s pht trin ca STL c ci t phn notrong Scheme, Ada, v C
2003 Prentice Hall, Inc. All rights reserved.
4
Th vin khun mu chun - STL
Mt s li khuyn v STL STL c thit kp v hiu qu - khng c tha k
hay hm o trong bt knh ngha no T t tng lp trnh tng qut dn ti nhng "khi c
bn" (building block) m c th kt hp vi nhau theo kiu
Tuy lm quen vi STL tn khng t thi gian nhngthnh qu tim tng v nng xut rt xng ng vi thigian u t
Tm li hy hc v hy s dng!
Bi ging ny ch gii thiu mt phn rt nh caSTL
-
7/30/2019 D05_STL
2/12
2003 Prentice Hall, Inc. All rights reserved.
5
Gii thiu Standard Template Library (STL)
Ba thnh phn chnh ca STL Cc thnh phn rt mnh xy dng da trn
template
Container: cc cu trc d liu template Iterator: ging con tr, dng truy nhp cc phn
t d liu ca cc container
Algorithm: cc thut ton thao tc d liu, tmkim, sp xp, v.v..
2003 Prentice Hall, Inc. All rights reserved.
6
Gii thiu v cc Container
3 loi container Sequence container container chui
cc cu trc d liu tuyn tnh (vector, danh sch lin kt)
first-class container
vector, deque, list
Associative container container lin kt cc cu trc phi tuyn, c th tm phn t nhanh chng
first-class container
cc cp kha/gi tr set, multiset, map, multimap
Container adapter cc b tng thch container stack, queue, priority_queue
2003 Prentice Hall, Inc. All rights reserved.
7
Cc hm thnh vin STL
Cc hm thnh vin mi container u c Default constructor, copy constructor, destructor
empty
max_size, size
= < >= == !=
swap
Cc hm thnh vin ca first-class containerbegin, end
rbegin, rend
erase, clear
2003 Prentice Hall, Inc. All rights reserved.
8
Gii thiu v Iterator
Iterator tng t nh con tr tr ti cc phn t trong mt container
cc ton t iterator cho mi container * truy nhp phn tc tr ti
++ tr ti phn t tip theo
begin() tr v iterator tr ti phn tu tin
end() tr v iterator tr ti phn tc bit chn cuicontainer
-
7/30/2019 D05_STL
3/12
2003 Prentice Hall, Inc. All rights reserved.
9
Cc loi Iterator
Input (v d: istream_iterator)
c cc phn t t mt container, h tr++,+= (ch tin)
Output (v d: ostream_iterator)
Ghi cc phn t vo container, h tr++,+= (ch tin) Forward (v d: hash_setiterator)
Kt hp input iterator v output iterator
Multi-pass (c th duyt chui nhiu ln)
Bidirectional (V d: listiterator) Nh forward iterator, nhng c th li (--,-=)
Random access (V d: vectoriterator)
Nh bidirectional, nhng cn c th nhy ti phn t ty
2003 Prentice Hall, Inc. All rights reserved.
10
Cc loi Iteratorc h tr
Sequence container
vector: random access
deque: random access
list: bidirectional
Associative container
(h trcc loi bidirectional) set, multiset,map, multimap
Container adapter (khng h triterator) stack, queue, priority_queue
2003 Prentice Hall, Inc. All rights reserved.
11
Cc php ton i vi Iterator
Input iterator++ , =*p , -> , == , !=
Output iterator++ , *p= , p = p1
Forward iterator Kt hp cc ton t ca input v output iterator
Bidirectional iteratorcc ton t cho forward, v--
Random iteratorcc ton t cho bidirectional, v+ , +=, -, -=, >, >=,
-
7/30/2019 D05_STL
4/12
2003 Prentice Hall, Inc. All rights reserved.
13
Sequence Container
3 loi sequence container: vector da theo mng
deque da theo mng
list
danh sch lin kt hiu qu cao
2003 Prentice Hall, Inc. All rights reserved.
14
vector Sequence Container
vector
cu trc d liu vi cc vng nhlin tip truy nhp cc phn t bng ton t []
s dng khi d liu cn c sp xp v truy nhp d dng
Cch hot ng khi ht b nh cp pht mt vng nhlin lc ln hn
t sao chp ra vng nhmi
tr li vng nhc
s dng random access iterator
2003 Prentice Hall, Inc. All rights reserved.
15
vector Sequence Container
Khai bo std::vector v;
type l kiu d liu ca phn t d liu (int, float, v.v..)
Iterator std::vector::iterator iterVar;
trng hp thng thng std::vector::const_iterator iterVar;
const_iterator khng th sa i cc phn t
std::vector::reverse_iterator iterVar;
Visits elements in reverse order (end to beginning)
Use rbegin to get starting point
Use rendto get ending point
2003 Prentice Hall, Inc. All rights reserved.
16
vector Sequence Container
Cc hm thnh vin ca vector v.push_back(value)
thm phn t vo cui (sequence container no cng c hmny).
v.size() kch thc hin ti ca vector
v.capacity() kch thc c th lu tr trc khi phi cp pht li
khi cp pht li s cp pht kch thc gp i
vector v(a, a + SIZE) to vector v t SIZE phn tu tin ca mng a
-
7/30/2019 D05_STL
5/12
2003 Prentice Hall, Inc. All rights reserved.
17
vector Sequence Container
Cc hm thnh vin ca vector v.insert(iterator, value )
chn value vo trc v tr ca iterator
v.insert(iterator, array , array + SIZE) chn vo vector SIZEphn tu tin ca mng array
v.erase( iterator )
xa phn t khi container
v.erase( iter1, iter2 )
xa b cc phn t bt u t iter1 n ht phn t lintrc iter2
v.clear()
Xa ton b container
2003 Prentice Hall, Inc. All rights reserved.
18
21.2.1 vector Sequence Container
Cc hm thnh vin ca vector v.front(), v.back()
Tr v phn tu tin v cui cng
v.[elementNumber] = value; Gn gi tr value cho mt phn t
v.at[elementNumber] = value; Nh trn, nhng km theo kim tra ch s hp l c th nm ngoi l out_of_bounds
2003 Prentice Hall, Inc.
All rights reserved.
Outline19
fig21_14.cpp
(1 of 3)
1 // Fig. 21.14: fig21_14.cpp
2 // Demonstrating standard library vector class template.
3 #include
4
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 #include // vector class-template definition
10
11 // prototype for function template printVector12 template < class T >
13 voidprintVector( const std::vector< T > &integers2 );
14
15 int main()
16 {
17 const int SIZE = 6;
18 int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 };
19
20 std::vector< int > integers;
21
22 cout
-
7/30/2019 D05_STL
6/12
2003 Prentice Hall, Inc.
All rights reserved.
Outline21
fig21_14.cpp
(3 of 3)
46 std::vector< int >::reverse_iterator reverseIterator;
47
48 for ( reverseIterator = integers.rbegin();
49 reverseIterator!= integers.rend();
50 ++reverseIterator )
51 cout ::const_iterator constIterator;
64
65 for ( constIterator = integers2.begin();
66 constIterator != integers2.end();
67 constIterator++ )
68 cout output( cout, " " );
19
20 cout
-
7/30/2019 D05_STL
7/12
2003 Prentice Hall, Inc.
All rights reserved.
Outline25
fig21_15.cpp
(3 of 3)
52 // erase remaining elements
53 integers.erase( integers.begin(), integers.end() );
54 cout
-
7/30/2019 D05_STL
8/12
2003 Prentice Hall, Inc.
All rights reserved.
Outline29
fig21_23.cpp
(1 of 3)
1 // Fig. 21.23: fig21_23.cpp
2 // Standard library adapter stack test program.
3 #include
4
5 using std::cout;
6 using std::endl;
7
8 #include // stack adapter definition
9 #include // vector class-template definition
10 #include // list class-template definition
1112 // popElements function-template prototype
13 template< class T >
14 voidpopElements( T &stackRef );
15
16 intmain()
17 {
18 // stack with default underlying deque
19 std::stack< int > intDequeStack;
20
21 // stack with underlying vector
22 std::stack< int, std::vector< int > > intVectorStack;
23
24 // stack with underlying list
25 std::stack< int, std::list< int > > intListStack;
26
To stack bng nhiu kiu ci t.
2003 Prentice Hall, Inc.
All rights reserved.
Outline30
fig21_23.cpp
(2 of 3)
27 // push the values 0-9 onto each stack
28 for ( int i = 0; i < 10; ++i ) {
29 intDequeStack.push( i );
30 intVectorStack.push( i );
31 intListStack.push( i );
32
33 } // end for
34
35 // display and remove elements from each stack
36 cout
-
7/30/2019 D05_STL
9/12
2003 Prentice Hall, Inc. All rights reserved.
33
remove, remove_if, remove_copy v
remove_copy_if
remove remove( iter1, iter2, value);
B mi phn t c gi tr value trong khong (iter1-iter2) theo cch sau:
Chuyn cc phn t c gi tr value xung cui
khng thay i kch thc ca container hoc thc s xa ccphn t
Tr v iterator ti kt thc mi ca container
cc phn t sau kt thc mi l khng xc nh
remove_copy remove_copy(iter1, iter2, iter3, value);
trong khong iter1-iter2, chp cc phn t khcvalue vo iter3 (output iterator)
2003 Prentice Hall, Inc. All rights reserved.
34
remove, remove_if, remove_copy v
remove_copy_if
remove_if ging remove
tr v iterator ti phn t cui cng
b cc phn t m hm tr v trueremove_if(iter1,iter2, function);
cc phn tc truyn cho function, hm ny tr v gitrbool
remove_copy_if ging remove_copy v remove_if
remove_copy_if(iter1, iter2, iter3, function);
2003 Prentice Hall, Inc. All rights reserved.
35
Cc thut ton ton hc
random_shuffle(iter1, iter2)
xo trn cc phn t trong khong mt cch ngu nhin
count(iter1, iter2, value)
tr v s ln xut hin ca value trong khong
count_if(iter1, iter2, function) m s phn t lm function tr v true
min_element(iter1, iter2)
tr v iterator ti phn t nh nht
max_element(iter1, iter2)
tr v iterator ti phn t ln nht
2003 Prentice Hall, Inc. All rights reserved.
36
Cc thut ton ton hc
accumulate(iter1, iter2)
tr v tng cc phn t trong khong
for_each(iter1, iter2, function)
Gi hm function cho mi phn t trong khong khng sa i phn t
transform(iter1, iter2, iter3, function)
gi function cho mi phn t trong khong iter1-iter2,kt qu ghi vo iter3
-
7/30/2019 D05_STL
10/12
2003 Prentice Hall, Inc. All rights reserved.
37
Cc thut ton tm kim v sp xp c bn
find(iter1, iter2, value)
tr v iterator ti ln xut hin u tin (trong khong) cavalue
find_if(iter1, iter2, function)
nh find tr v iterator khi function tr v true
sort(iter1, iter2)
sp xp cc phn t theo th t tng dn
binary_search(iter1, iter2, value)
tmgi tr trong dy sp xp tng dn,
s dng thut ton tm kim nh phn
2003 Prentice Hall, Inc.
All rights reserved.
Outline38
fig21_31.cpp
(1 of 4)
1 // Fig. 21.31: fig21_31.cpp
2 // Standard library search and sort algorithms.
3 #include
4
5 using std::cout;
6 using std::endl;
7
8 #include // algorithm definitions
9 #include // vector class-template definition
1011 bool greater10( int value ); // prototype
12
13 intmain()
14 {
15 const int SIZE = 10;
16 int a[ SIZE ] = { 10, 2, 17, 5, 16, 8, 13, 11, 20, 7 };
17
18 std::vector< int > v( a, a + SIZE );
19 std::ostream_iterator< int > output( cout, " " );
20
21 cout ::iterator location;
26 location = std::find( v.begin(), v.end(), 16 );
2003 Prentice Hall, Inc.
All rights reserved.
Outline39
fig21_31.cpp
(2 of 4)
27
28 if ( location != v.end() )
29 cout
-
7/30/2019 D05_STL
11/12
2003 Prentice Hall, Inc.
All rights reserved.
Outline41
fig21_31.cpp
(4 of 4)
fig21_31.cpp
output (1 of 1)
77 // determine whether argument is greater than 10
78 bool greater10( int value )
79 {
80 return value >10;
81
82 } // end function greater10
Vector v contains: 10 2 17 5 16 8 13 11 20 7
Found 16 at location 4
100 not found
The first value greater than 10 is 17
found at location 2
Vector v after sort: 2 5 7 8 10 11 13 16 17 20
13 was found in v
100 was not found in v
2003 Prentice Hall, Inc. All rights reserved.
42
Function Object functor
Function object() Cc i tng c th gi
nh hm bng ton t ()
STL f unc t io n o b je c ts Typ e
divides< T > arithmetic
equal_to< T > relational
greater< T > relational
greater_equal< T > relationalless< T > relational
less_equal< T > relational
logical_and< T > logical
logical_not< T > logical
logical_or< T > logical
minus< T > arithmetic
modulus< T > arithmetic
negate< T > arithmetic
not_equal_to< T > relational
plus< T > arithmetic
multiplies< T > arithmetic
2003 Prentice Hall, Inc.
All rights reserved.
Outline43
fig21_42.cpp
(1 of 4)
1 // Fig. 21.42: fig21_42.cpp
2 // Demonstrating function objects.
3 #include
4
5 using std::cout;
6 using std::endl;
7
8 #include // vector class-template definition
9 #include // copy algorithm
10 #include // accumulate algorithm
11 #include // binary_function definition12
13 // binary function adds square of its second argument and
14 // running total in its first argument, then returns sum
15 int sumSquares( int total, int value )
16 {
17 return total + value * value;
18
19 } // end function sumSquares
20
To mt hm dng viaccumulate.
2003 Prentice Hall, Inc.
All rights reserved.
Outline44
fig21_42.cpp
(2 of 4)
21 // binary function class template defines overloaded operator()
22 // that adds suare of its second argument and running total in
23 // its first argument, then returns sum
24 template< class T >
25 class SumSquaresClass :public std::binary_function< T, T, T > {
26
27 public:
28
29 // add square of value to total and return result
30 const T operator()( const T &total, const T &value )31 {
32 return total + value * value;
33
34 } // end function operator()
35
36 }; // end class SumSquaresClass
37
To mt function object (ncn c th ng gi d liu).Overload operator().
-
7/30/2019 D05_STL
12/12
2003 Prentice Hall, Inc.
All rights reserved.
Outline45
fig21_42.cpp
(3 of 4)
38 intmain()
39 {
40 const int SIZE = 10;
41 int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
42
43 std::vector< int > integers( array, array + SIZE );
44
45 std::ostream_iterator< int > output( cout, " " );
46
47 int result = 0;
4849 cout