際際滷

際際滷Share a Scribd company logo
Th動 vi畛n chu畉n C++
Standard Template Library (STL)
Th動 vi畛n khu担n m畉u chu畉n - STL
 Th動 vi畛n chu畉n C++ bao g畛m 32 header file
<algorithm>
<bitset>
<complex>
<deque>
<exception>
<fstream>
<functional>
<iomanip>
<ios>
<iosfwd>
<iostream>
<istream>
<iterator>
<limits>
<list>
<locale>
<map>
<memory>
<new>
<numeric>
<ostream>
<queue>
<set>
<sstream>
<stack>
<stdexcept>
<streambuf>
<string>
<typeinfo>
<utility>
<valarray>
<vector>
Th動 vi畛n khu担n m畉u chu畉n - STL
 Th動 vi畛n chu畉n C++ g畛m 2 ph畉n:
 L畛p string
 Th動 vi畛n khu担n m畉u chu畉n  STL
 Ngo畉i tr畛 l畛p string, t畉t c畉 c叩c thnh ph畉n c嘆n
l畉i c畛a th動 vi畛n 畛u l c叩c khu担n m畉u
 T叩c gi畉 畉u ti棚n c畛a STL l Alexander Stepanov,
m畛c 鱈ch c畛a 担ng l x但y d畛ng m畛t c叩ch th畛 hi畛n
t動 t動畛ng l畉p tr狸nh t畛ng qu叩t
Th動 vi畛n khu担n m畉u chu畉n - STL
 C叩c kh叩i ni畛m trong STL 動畛c ph叩t tri畛n 畛c
l畉p v畛i C++
 Do 坦, ban 畉u, STL kh担ng ph畉i l m畛t th動 vi畛n C++,
m n坦 達 動畛c chuy畛n 畛i thnh th動 vi畛n C++
 Nhi畛u t動 t動畛ng d畉n 畉n s畛 ph叩t tri畛n c畛a STL 達
動畛c ci 畉t ph畉n no trong Scheme, Ada, v C
Th動 vi畛n khu担n m畉u chu畉n - STL
 M畛t s畛 l畛i khuy棚n v畛 STL
 STL 動畛c thi畉t k畉 畉p v hi畛u qu畉 - kh担ng c坦 th畛a
k畉 hay hm 畉o trong b畉t k畛 畛nh ngh挑a no
 T畛 t動 t動畛ng l畉p tr狸nh t畛ng qu叩t d畉n t畛i nh畛ng "kh畛i
c董 b畉n" (building block) m c坦 th畛 k畉t h畛p v畛i nhau
theo 畛 ki畛u
 Tuy lm quen v畛i STL t畛n kh担ng 鱈t th畛i gian nh動ng
thnh qu畉 ti畛m tng v畛 nng xu畉t r畉t x畛ng 叩ng v畛i
th畛i gian 畉u t動
 T坦m l畉i  h達y h畛c v h達y s畛 d畛ng!
 Bi gi畉ng ny ch畛 畛 gi畛i thi畛u m畛t ph畉n r畉t nh畛
c畛a STL
Gi畛i thi畛u STL
 Ba thnh ph畉n ch鱈nh c畛a STL
 C叩c thnh ph畉n r畉t m畉nh x但y d畛ng d畛a tr棚n
template
 Container: c叩c c畉u tr炭c d畛 li畛u template
 Iterator: gi畛ng con tr畛, d湛ng 畛 truy nh畉p
c叩c ph畉n t畛 d畛 li畛u c畛a c叩c container
 Algorithm: c叩c thu畉t to叩n 畛 thao t叩c d畛
li畛u, t狸m ki畉m, s畉p x畉p, v.v..
Gi畛i thi畛u v畛 c叩c Container
 3 lo畉i container
 Sequence container  container chu畛i
 c叩c c畉u tr炭c d畛 li畛u tuy畉n t鱈nh (vector, danh s叩ch li棚n
k畉t)
 first-class container
 vector, deque, list
 Associative container  container li棚n k畉t
 c叩c c畉u tr炭c phi tuy畉n, c坦 th畛 t狸m ph畉n t畛 nhanh ch坦ng
 first-class container
 c叩c c畉p kh坦a/gi叩 tr畛
 set, multiset, map, multimap
 Container adapter  c叩c b畛 t動董ng th鱈ch container
 stack, queue, priority_queue
C叩c hm thnh vi棚n STL
 C叩c hm thnh vi棚n m畛i container 畛u c坦
 Default constructor, copy constructor, destructor
 empty
 max_size, size
 = < <= > >= == !=
 swap
 C叩c hm thnh vi棚n c畛a first-class container
 begin, end
 rbegin, rend
 erase, clear
Gi畛i thi畛u v畛 Iterator
 Iterator t動董ng t畛 nh動 con tr畛
 tr畛 t畛i c叩c ph畉n t畛 trong m畛t container
 c叩c to叩n t畛 iterator cho m畛i container
 * truy nh畉p ph畉n t畛 動畛c tr畛 t畛i
 ++ tr畛 t畛i ph畉n t畛 ti畉p theo
 begin() tr畉 v畛 iterator tr畛 t畛i ph畉n t畛 畉u ti棚n
 end() tr畉 v畛 iterator tr畛 t畛i ph畉n t畛 畉c bi畛t ch畉n
cu畛i container
C叩c lo畉i Iterator
 Input (v鱈 d畛: istream_iterator)
 畛c c叩c ph畉n t畛 t畛 m畛t container, h畛 tr畛 ++,+= (ch畛 ti畉n)
 Output (v鱈 d畛: ostream_iterator)
 Ghi c叩c ph畉n t畛 vo container, h畛 tr畛 ++,+= (ch畛 ti畉n)
 Forward (v鱈 d畛: hash_set<T> iterator)
 K畉t h畛p input iterator v output iterator
 Multi-pass (c坦 th畛 duy畛t chu畛i nhi畛u l畉n)
 Bidirectional (V鱈 d畛: list<T> iterator)
 Nh動 forward iterator, nh動ng c坦 th畛 l湛i (--,-=)
 Randomaccess (V鱈 d畛: vector<T> iterator)
 Nh動 bidirectional, nh動ng c嘆n c坦 th畛 nh畉y t畛i ph畉n t畛 t湛y 箪
C叩c lo畉i Iterator 動畛c h畛 tr畛
 Sequence container
 vector: random access
 deque: random access
 list: bidirectional
 Associative container
(h畛 tr畛 c叩c lo畉i bidirectional)
 set, multiset,map, multimap
 Container adapter (kh担ng h畛 tr畛 iterator)
 stack, queue, priority_queue
C叩c ph辿p to叩n 畛i v畛i Iterator
 Input iterator
 ++ , =*p , -> , == , !=
 Output iterator
 ++ , *p= , p = p1
 Forward iterator
 K畉t h畛p c叩c to叩n t畛 c畛a input v output iterator
 Bidirectional iterator
 c叩c to叩n t畛 cho forward, v --
 Random iterator
 c叩c to叩n t畛 cho bidirectional, v
+ , +=, -, -=, >, >=, <, <=,[]
Gi畛i thi畛u c叩c thu畉t to叩n  Algorithm
 STL c坦 c叩c thu畉t to叩n 動畛c s畛 d畛ng t畛ng qu叩t
cho nhi畛u lo畉i container
 thao t叩c gi叩n ti畉p v畛i c叩c ph畉n t畛 qua c叩c iterator
 th動畛ng d湛ng cho c叩c ph畉n t畛 trong m畛t chu畛i
 chu畛i x叩c 畛nh b畛i m畛t c畉p iterator tr畛 t畛i ph畉n t畛
畉u ti棚n v cu畛i c湛ng c畛a chu畛i
 c叩c thu畉t to叩n th動畛ng tr畉 v畛 iterator
 v鱈 d畛: find() tr畉 v畛 iterator tr畛 t畛i ph畉n t畛 c畉n t狸m
ho畉c tr畉 v畛 end() n畉u kh担ng t狸m th畉y
 s畛 d畛ng c叩c thu畉t to叩n 動畛c cung c畉p gi炭p l畉p
tr狸nh vi棚n ti畉t ki畛m th畛i gian v c担ng s畛c
Sequence Container
 3 lo畉i sequence container:
 vector  d畛a theo m畉ng
 deque  d畛a theo m畉ng
 list  danh s叩ch li棚n k畉t hi畛u qu畉 cao
vector Sequence Container
 vector
 <vector>
 c畉u tr炭c d畛 li畛u v畛i c叩c v湛ng nh畛 li棚n ti畉p
 truy nh畉p c叩c ph畉n t畛 b畉ng to叩n t畛 [ ]
 s畛 d畛ng khi d畛 li畛u c畉n 動畛c s畉p x畉p v truy nh畉p
d畛 dng
 C董ch畉 ho畉t 畛ng khi h畉t b畛 nh畛
 c畉p ph叩t m畛t v湛ng nh畛 li棚n l畛c l畛n h董n
 t畛 sao ch辿p ra v湛ng nh畛 m畛i
 tr畉 l畉i v湛ng nh畛 c滴
 s畛 d畛ng randomaccess iterator
vector Sequence Container
 Khai b叩o
 std::vector <type> v;
 type l ki畛u d畛 li畛u c畛a ph畉n t畛 d畛 li畛u (int, float, v.v..)
 Iterator
 std::vector<type>::iterator iterVar;
 tr動畛ng h畛p th担ng th動畛ng
 std::vector<type>::const_iterator iterVar;
 const_iterator kh担ng th畛 s畛a 畛i c叩c ph畉n t畛
 std::vector<type>::reverse_iterator iterVar;
 Visits elements in reverse order (end to beginning)
 Use rbegin to get starting point
 Use rend to get ending point
vector Sequence Container
 C叩c hm thnh vi棚n c畛a vector
 v.push_back(value)
 th棚m ph畉n t畛 vo cu畛i (sequence container no c滴ng
c坦 hm ny).
 v.size()
 k鱈ch th動畛c hi畛n t畉i c畛a vector
 v.capacity()
 k鱈ch th動畛c c坦 th畛 l動u tr畛 tr動畛c khi ph畉i c畉p ph叩t l畉i
 khi c畉p ph叩t l畉i s畉 c畉p ph叩t k鱈ch th動畛c g畉p 担i
 vector<type> v(a, a + SIZE)
 t畉o vector vt畛 SIZE ph畉n t畛 畉u ti棚n c畛a m畉ng a
vector Sequence Container
 C叩c hm thnh vi棚n c畛a vector
 v.insert(iterator, value )
 ch竪n value vo tr動畛c v畛 tr鱈 c畛a iterator
 v.insert(iterator, array , array + SIZE)
 ch竪n vo vector SIZE ph畉n t畛 畉u ti棚n c畛a m畉ng array
 v.erase( iterator )
 x坦a ph畉n t畛 kh畛i container
 v.erase( iter1, iter2 )
 x坦a b畛 c叩c ph畉n t畛 b畉t 畉u t畛 iter1 畉n h畉t ph畉n t畛
li畛n tr動畛c iter2
vector Sequence Container
 C叩c hm thnh vi棚n c畛a vector
 v.clear()
 X坦a ton b畛 container
 v.front(), v.back()
 Tr畉 v畛 ph畉n t畛 畉u ti棚n v cu畛i c湛ng
 v.[elementNumber] = value;
 G叩n gi叩 tr畛 value cho m畛t ph畉n t畛
 v.at[elementNumber] = value;
 Nh動 tr棚n, nh動ng k竪m theo ki畛m tra ch畛 s畛 h畛p l畛
 c坦 th畛 n辿m ngo畉i l畛 out_of_bounds
vector Sequence Container
 ostream_iterator
 std::ostream_iterator< type >
Name( outputStream, separator );
type: outputs values of a certain type
outputStream: iterator output location
separator: character separating outputs
 Example
 std::ostream_iterator< int > output( cout, " " );
 std::copy( iterator1, iterator2, output );
 Copies elements from iterator1 up to (not
including) iterator2 to output, an
ostream_iterator
20
1 // Fig. 21.14: fig21_14.cpp
2 // Demonstrating standard library vector class template.
3 #include <iostream>
4
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 #include <vector> // vector class-template definition
10
11 // prototype for function template printVector
12 template < class T >
13 void printVector( 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 << "The initial size of integers is: "
23 << integers.size()
24 << "nThe initial capacity of integers is: "
25 << integers.capacity();
26
21
T畉o m畛t vector ch畛a
c叩c gi叩 tr畛 int
G畛i c叩c hm thnh vi棚n.
27 // function push_back is in every sequence collection
28 integers.push_back( 2 );
29 integers.push_back( 3 );
30 integers.push_back( 4 );
31
32 cout << "nThe size of integers is: " << integers.size()
33 << "nThe capacity of integers is: "
34 << integers.capacity();
35
36 cout << "nnOutput array using pointer notation: ";
37
38 for ( int *ptr = array; ptr != array + SIZE; ++ptr )
39 cout << *ptr << ' ';
40
41 cout << "nOutput vector using iterator notation: ";
42 printVector( integers );
43
44 cout << "nReversed contents of vector integers: ";
45
22
s畛 d畛ng push_back 畛
th棚m ph畉n t畛 vo cu畛i
vector
46 std::vector< int >::reverse_iterator reverseIterator;
47
48 for ( reverseIterator = integers.rbegin();
49 reverseIterator!= integers.rend();
50 ++reverseIterator )
51 cout << *reverseIterator << ' ';
52
53 cout << endl;
54
55 return 0;
56
57 } // end main
58
59 // function template for outputting vector elements
60 template < class T >
61 void printVector( const std::vector< T > &integers2 )
62 {
63 std::vector< T >::const_iterator constIterator;
64
65 for ( constIterator = integers2.begin();
66 constIterator != integers2.end();
67 constIterator++ )
68 cout << *constIterator << ' ';
69
70 } // end function printVector
23
fig21_14.cpp
(3 of 3)
Duy畛t ng動畛c vector b畉ng
m畛t reverse_iterator.
Template function 畛
duy畛t vector theo
chi畛u ti畉n.
The initial size of v is: 0
The initial capacity of v is: 0
The size of v is: 3
The capacity of v is: 4
Contents of array a using pointer notation: 1 2 3 4 5 6
Contents of vector v using iterator notation: 2 3 4
Reversed contents of vector v: 4 3 2
24
fig21_14.cpp
output (1 of 1)
Container Adapter
 Container adapter
 stack, queue v priority_queue
 Kh担ng ph畉i first class container, cho n棚n
 Kh担ng h畛 tr畛 iterator
 Kh担ng cung c畉p c畉u tr炭c d畛 li畛u
 L畉p tr狸nh vi棚n c坦 th畛 ch畛n c叩ch ci 畉t (s畛 d畛ng c畉u
tr炭c d畛 li畛u no)
 畛u cung c畉p c叩c hm thnh vi棚n push v pop b棚n
c畉nh c叩c hm thnh vi棚n kh叩c.
stack Adapter
 stack
 Header <stack>
 ch竪n v x坦a t畉i m畛t 畉u
 C畉u tr炭c Last-in, first-out (LIFO)
 C坦 th畛 ch畛n ci 畉t b畉ng vector, list, ho畉c deque (m畉c
畛nh)
 Khai b叩o
stack<type, vector<type> > myStack;
stack<type, list<type> > myOtherStack;
stack<type> anotherStack; // default deque
 ch畛n ci 畉t l vector, list hay deque kh担ng lm thay 畛i
hnh vi, ch畛 畉nh h動畛ng t畛i hi畛u qu畉 (ci b畉ng deque v
vector l nhanh nh畉t)
1 // Fig. 21.23: fig21_23.cpp
2 // Standard library adapter stack test program.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <stack> // stack adapter definition
9 #include <vector> // vector class-template definition
10 #include <list> // list class-template definition
11
12 // popElements function-template prototype
13 template< class T >
14 void popElements( T &stackRef );
15
16 int main()
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
27
fig21_23.cpp
(1 of 3)
T畉o stack b畉ng nhi畛u
ki畛u ci 畉t.
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 << "Popping from intDequeStack: ";
37 popElements( intDequeStack );
38 cout << "nPopping from intVectorStack: ";
39 popElements( intVectorStack );
40 cout << "nPopping from intListStack: ";
41 popElements( intListStack );
42
43 cout << endl;
44
45 return 0;
46
47 } // end main
48
28
fig21_23.cpp
(2 of 3)
s畛 d畛ng hm thnh vi棚n push.
49 // pop elements from stack object to which stackRef refers
50 template< class T >
51 void popElements( T &stackRef )
52 {
53 while ( !stackRef.empty() ) {
54 cout << stackRef.top() << ' '; // view top element
55 stackRef.pop(); // remove top element
56
57 } // end while
58
59 } // end function popElements
29
fig21_23.cpp
(3 of 3)
fig21_23.cpp
output (1 of 1)
Popping from intDequeStack: 9 8 7 6 5 4 3 2 1 0
Popping from intVectorStack: 9 8 7 6 5 4 3 2 1 0
Popping from intListStack: 9 8 7 6 5 4 3 2 1 0
C叩c thu畉t to叩n
 Tr動畛c STL
 c叩c th動 vi畛n c畛a c叩c h達ng kh叩c nhau kh担ng
t動董ng th鱈ch
 C叩c thu畉t to叩n 動畛c x但y d畛ng v g畉n vo trong
c叩c l畛p container
 STL t叩ch r畛i c叩c container v c叩c thu畉t to叩n
 l畛i th畉:
 d畛 b畛 sung c叩c thu畉t to叩n m畛i
 hi畛u qu畉 h董n, tr叩nh c叩c l畛i g畛i hm 畉o
 header <algorithm>
remove, remove_if, remove_copy v
remove_copy_if
 remove
 remove( iter1, iter2, value);
 B畛 m畛i ph畉n t畛 c坦 gi叩 tr畛 value trong kho畉ng
(iter1 - iter2) theo c叩ch sau:
 Chuy畛n c叩c ph畉n t畛 c坦 gi叩 tr畛 value xu畛ng cu畛i
 kh担ng thay 畛i k鱈ch th動畛c c畛a container ho畉c th畛c
s畛 x坦a c叩c ph畉n t畛
 Tr畉 v畛 iterator t畛i k畉t th炭c m畛i c畛a container
 c叩c ph畉n t畛 sau k畉t th炭c m畛i l kh担ng x叩c 畛nh
remove, remove_if, remove_copy v
remove_copy_if
 remove_copy
 remove_copy(iter1, iter2, iter3, value);
 trong kho畉ng iter1-iter2, ch辿p c叩c ph畉n t畛 kh叩c
value vo iter3 (output iterator)
 remove_if
 gi畛ng remove
 tr畉 v畛 iterator t畛i ph畉n t畛 cu畛i c湛ng
 b畛 c叩c ph畉n t畛 m hm tr畉 v畛 true
remove_if(iter1,iter2, function);
 c叩c ph畉n t畛 動畛c truy畛n cho function, hm ny
tr畉 v畛 gi叩 tr畛 bool
remove, remove_if, remove_copy v
remove_copy_if
 remove_copy_if
 gi畛ng remove_copy v remove_if
remove_copy_if(iter1, iter2, iter3, function);
C叩c thu畉t to叩n to叩n h畛c
 random_shuffle(iter1, iter2)
 x叩o tr畛n c叩c ph畉n t畛 trong kho畉ng m畛t c叩ch ng畉u nhi棚n
 count(iter1, iter2, value)
 tr畉 v畛 s畛 l畉n xu畉t hi畛n c畛a value trong kho畉ng
 count_if(iter1, iter2, function)
 畉m s畛 ph畉n t畛 lm function tr畉 v畛 true
 min_element(iter1, iter2)
 tr畉 v畛 iterator t畛i ph畉n t畛 nh畛 nh畉t
 max_element(iter1, iter2)
 tr畉 v畛 iterator t畛i ph畉n t畛 l畛n nh畉t
C叩c thu畉t to叩n to叩n h畛c
 accumulate(iter1, iter2)
 tr畉 v畛 t畛ng c叩c ph畉n t畛 trong kho畉ng
 for_each(iter1, iter2, function)
 G畛i hm function cho m畛i ph畉n t畛 trong kho畉ng
 kh担ng s畛a 畛i ph畉n t畛
 transform(iter1, iter2, iter3, function)
 g畛i function cho m畛i ph畉n t畛 trong kho畉ng iter1-
iter2, k畉t qu畉 ghi vo iter3
C叩c thu畉t to叩n t狸m ki畉m
v s畉p x畉p c董 b畉n
 find(iter1, iter2, value)
 tr畉 v畛 iterator t畛i l畉n xu畉t hi畛n 畉u ti棚n (trong
kho畉ng) c畛a value
 find_if(iter1, iter2, function)
 nh動 find
 tr畉 v畛 iterator khi function tr畉 v畛 true
 sort(iter1, iter2)
 s畉p x畉p c叩c ph畉n t畛 theo th畛 t畛 tng d畉n
 binary_search(iter1, iter2, value)
1 // Fig. 21.31: fig21_31.cpp
2 // Standard library search and sort algorithms.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <algorithm> // algorithm definitions
9 #include <vector> // vector class-template definition
10
11 bool greater10( int value ); // prototype
12
13 int main()
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 << "Vector v contains: ";
22 std::copy( v.begin(), v.end(), output );
23
24 // locate first occurrence of 16 in v
25 std::vector< int >::iterator location;
26 location = std::find( v.begin(), v.end(), 16 );
37
fig21_31.cpp
(1 of 4)
27
28 if ( location != v.end() )
29 cout << "nnFound 16 at location "
30 << ( location - v.begin() );
31 else
32 cout << "nn16 not found";
33
34 // locate first occurrence of 100 in v
35 location = std::find( v.begin(), v.end(), 100 );
36
37 if ( location != v.end() )
38 cout << "nFound 100 at location "
39 << ( location - v.begin() );
40 else
41 cout << "n100 not found";
42
43 // locate first occurrence of value greater than 10 in v
44 location = std::find_if( v.begin(), v.end(), greater10 );
45
46 if ( location != v.end() )
47 cout << "nnThe first value greater than 10 is "
48 << *location << "nfound at location "
49 << ( location - v.begin() );
50 else
51 cout << "nnNo values greater than 10 were found";
52
38
fig21_31.cpp
(2 of 4)
53 // sort elements of v
54 std::sort( v.begin(), v.end() );
55
56 cout << "nnVector v after sort: ";
57 std::copy( v.begin(), v.end(), output );
58
59 // use binary_search to locate 13 in v
60 if ( std::binary_search( v.begin(), v.end(), 13 ) )
61 cout << "nn13 was found in v";
62 else
63 cout << "nn13 was not found in v";
64
65 // use binary_search to locate 100 in v
66 if ( std::binary_search( v.begin(), v.end(), 100 ) )
67 cout << "n100 was found in v";
68 else
69 cout << "n100 was not found in v";
70
71 cout << endl;
72
73 return 0;
74
75 } // end main
76
39
fig21_31.cpp
(3 of 4)
77 // determine whether argument is greater than 10
78 bool greater10( int value )
79 {
80 return value > 10;
81
82 } // end function greater10
40
fig21_31.cpp
(4 of 4)
fig21_31.cpp
output (1 of 1)
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
Function Object  functor
 (<functional>)
 C叩c畛it動畛ngc坦th畛g畛inh動hmb畉ngto叩nt畛()

STL function objects Type
divides< T > arithmetic
equal_to< T > relational
greater< T > relational
greater_equal< T > relational
less< 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
1 // Fig. 21.42: fig21_42.cpp
2 // Demonstrating function objects.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 #include <vector> // vector class-template definition
9 #include <algorithm> // copy algorithm
10 #include <numeric> // accumulate algorithm
11 #include <functional> // binary_function definition
12
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
42
fig21_42.cpp
(1 of 4)
T畉o m畛t hm 畛 d湛ng
v畛i accumulate.
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
43
fig21_42.cpp
(2 of 4)T畉o m畛t function object
(n坦 c嘆n c坦 th畛 坦ng g坦i
d畛 li畛u).
Overload operator().
38 int main()
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;
48
49 cout << "vector v contains:n";
50 std::copy( integers.begin(), integers.end(), output );
51
52 // calculate sum of squares of elements of vector integers
53 // using binary function sumSquares
54 result = std::accumulate( integers.begin(), integers.end(),
55 0, sumSquares );
56
57 cout << "nnSum of squares of elements in integers using "
58 << "binarynfunction sumSquares: " << result;
59
44
fig21_42.cpp
(3 of 4)
畉u ti棚n, accumulate
truy畛n 0 v ph畉n t畛
th畛 nh畉t l畉n l動畛t lm
c叩c tham s畛. Sau 坦, n坦
d湛ng k畉tqu畉 tr畉 v畛 lm
tham s畛 th畛 nh畉t, v l畉p
qua c叩c ph畉n t畛 c嘆n l畉i.
60 // calculate sum of squares of elements of vector integers
61 // using binary-function object
62 result = std::accumulate( integers.begin(), integers.end(),
63 0, SumSquaresClass< int >() );
64
65 cout << "nnSum of squares of elements in integers using "
66 << "binarynfunction object of type "
67 << "SumSquaresClass< int >: " << result << endl;
68
69 return 0;
70
71 } // end main
45
fig21_42.cpp
(4 of 4)
fig21_42.cpp
output (1 of 1)
vector v contains:
1 2 3 4 5 6 7 8 9 10

Sum of squares of elements in integers using binary
function sumSquares: 385

Sum of squares of elements in integers using binary
function object of type SumSquaresClass< int >: 385
d湛ng accumulate v畛i
m畛t function object.

More Related Content

7 stl c++

  • 1. Th動 vi畛n chu畉n C++ Standard Template Library (STL)
  • 2. Th動 vi畛n khu担n m畉u chu畉n - STL Th動 vi畛n chu畉n C++ bao g畛m 32 header file <algorithm> <bitset> <complex> <deque> <exception> <fstream> <functional> <iomanip> <ios> <iosfwd> <iostream> <istream> <iterator> <limits> <list> <locale> <map> <memory> <new> <numeric> <ostream> <queue> <set> <sstream> <stack> <stdexcept> <streambuf> <string> <typeinfo> <utility> <valarray> <vector>
  • 3. Th動 vi畛n khu担n m畉u chu畉n - STL Th動 vi畛n chu畉n C++ g畛m 2 ph畉n: L畛p string Th動 vi畛n khu担n m畉u chu畉n STL Ngo畉i tr畛 l畛p string, t畉t c畉 c叩c thnh ph畉n c嘆n l畉i c畛a th動 vi畛n 畛u l c叩c khu担n m畉u T叩c gi畉 畉u ti棚n c畛a STL l Alexander Stepanov, m畛c 鱈ch c畛a 担ng l x但y d畛ng m畛t c叩ch th畛 hi畛n t動 t動畛ng l畉p tr狸nh t畛ng qu叩t
  • 4. Th動 vi畛n khu担n m畉u chu畉n - STL C叩c kh叩i ni畛m trong STL 動畛c ph叩t tri畛n 畛c l畉p v畛i C++ Do 坦, ban 畉u, STL kh担ng ph畉i l m畛t th動 vi畛n C++, m n坦 達 動畛c chuy畛n 畛i thnh th動 vi畛n C++ Nhi畛u t動 t動畛ng d畉n 畉n s畛 ph叩t tri畛n c畛a STL 達 動畛c ci 畉t ph畉n no trong Scheme, Ada, v C
  • 5. Th動 vi畛n khu担n m畉u chu畉n - STL M畛t s畛 l畛i khuy棚n v畛 STL STL 動畛c thi畉t k畉 畉p v hi畛u qu畉 - kh担ng c坦 th畛a k畉 hay hm 畉o trong b畉t k畛 畛nh ngh挑a no T畛 t動 t動畛ng l畉p tr狸nh t畛ng qu叩t d畉n t畛i nh畛ng "kh畛i c董 b畉n" (building block) m c坦 th畛 k畉t h畛p v畛i nhau theo 畛 ki畛u Tuy lm quen v畛i STL t畛n kh担ng 鱈t th畛i gian nh動ng thnh qu畉 ti畛m tng v畛 nng xu畉t r畉t x畛ng 叩ng v畛i th畛i gian 畉u t動 T坦m l畉i h達y h畛c v h達y s畛 d畛ng! Bi gi畉ng ny ch畛 畛 gi畛i thi畛u m畛t ph畉n r畉t nh畛 c畛a STL
  • 6. Gi畛i thi畛u STL Ba thnh ph畉n ch鱈nh c畛a STL C叩c thnh ph畉n r畉t m畉nh x但y d畛ng d畛a tr棚n template Container: c叩c c畉u tr炭c d畛 li畛u template Iterator: gi畛ng con tr畛, d湛ng 畛 truy nh畉p c叩c ph畉n t畛 d畛 li畛u c畛a c叩c container Algorithm: c叩c thu畉t to叩n 畛 thao t叩c d畛 li畛u, t狸m ki畉m, s畉p x畉p, v.v..
  • 7. Gi畛i thi畛u v畛 c叩c Container 3 lo畉i container Sequence container container chu畛i c叩c c畉u tr炭c d畛 li畛u tuy畉n t鱈nh (vector, danh s叩ch li棚n k畉t) first-class container vector, deque, list Associative container container li棚n k畉t c叩c c畉u tr炭c phi tuy畉n, c坦 th畛 t狸m ph畉n t畛 nhanh ch坦ng first-class container c叩c c畉p kh坦a/gi叩 tr畛 set, multiset, map, multimap Container adapter c叩c b畛 t動董ng th鱈ch container stack, queue, priority_queue
  • 8. C叩c hm thnh vi棚n STL C叩c hm thnh vi棚n m畛i container 畛u c坦 Default constructor, copy constructor, destructor empty max_size, size = < <= > >= == != swap C叩c hm thnh vi棚n c畛a first-class container begin, end rbegin, rend erase, clear
  • 9. Gi畛i thi畛u v畛 Iterator Iterator t動董ng t畛 nh動 con tr畛 tr畛 t畛i c叩c ph畉n t畛 trong m畛t container c叩c to叩n t畛 iterator cho m畛i container * truy nh畉p ph畉n t畛 動畛c tr畛 t畛i ++ tr畛 t畛i ph畉n t畛 ti畉p theo begin() tr畉 v畛 iterator tr畛 t畛i ph畉n t畛 畉u ti棚n end() tr畉 v畛 iterator tr畛 t畛i ph畉n t畛 畉c bi畛t ch畉n cu畛i container
  • 10. C叩c lo畉i Iterator Input (v鱈 d畛: istream_iterator) 畛c c叩c ph畉n t畛 t畛 m畛t container, h畛 tr畛 ++,+= (ch畛 ti畉n) Output (v鱈 d畛: ostream_iterator) Ghi c叩c ph畉n t畛 vo container, h畛 tr畛 ++,+= (ch畛 ti畉n) Forward (v鱈 d畛: hash_set<T> iterator) K畉t h畛p input iterator v output iterator Multi-pass (c坦 th畛 duy畛t chu畛i nhi畛u l畉n) Bidirectional (V鱈 d畛: list<T> iterator) Nh動 forward iterator, nh動ng c坦 th畛 l湛i (--,-=) Randomaccess (V鱈 d畛: vector<T> iterator) Nh動 bidirectional, nh動ng c嘆n c坦 th畛 nh畉y t畛i ph畉n t畛 t湛y 箪
  • 11. C叩c lo畉i Iterator 動畛c h畛 tr畛 Sequence container vector: random access deque: random access list: bidirectional Associative container (h畛 tr畛 c叩c lo畉i bidirectional) set, multiset,map, multimap Container adapter (kh担ng h畛 tr畛 iterator) stack, queue, priority_queue
  • 12. C叩c ph辿p to叩n 畛i v畛i Iterator Input iterator ++ , =*p , -> , == , != Output iterator ++ , *p= , p = p1 Forward iterator K畉t h畛p c叩c to叩n t畛 c畛a input v output iterator Bidirectional iterator c叩c to叩n t畛 cho forward, v -- Random iterator c叩c to叩n t畛 cho bidirectional, v + , +=, -, -=, >, >=, <, <=,[]
  • 13. Gi畛i thi畛u c叩c thu畉t to叩n Algorithm STL c坦 c叩c thu畉t to叩n 動畛c s畛 d畛ng t畛ng qu叩t cho nhi畛u lo畉i container thao t叩c gi叩n ti畉p v畛i c叩c ph畉n t畛 qua c叩c iterator th動畛ng d湛ng cho c叩c ph畉n t畛 trong m畛t chu畛i chu畛i x叩c 畛nh b畛i m畛t c畉p iterator tr畛 t畛i ph畉n t畛 畉u ti棚n v cu畛i c湛ng c畛a chu畛i c叩c thu畉t to叩n th動畛ng tr畉 v畛 iterator v鱈 d畛: find() tr畉 v畛 iterator tr畛 t畛i ph畉n t畛 c畉n t狸m ho畉c tr畉 v畛 end() n畉u kh担ng t狸m th畉y s畛 d畛ng c叩c thu畉t to叩n 動畛c cung c畉p gi炭p l畉p tr狸nh vi棚n ti畉t ki畛m th畛i gian v c担ng s畛c
  • 14. Sequence Container 3 lo畉i sequence container: vector d畛a theo m畉ng deque d畛a theo m畉ng list danh s叩ch li棚n k畉t hi畛u qu畉 cao
  • 15. vector Sequence Container vector <vector> c畉u tr炭c d畛 li畛u v畛i c叩c v湛ng nh畛 li棚n ti畉p truy nh畉p c叩c ph畉n t畛 b畉ng to叩n t畛 [ ] s畛 d畛ng khi d畛 li畛u c畉n 動畛c s畉p x畉p v truy nh畉p d畛 dng C董ch畉 ho畉t 畛ng khi h畉t b畛 nh畛 c畉p ph叩t m畛t v湛ng nh畛 li棚n l畛c l畛n h董n t畛 sao ch辿p ra v湛ng nh畛 m畛i tr畉 l畉i v湛ng nh畛 c滴 s畛 d畛ng randomaccess iterator
  • 16. vector Sequence Container Khai b叩o std::vector <type> v; type l ki畛u d畛 li畛u c畛a ph畉n t畛 d畛 li畛u (int, float, v.v..) Iterator std::vector<type>::iterator iterVar; tr動畛ng h畛p th担ng th動畛ng std::vector<type>::const_iterator iterVar; const_iterator kh担ng th畛 s畛a 畛i c叩c ph畉n t畛 std::vector<type>::reverse_iterator iterVar; Visits elements in reverse order (end to beginning) Use rbegin to get starting point Use rend to get ending point
  • 17. vector Sequence Container C叩c hm thnh vi棚n c畛a vector v.push_back(value) th棚m ph畉n t畛 vo cu畛i (sequence container no c滴ng c坦 hm ny). v.size() k鱈ch th動畛c hi畛n t畉i c畛a vector v.capacity() k鱈ch th動畛c c坦 th畛 l動u tr畛 tr動畛c khi ph畉i c畉p ph叩t l畉i khi c畉p ph叩t l畉i s畉 c畉p ph叩t k鱈ch th動畛c g畉p 担i vector<type> v(a, a + SIZE) t畉o vector vt畛 SIZE ph畉n t畛 畉u ti棚n c畛a m畉ng a
  • 18. vector Sequence Container C叩c hm thnh vi棚n c畛a vector v.insert(iterator, value ) ch竪n value vo tr動畛c v畛 tr鱈 c畛a iterator v.insert(iterator, array , array + SIZE) ch竪n vo vector SIZE ph畉n t畛 畉u ti棚n c畛a m畉ng array v.erase( iterator ) x坦a ph畉n t畛 kh畛i container v.erase( iter1, iter2 ) x坦a b畛 c叩c ph畉n t畛 b畉t 畉u t畛 iter1 畉n h畉t ph畉n t畛 li畛n tr動畛c iter2
  • 19. vector Sequence Container C叩c hm thnh vi棚n c畛a vector v.clear() X坦a ton b畛 container v.front(), v.back() Tr畉 v畛 ph畉n t畛 畉u ti棚n v cu畛i c湛ng v.[elementNumber] = value; G叩n gi叩 tr畛 value cho m畛t ph畉n t畛 v.at[elementNumber] = value; Nh動 tr棚n, nh動ng k竪m theo ki畛m tra ch畛 s畛 h畛p l畛 c坦 th畛 n辿m ngo畉i l畛 out_of_bounds
  • 20. vector Sequence Container ostream_iterator std::ostream_iterator< type > Name( outputStream, separator ); type: outputs values of a certain type outputStream: iterator output location separator: character separating outputs Example std::ostream_iterator< int > output( cout, " " ); std::copy( iterator1, iterator2, output ); Copies elements from iterator1 up to (not including) iterator2 to output, an ostream_iterator 20
  • 21. 1 // Fig. 21.14: fig21_14.cpp 2 // Demonstrating standard library vector class template. 3 #include <iostream> 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 #include <vector> // vector class-template definition 10 11 // prototype for function template printVector 12 template < class T > 13 void printVector( 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 << "The initial size of integers is: " 23 << integers.size() 24 << "nThe initial capacity of integers is: " 25 << integers.capacity(); 26 21 T畉o m畛t vector ch畛a c叩c gi叩 tr畛 int G畛i c叩c hm thnh vi棚n.
  • 22. 27 // function push_back is in every sequence collection 28 integers.push_back( 2 ); 29 integers.push_back( 3 ); 30 integers.push_back( 4 ); 31 32 cout << "nThe size of integers is: " << integers.size() 33 << "nThe capacity of integers is: " 34 << integers.capacity(); 35 36 cout << "nnOutput array using pointer notation: "; 37 38 for ( int *ptr = array; ptr != array + SIZE; ++ptr ) 39 cout << *ptr << ' '; 40 41 cout << "nOutput vector using iterator notation: "; 42 printVector( integers ); 43 44 cout << "nReversed contents of vector integers: "; 45 22 s畛 d畛ng push_back 畛 th棚m ph畉n t畛 vo cu畛i vector
  • 23. 46 std::vector< int >::reverse_iterator reverseIterator; 47 48 for ( reverseIterator = integers.rbegin(); 49 reverseIterator!= integers.rend(); 50 ++reverseIterator ) 51 cout << *reverseIterator << ' '; 52 53 cout << endl; 54 55 return 0; 56 57 } // end main 58 59 // function template for outputting vector elements 60 template < class T > 61 void printVector( const std::vector< T > &integers2 ) 62 { 63 std::vector< T >::const_iterator constIterator; 64 65 for ( constIterator = integers2.begin(); 66 constIterator != integers2.end(); 67 constIterator++ ) 68 cout << *constIterator << ' '; 69 70 } // end function printVector 23 fig21_14.cpp (3 of 3) Duy畛t ng動畛c vector b畉ng m畛t reverse_iterator. Template function 畛 duy畛t vector theo chi畛u ti畉n.
  • 24. The initial size of v is: 0 The initial capacity of v is: 0 The size of v is: 3 The capacity of v is: 4 Contents of array a using pointer notation: 1 2 3 4 5 6 Contents of vector v using iterator notation: 2 3 4 Reversed contents of vector v: 4 3 2 24 fig21_14.cpp output (1 of 1)
  • 25. Container Adapter Container adapter stack, queue v priority_queue Kh担ng ph畉i first class container, cho n棚n Kh担ng h畛 tr畛 iterator Kh担ng cung c畉p c畉u tr炭c d畛 li畛u L畉p tr狸nh vi棚n c坦 th畛 ch畛n c叩ch ci 畉t (s畛 d畛ng c畉u tr炭c d畛 li畛u no) 畛u cung c畉p c叩c hm thnh vi棚n push v pop b棚n c畉nh c叩c hm thnh vi棚n kh叩c.
  • 26. stack Adapter stack Header <stack> ch竪n v x坦a t畉i m畛t 畉u C畉u tr炭c Last-in, first-out (LIFO) C坦 th畛 ch畛n ci 畉t b畉ng vector, list, ho畉c deque (m畉c 畛nh) Khai b叩o stack<type, vector<type> > myStack; stack<type, list<type> > myOtherStack; stack<type> anotherStack; // default deque ch畛n ci 畉t l vector, list hay deque kh担ng lm thay 畛i hnh vi, ch畛 畉nh h動畛ng t畛i hi畛u qu畉 (ci b畉ng deque v vector l nhanh nh畉t)
  • 27. 1 // Fig. 21.23: fig21_23.cpp 2 // Standard library adapter stack test program. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <stack> // stack adapter definition 9 #include <vector> // vector class-template definition 10 #include <list> // list class-template definition 11 12 // popElements function-template prototype 13 template< class T > 14 void popElements( T &stackRef ); 15 16 int main() 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 27 fig21_23.cpp (1 of 3) T畉o stack b畉ng nhi畛u ki畛u ci 畉t.
  • 28. 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 << "Popping from intDequeStack: "; 37 popElements( intDequeStack ); 38 cout << "nPopping from intVectorStack: "; 39 popElements( intVectorStack ); 40 cout << "nPopping from intListStack: "; 41 popElements( intListStack ); 42 43 cout << endl; 44 45 return 0; 46 47 } // end main 48 28 fig21_23.cpp (2 of 3) s畛 d畛ng hm thnh vi棚n push.
  • 29. 49 // pop elements from stack object to which stackRef refers 50 template< class T > 51 void popElements( T &stackRef ) 52 { 53 while ( !stackRef.empty() ) { 54 cout << stackRef.top() << ' '; // view top element 55 stackRef.pop(); // remove top element 56 57 } // end while 58 59 } // end function popElements 29 fig21_23.cpp (3 of 3) fig21_23.cpp output (1 of 1) Popping from intDequeStack: 9 8 7 6 5 4 3 2 1 0 Popping from intVectorStack: 9 8 7 6 5 4 3 2 1 0 Popping from intListStack: 9 8 7 6 5 4 3 2 1 0
  • 30. C叩c thu畉t to叩n Tr動畛c STL c叩c th動 vi畛n c畛a c叩c h達ng kh叩c nhau kh担ng t動董ng th鱈ch C叩c thu畉t to叩n 動畛c x但y d畛ng v g畉n vo trong c叩c l畛p container STL t叩ch r畛i c叩c container v c叩c thu畉t to叩n l畛i th畉: d畛 b畛 sung c叩c thu畉t to叩n m畛i hi畛u qu畉 h董n, tr叩nh c叩c l畛i g畛i hm 畉o header <algorithm>
  • 31. remove, remove_if, remove_copy v remove_copy_if remove remove( iter1, iter2, value); B畛 m畛i ph畉n t畛 c坦 gi叩 tr畛 value trong kho畉ng (iter1 - iter2) theo c叩ch sau: Chuy畛n c叩c ph畉n t畛 c坦 gi叩 tr畛 value xu畛ng cu畛i kh担ng thay 畛i k鱈ch th動畛c c畛a container ho畉c th畛c s畛 x坦a c叩c ph畉n t畛 Tr畉 v畛 iterator t畛i k畉t th炭c m畛i c畛a container c叩c ph畉n t畛 sau k畉t th炭c m畛i l kh担ng x叩c 畛nh
  • 32. remove, remove_if, remove_copy v remove_copy_if remove_copy remove_copy(iter1, iter2, iter3, value); trong kho畉ng iter1-iter2, ch辿p c叩c ph畉n t畛 kh叩c value vo iter3 (output iterator) remove_if gi畛ng remove tr畉 v畛 iterator t畛i ph畉n t畛 cu畛i c湛ng b畛 c叩c ph畉n t畛 m hm tr畉 v畛 true remove_if(iter1,iter2, function); c叩c ph畉n t畛 動畛c truy畛n cho function, hm ny tr畉 v畛 gi叩 tr畛 bool
  • 33. remove, remove_if, remove_copy v remove_copy_if remove_copy_if gi畛ng remove_copy v remove_if remove_copy_if(iter1, iter2, iter3, function);
  • 34. C叩c thu畉t to叩n to叩n h畛c random_shuffle(iter1, iter2) x叩o tr畛n c叩c ph畉n t畛 trong kho畉ng m畛t c叩ch ng畉u nhi棚n count(iter1, iter2, value) tr畉 v畛 s畛 l畉n xu畉t hi畛n c畛a value trong kho畉ng count_if(iter1, iter2, function) 畉m s畛 ph畉n t畛 lm function tr畉 v畛 true min_element(iter1, iter2) tr畉 v畛 iterator t畛i ph畉n t畛 nh畛 nh畉t max_element(iter1, iter2) tr畉 v畛 iterator t畛i ph畉n t畛 l畛n nh畉t
  • 35. C叩c thu畉t to叩n to叩n h畛c accumulate(iter1, iter2) tr畉 v畛 t畛ng c叩c ph畉n t畛 trong kho畉ng for_each(iter1, iter2, function) G畛i hm function cho m畛i ph畉n t畛 trong kho畉ng kh担ng s畛a 畛i ph畉n t畛 transform(iter1, iter2, iter3, function) g畛i function cho m畛i ph畉n t畛 trong kho畉ng iter1- iter2, k畉t qu畉 ghi vo iter3
  • 36. C叩c thu畉t to叩n t狸m ki畉m v s畉p x畉p c董 b畉n find(iter1, iter2, value) tr畉 v畛 iterator t畛i l畉n xu畉t hi畛n 畉u ti棚n (trong kho畉ng) c畛a value find_if(iter1, iter2, function) nh動 find tr畉 v畛 iterator khi function tr畉 v畛 true sort(iter1, iter2) s畉p x畉p c叩c ph畉n t畛 theo th畛 t畛 tng d畉n binary_search(iter1, iter2, value)
  • 37. 1 // Fig. 21.31: fig21_31.cpp 2 // Standard library search and sort algorithms. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <algorithm> // algorithm definitions 9 #include <vector> // vector class-template definition 10 11 bool greater10( int value ); // prototype 12 13 int main() 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 << "Vector v contains: "; 22 std::copy( v.begin(), v.end(), output ); 23 24 // locate first occurrence of 16 in v 25 std::vector< int >::iterator location; 26 location = std::find( v.begin(), v.end(), 16 ); 37 fig21_31.cpp (1 of 4)
  • 38. 27 28 if ( location != v.end() ) 29 cout << "nnFound 16 at location " 30 << ( location - v.begin() ); 31 else 32 cout << "nn16 not found"; 33 34 // locate first occurrence of 100 in v 35 location = std::find( v.begin(), v.end(), 100 ); 36 37 if ( location != v.end() ) 38 cout << "nFound 100 at location " 39 << ( location - v.begin() ); 40 else 41 cout << "n100 not found"; 42 43 // locate first occurrence of value greater than 10 in v 44 location = std::find_if( v.begin(), v.end(), greater10 ); 45 46 if ( location != v.end() ) 47 cout << "nnThe first value greater than 10 is " 48 << *location << "nfound at location " 49 << ( location - v.begin() ); 50 else 51 cout << "nnNo values greater than 10 were found"; 52 38 fig21_31.cpp (2 of 4)
  • 39. 53 // sort elements of v 54 std::sort( v.begin(), v.end() ); 55 56 cout << "nnVector v after sort: "; 57 std::copy( v.begin(), v.end(), output ); 58 59 // use binary_search to locate 13 in v 60 if ( std::binary_search( v.begin(), v.end(), 13 ) ) 61 cout << "nn13 was found in v"; 62 else 63 cout << "nn13 was not found in v"; 64 65 // use binary_search to locate 100 in v 66 if ( std::binary_search( v.begin(), v.end(), 100 ) ) 67 cout << "n100 was found in v"; 68 else 69 cout << "n100 was not found in v"; 70 71 cout << endl; 72 73 return 0; 74 75 } // end main 76 39 fig21_31.cpp (3 of 4)
  • 40. 77 // determine whether argument is greater than 10 78 bool greater10( int value ) 79 { 80 return value > 10; 81 82 } // end function greater10 40 fig21_31.cpp (4 of 4) fig21_31.cpp output (1 of 1) 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
  • 41. Function Object functor (<functional>) C叩c畛it動畛ngc坦th畛g畛inh動hmb畉ngto叩nt畛() STL function objects Type divides< T > arithmetic equal_to< T > relational greater< T > relational greater_equal< T > relational less< 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
  • 42. 1 // Fig. 21.42: fig21_42.cpp 2 // Demonstrating function objects. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 #include <vector> // vector class-template definition 9 #include <algorithm> // copy algorithm 10 #include <numeric> // accumulate algorithm 11 #include <functional> // binary_function definition 12 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 42 fig21_42.cpp (1 of 4) T畉o m畛t hm 畛 d湛ng v畛i accumulate.
  • 43. 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 43 fig21_42.cpp (2 of 4)T畉o m畛t function object (n坦 c嘆n c坦 th畛 坦ng g坦i d畛 li畛u). Overload operator().
  • 44. 38 int main() 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; 48 49 cout << "vector v contains:n"; 50 std::copy( integers.begin(), integers.end(), output ); 51 52 // calculate sum of squares of elements of vector integers 53 // using binary function sumSquares 54 result = std::accumulate( integers.begin(), integers.end(), 55 0, sumSquares ); 56 57 cout << "nnSum of squares of elements in integers using " 58 << "binarynfunction sumSquares: " << result; 59 44 fig21_42.cpp (3 of 4) 畉u ti棚n, accumulate truy畛n 0 v ph畉n t畛 th畛 nh畉t l畉n l動畛t lm c叩c tham s畛. Sau 坦, n坦 d湛ng k畉tqu畉 tr畉 v畛 lm tham s畛 th畛 nh畉t, v l畉p qua c叩c ph畉n t畛 c嘆n l畉i.
  • 45. 60 // calculate sum of squares of elements of vector integers 61 // using binary-function object 62 result = std::accumulate( integers.begin(), integers.end(), 63 0, SumSquaresClass< int >() ); 64 65 cout << "nnSum of squares of elements in integers using " 66 << "binarynfunction object of type " 67 << "SumSquaresClass< int >: " << result << endl; 68 69 return 0; 70 71 } // end main 45 fig21_42.cpp (4 of 4) fig21_42.cpp output (1 of 1) vector v contains: 1 2 3 4 5 6 7 8 9 10 Sum of squares of elements in integers using binary function sumSquares: 385 Sum of squares of elements in integers using binary function object of type SumSquaresClass< int >: 385 d湛ng accumulate v畛i m畛t function object.