際際滷

際際滷Share a Scribd company logo
K畛 thu畉t l畉p tr狸nh
010101010101010110000101010101010101011000010101010101010101100001
010101010010101010010101010101001010101001010101010100101010100101
101001100011001001001010100110001100100100101010011000110010010010
110010110010001000001011001011001000100000101100101100100010000010
010101010101010110000101010101010101011000010101010101010101100001
010101010010101010010101010101001010101001010101010100101010100101
101001100011001001001010100110001100100100101010011000110010010010
110010110010001000001011001011001000100000101100101100100010000010
010101010101010110000101010101010101011000010101010101010101100001
010101010010101010010101010101001010101001010101010100101010100101
101001100011001001001010100110001100100100101010011000110010010010
110010110010001000001011001011001000100000101100101100100010000010
12/25/2007
y = A*x + B*u;
x = C*x + d*u;
StateController
start()
stop()
LQGController
start()
stop()
Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
2Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
N畛i dung ch動董ng 10
10.1 T畛ng qu叩t h坦a ki畛u d畛 li畛u ph畉n t畛
10.2 T畛ng qu叩t h坦a ph辿p to叩n c董 s畛
10.3 T畛ng qu叩t h坦a ph動董ng ph叩p truy l畉p ph畉n t畛
3Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
10.1 T畛ng qu叩t h坦a ki畛u d畛 li畛u ph畉n t畛
Th畛c t畉:
 Kho畉ng 80% th畛i gian lm vi畛c c畛a m畛t ng動畛i th動 k箪 vn ph嘆ng
tr動畛c 但y (v hi畛n nay 畛 nhi畛u n董i) s畛 d畛ng cho c担ng vi畛c t狸m
ki畉m, s畉p x畉p, 畛i chi畉u, so s叩nh,.... ti li畛u v h畛 s董
 Trung b狸nh, kho畉ng 80% m達 ch動董ng tr狸nh v th畛i gian th畛c hi畛n
ch動董ng tr狸nh dnh cho th畛c hi畛n c叩c thu畉t to叩n 鱈t li棚n quan tr畛c
ti畉p t畛i bi to叩n 畛ng d畛ng c畛 th畛, m li棚n quan t畛i t狸m ki畉m, s畉p
x畉p, l畛a ch畛n, so s叩nh... d畛 li畛u
D畛 li畛u 動畛c qu畉n l箪 t畛t nh畉t trong c叩c c畉u tr炭c d畉ng
"container" (vector, list, map, tree, queue,...)
V畉n 畛 x但y d畛ng hm 叩p d畛ng cho c叩c "container": Nhi畛u hm
ch畛 kh叩c nhau v畛 ki畛u d畛 li畛u tham s畛 叩p d畛ng, kh担ng kh叩c
nhau v畛 thu畉t to叩n
Gi畉i ph叩p: X但y d畛ng khu担n m畉u hm, t畛ng qu叩t h坦a ki畛u d畛
li畛u ph畉n t畛
4Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
V鱈 d畛: Thu畉t to叩n t狸m 畛a ch畛 ph畉n t畛 畉u ti棚n trong m畛t m畉ng c坦 gi叩
tr畛 l畛n h董n m畛t s畛 cho tr動畛c:
template <typename T>
T* find_elem(T *first, T* last, T k) {
while (first != last && !(*first > k))
++first;
return first;
}
void main() {
int a[] = { 1, 3, 5, 2, 7, 9, 6 };
int *p = find_elem(a,a+7,4);
if (p != a+7) {
cout << "First number > 4 :" << *p;
p = find_elem(p+1,a+7,4);
if (p != a+7) cout << "Second number > 4:" << *p;
}
double b[] = { 1.5, 3.2, 5.1, 2.4, 7.6, 9.7, 6.5 };
double *q = find_elem(b+2,b+6,7.0);
*q = 7.0;
...
}
5Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
V鱈 d畛: Thu畉t to叩n c畛ng hai vector, k畉t qu畉 l動u vo vector th畛 ba
#include <assert.h>
#include "myvector.h"
template <typename T>
void addVector(const Vector<T>& a, const Vector<T>& b,
Vector<T>& c) {
assert(a.size() == b.size() && a.size() == c.size());
for (int i= 0; i < a.size(); ++i)
c[i] = a[i] + b[i];
}
template <typename T>
Vector<T> operator+(const Vector<T>&a, const Vector<T>& b) {
Vector<T> c(a.size());
addVector(a,b,c);
return c;
}
6Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
10.2 T畛ng qu叩t h坦a ph辿p to叩n c董 s畛
V畉n 畛: Nhi畛u thu畉t to叩n ch畛 kh叩c nhau 畛 m畛t vi ph辿p to叩n
(c董 s畛) trong khi th畛c hi畛n hm
V鱈 d畛:
 C叩c thu畉t to叩n t狸m 畛a ch畛 ph畉n t畛 畉u ti棚n trong m畛t m畉ng s畛
nguy棚n c坦 gi叩 tr畛 l畛n h董n, nh畛 h董n, l畛n h董n ho畉c b畉ng, nh畛 h董n
ho畉c b畉ng, ... m畛t s畛 cho tr動畛c
 C叩c thu畉t to叩n c畛ng, tr畛, nh但n, chia,... t畛ng ph畉n t畛 c畛a hai m畉ng
s畛 th畛c, k畉t qu畉 l動u vo m畛t m畉ng m畛i
 C叩c thu畉t to叩n c畛ng, tr畛, nh但n, chia,... t畛ng ph畉n t畛 c畛a hai
vector (ho畉c c畛a hai danh s叩ch, hai ma tr畉n, ...)
Gi畉i ph叩p: T畛ng qu叩t h坦a thu畉t to叩n cho c叩c ph辿p to叩n c董 s畛
kh叩c nhau!
7Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
template <typename COMP>
int* find_elem(int* first, int* last, int k, COMP comp) {
while (first != last && !comp(*first, k))
++first;
return first;
}
bool is_greater(int a, int b) { return a > b; }
bool is_less(int a, int b) { return a < b; }
bool is_equal(int a, int b) { return a == b;}
void main() {
int a[] = { 1, 3, 5, 2, 7, 9, 6 };
int* alast = a+7;
int* p1 = find_elem(a,alast,4,is_greater);
int* p2 = find_elem(a,alast,4,is_less);
int* p3 = find_elem(a,alast,4,is_equal);
if (p1 != alast) cout << "First number > 4 is " << *p1;
if (p2 != alast) cout << "First number < 4 is " << *p2;
if (p3 != alast) cout << "First number = 4 is at index "
<< p3 - a;
char c; cin >> c;
}
8Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
Tham s畛 khu担n m畉u cho ph辿p to叩n
C坦 th畛 l m畛t hm, v鱈 d畛
bool is_greater(int a, int b){ return a > b; }
bool is_less(int a, int b) { return a < b; }
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
...
Ho畉c t畛t h董n h畉t l m畛t 畛i t動畛ng thu畛c m畛t l畛p c坦 h畛 tr畛 (n畉p
ch畛ng) to叩n t畛 g畛i hm => 畛i t動畛ng hm, v鱈 d畛
struct Greater {
bool operator()(int a, int b) { return a > b; }
};
struct Less {
bool operator()(int a, int b) { return a < b; }
};
struct Add {
int operator()(int a, int b) { return a + b; }
};
...
9Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
V鱈 d畛 s畛 d畛ng 畛i t動畛ng hm
void main() {
int a[] = { 1, 3, 5, 2, 7, 9, 6 };
int* alast = a+7;
Greater greater;
Less less;
int* p1 = find_elem(a,alast,4,greater);
int* p2 = find_elem(a,alast,4,less);
if (p1 != alast) cout << "First number > 4 is " << *p1;
if (p2 != alast) cout << "First number < 4 is " << *p2;
p1 = find_elem(a,alast,4,Greater());
p2 = find_elem(a,alast,4,Less());
char c; cin >> c;
}
10Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
働u i畛m c畛a 畛i t動畛ng hm
畛i t動畛ng hm c坦 th畛 ch畛a tr畉ng th叩i
Hm to叩n t畛 () c坦 th畛 畛nh ngh挑a inline => tng hi畛u su畉t
template <typename OP>
void apply(int* first, int* last, OP& op) {
while (first != last) {
op(*first);
++first;
}
}
class Sum {
int val;
public:
Sum(int init = 0) : val(init) {}
void operator()(int k) { val += k; }
int value() const { return val; }
};
11Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
class Prod {
int val;
public:
Prod(int init=1): val(init) {}
void operator()(int k) { val *= k; }
int value() const { return val; }
};
struct Negate {void operator()(int& k) { k = -k;} };
struct Print { void operator()(int& k) { cout << k << ' ';} };
void main() {
int a[] = {1, 2, 3, 4, 5, 6, 7};
Sum sum_op;
Prod prod_op;
apply(a,a+7,sum_op); cout << sum_op.value() << endl;
apply(a,a+7,prod_op); cout << prod_op.value() << endl;
apply(a,a+7,Negate());
apply(a,a+7,Print());
char c; cin >> c;
}
12Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
K畉t h畛p 2 b動畛c t畛ng qu叩t h坦a
template <typename T, typename COMP>
T* find_elem(T* first, T* last, T k, COMP comp) {
while (first != last && !comp(*first, k))
++first;
return first;
}
template <typename T, typename OP>
void apply(T* first, T* last, OP& op) {
while (first != last) {
op(*first);
++first;
}
}
13Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
Khu担n m畉u l畛p cho c叩c 畛i t動畛ng hm
template <typename T> struct Greater{
bool operator()(const T& a, const T& b)
{ return a > b; }
};
template <typename T> struct Less{
bool operator()(const T& a, const T& b)
{ return a > b; }
};
template <typename T> class Sum {
T val;
public:
Sum(const T& init = T(0)) : val(init) {}
void operator()(const T& k) { val += k; }
T value() const { return val; }
};
14Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
template <typename T> struct Negate {
void operator()(T& k) { k = -k;}
};
template <typename T> struct Print {
void operator()(const T& k) { cout << k << ' ';}
};
void main() {
int a[] = { 1, 3, 5, 2, 7, 9, 6 };
int* alast = a+7;
int* p1 = find_elem(a,alast,4,Greater<int>());
int* p2 = find_elem(a,alast,4,Less<int>());
if (p1 != alast) cout << "nFirst number > 4 is " << *p1;
if (p2 != alast) cout << "nFirst number < 4 is " << *p2;
Sum<int> sum_op; apply(a,a+7,sum_op);
cout<< "nSum of the sequence " << sum_op.value() << endl;
apply(a,a+7,Negate<int>());
apply(a,a+7,Print<int>());
char c; cin >> c;
}
15Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
10.3 T畛ng qu叩t h坦a truy l畉p ph畉n t畛
V畉n 畛 1: M畛t thu畉t to叩n (t狸m ki畉m, l畛a ch畛n, ph但n lo畉i, t鱈nh
t畛ng, ...) 叩p d畛ng cho m畛t m畉ng, m畛t vector, m畛t danh s叩ch
h畛c m畛t c畉u tr炭c kh叩c th畛c ch畉t ch畛 kh叩c nhau 畛 c叩ch truy
l畉p ph畉n t畛
V畉n 畛 2: Theo ph動董ng ph叩p truy畛n th畛ng, 畛 truy l畉p ph畉n t畛
c畛a m畛t c畉u tr炭c "container", n坦i chung ta c畉n bi畉t c畉u tr炭c 坦
動畛c x但y d畛ng nh動 th畉 no
 M畉ng: Truy l畉p qua ch畛 s畛 ho畉c qua con tr畛
 Vector: Truy l畉p qua ch畛 s畛
 List: Truy l畉p qua quan h畛 m坦c n畛i (s畛 d畛ng con tr畛)
 ...
16Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
V鱈 d畛 thu畉t to叩n copy
p d畛ng cho ki畛u m畉ng th担
template <class T> void copy(const T* s, T* d, int n) {
while (n--) { *d = *s; ++s; ++d; }
}
p d畛ng cho ki畛u Vector
template <class T>
void copy(const Vector<T>& s, Vector<T>& d) {
for (int i=0; i < s.size(); ++i) d[i] = s[i];
}
p d畛ng cho ki畛u List
template <class T>
void copy(const List<T>& s, List<T>& d) {
ListItem<T> *sItem=s.getHead(), *dItem=d.getHead();
while (sItem != 0) {
dItem->data = sItem->data;
dIem = dItem->getNext(); sItem=sItem->getNext();
}
}
17Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
V鱈 d畛 thu畉t to叩n find_max
p d畛ng cho ki畛u m畉ng th担
template <typename T> T* find_max(T* first, T* last) {
T* pMax = first;
while (first != last) {
if (*first > *pMax) pMax = first;
++first;
}
return pMax;
}
p d畛ng cho ki畛u Vector
template <typename T> T* find_max(const Vector<T>& v) {
int iMax = 0;
for (int i=0; i < v.size(); ++ i)
if (v[i] > v[iMax]) iMax = i;
return &v[iMax];
}
18Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
p d畛ng cho ki畛u List (達 lm quen):
template <typename T>
ListItem<T>* find_max(List<T>& l) {
ListItem<T> *pItem = l.getHead();
ListItem<T> *pMaxItem = pItem;
while (pItem != 0) {
if (pItem->data > pMaxItem->data) pMaxItem = pItem;
pItem = pItem->getNext();
}
return pMaxItem;
}
C畉n t畛ng qu叩t h坦a ph動董ng ph叩p truy l畉p ph畉n t畛!
19Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
B畛 truy l畉p (iterator)
M畛c 鱈ch: T畉o m畛t c董 ch畉 th畛ng nh畉t cho vi畛c truy l畉p ph畉n t畛
cho c叩c c畉u tr炭c d畛 li畛u m kh担ng c畉n bi畉t chi ti畉t th畛c thi b棚n
trong t畛ng c畉u tr炭c
 t動畛ng: M畛i c畉u tr炭c d畛 li畛u cung c畉p m畛t ki畛u b畛 truy l畉p
ri棚ng, c坦 畉c t鱈nh t動董ng t畛 nh動 m畛t con tr畛 (trong tr動畛ng
h畛p 畉c bi畛t c坦 th畛 l m畛t con tr畛 th畛c)
T畛ng qu叩t h坦a thu畉t to叩n copy:
template <class Iterator1, class Iterator2>
void copy(Iterator1 s, Iterator2 d, int n) {
while (n--) {
*d = *s;
++s;
++d;
}
}
C叩c ph辿p to叩n 叩p d畛ng
動畛c t動董ng t畛 con tr畛
20Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
T畛ng qu叩t h坦a thu畉t to叩n find_max:
template <typename ITERATOR>
ITERATOR find_max(ITERATOR first, ITERATOR last) {
ITERATOR pMax = first;
while (first != last) {
if (*first > *pMax) pMax = first;
++first;
}
return pMax;
}
C叩c ph辿p to叩n 叩p d畛ng
動畛c t動董ng t畛 con tr畛
21Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
B畛 sung b畛 truy l畉p cho ki畛u Vector
Ki畛u Vector l動u tr畛 d畛 li畛u d動畛i d畉ng m畛t m畉ng => c坦 th畛 s畛
d畛ng b畛 truy l畉p d動畛i d畉ng con tr畛!
template <class T> class Vector {
int nelem;
T* data;
public:
...
typedef T* Iterator;
Iteratator begin() { return data; }
Iteratator end() { return data + nElem; }
};
void main() {
Vector<double> a(5,1.0),b(6);
copy(a.begin(),b.begin(),a.size());
...
}
22Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
B畛 sung b畛 truy l畉p cho ki畛u List
template <class T> class ListIterator {
ListItem<T> *pItem;
ListIterator(ListItem<T>* p = 0) : pItem(p) {}
friend class List<T>;
public:
T& operator*() { return pItem->data; }
ListIterator<T>& operator++() {
if (pItem != 0) pItem = pItem->getNext();
return *this;
}
friend bool operator!=(ListIterator<T> a,
ListIterator<T> b) {
return a.pItem != b.pItem;
}
};
23Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
Khu担n m畉u List c畉i ti畉n
template <class T> class List {
ListItem<T> *pHead;
public:
...
ListIterator<T> begin() {
return ListIterator<T>(pHead);
}
ListIterator<T> end() {
return ListIterator<T>(0);
}
};
24Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
Bi t畉p v畛 nh
X但y d畛ng thu畉t to叩n s畉p x畉p t畛ng qu叩t 畛 c坦 th畛 叩p d畛ng cho
nhi畛u c畉u tr炭c d畛 li畛u t畉p h畛p kh叩c nhau c滴ng nh動 nhi畛u ti棚u
chu畉n s畉p x畉p kh叩c nhau. Vi畉t ch動董ng tr狸nh minh h畛a.
X但y d畛ng thu畉t to叩n c畛ng/tr畛/nh但n/chia t畛ng ph畉n t畛 c畛a hai
c畉u tr炭c d畛 li畛u t畉p h畛p b畉t k畛. Vi畉t ch動董ng tr狸nh minh h畛a.

More Related Content

C10 generic algorithms

  • 1. K畛 thu畉t l畉p tr狸nh 010101010101010110000101010101010101011000010101010101010101100001 010101010010101010010101010101001010101001010101010100101010100101 101001100011001001001010100110001100100100101010011000110010010010 110010110010001000001011001011001000100000101100101100100010000010 010101010101010110000101010101010101011000010101010101010101100001 010101010010101010010101010101001010101001010101010100101010100101 101001100011001001001010100110001100100100101010011000110010010010 110010110010001000001011001011001000100000101100101100100010000010 010101010101010110000101010101010101011000010101010101010101100001 010101010010101010010101010101001010101001010101010100101010100101 101001100011001001001010100110001100100100101010011000110010010010 110010110010001000001011001011001000100000101100101100100010000010 12/25/2007 y = A*x + B*u; x = C*x + d*u; StateController start() stop() LQGController start() stop() Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t
  • 2. 2Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t N畛i dung ch動董ng 10 10.1 T畛ng qu叩t h坦a ki畛u d畛 li畛u ph畉n t畛 10.2 T畛ng qu叩t h坦a ph辿p to叩n c董 s畛 10.3 T畛ng qu叩t h坦a ph動董ng ph叩p truy l畉p ph畉n t畛
  • 3. 3Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t 10.1 T畛ng qu叩t h坦a ki畛u d畛 li畛u ph畉n t畛 Th畛c t畉: Kho畉ng 80% th畛i gian lm vi畛c c畛a m畛t ng動畛i th動 k箪 vn ph嘆ng tr動畛c 但y (v hi畛n nay 畛 nhi畛u n董i) s畛 d畛ng cho c担ng vi畛c t狸m ki畉m, s畉p x畉p, 畛i chi畉u, so s叩nh,.... ti li畛u v h畛 s董 Trung b狸nh, kho畉ng 80% m達 ch動董ng tr狸nh v th畛i gian th畛c hi畛n ch動董ng tr狸nh dnh cho th畛c hi畛n c叩c thu畉t to叩n 鱈t li棚n quan tr畛c ti畉p t畛i bi to叩n 畛ng d畛ng c畛 th畛, m li棚n quan t畛i t狸m ki畉m, s畉p x畉p, l畛a ch畛n, so s叩nh... d畛 li畛u D畛 li畛u 動畛c qu畉n l箪 t畛t nh畉t trong c叩c c畉u tr炭c d畉ng "container" (vector, list, map, tree, queue,...) V畉n 畛 x但y d畛ng hm 叩p d畛ng cho c叩c "container": Nhi畛u hm ch畛 kh叩c nhau v畛 ki畛u d畛 li畛u tham s畛 叩p d畛ng, kh担ng kh叩c nhau v畛 thu畉t to叩n Gi畉i ph叩p: X但y d畛ng khu担n m畉u hm, t畛ng qu叩t h坦a ki畛u d畛 li畛u ph畉n t畛
  • 4. 4Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t V鱈 d畛: Thu畉t to叩n t狸m 畛a ch畛 ph畉n t畛 畉u ti棚n trong m畛t m畉ng c坦 gi叩 tr畛 l畛n h董n m畛t s畛 cho tr動畛c: template <typename T> T* find_elem(T *first, T* last, T k) { while (first != last && !(*first > k)) ++first; return first; } void main() { int a[] = { 1, 3, 5, 2, 7, 9, 6 }; int *p = find_elem(a,a+7,4); if (p != a+7) { cout << "First number > 4 :" << *p; p = find_elem(p+1,a+7,4); if (p != a+7) cout << "Second number > 4:" << *p; } double b[] = { 1.5, 3.2, 5.1, 2.4, 7.6, 9.7, 6.5 }; double *q = find_elem(b+2,b+6,7.0); *q = 7.0; ... }
  • 5. 5Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t V鱈 d畛: Thu畉t to叩n c畛ng hai vector, k畉t qu畉 l動u vo vector th畛 ba #include <assert.h> #include "myvector.h" template <typename T> void addVector(const Vector<T>& a, const Vector<T>& b, Vector<T>& c) { assert(a.size() == b.size() && a.size() == c.size()); for (int i= 0; i < a.size(); ++i) c[i] = a[i] + b[i]; } template <typename T> Vector<T> operator+(const Vector<T>&a, const Vector<T>& b) { Vector<T> c(a.size()); addVector(a,b,c); return c; }
  • 6. 6Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t 10.2 T畛ng qu叩t h坦a ph辿p to叩n c董 s畛 V畉n 畛: Nhi畛u thu畉t to叩n ch畛 kh叩c nhau 畛 m畛t vi ph辿p to叩n (c董 s畛) trong khi th畛c hi畛n hm V鱈 d畛: C叩c thu畉t to叩n t狸m 畛a ch畛 ph畉n t畛 畉u ti棚n trong m畛t m畉ng s畛 nguy棚n c坦 gi叩 tr畛 l畛n h董n, nh畛 h董n, l畛n h董n ho畉c b畉ng, nh畛 h董n ho畉c b畉ng, ... m畛t s畛 cho tr動畛c C叩c thu畉t to叩n c畛ng, tr畛, nh但n, chia,... t畛ng ph畉n t畛 c畛a hai m畉ng s畛 th畛c, k畉t qu畉 l動u vo m畛t m畉ng m畛i C叩c thu畉t to叩n c畛ng, tr畛, nh但n, chia,... t畛ng ph畉n t畛 c畛a hai vector (ho畉c c畛a hai danh s叩ch, hai ma tr畉n, ...) Gi畉i ph叩p: T畛ng qu叩t h坦a thu畉t to叩n cho c叩c ph辿p to叩n c董 s畛 kh叩c nhau!
  • 7. 7Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t template <typename COMP> int* find_elem(int* first, int* last, int k, COMP comp) { while (first != last && !comp(*first, k)) ++first; return first; } bool is_greater(int a, int b) { return a > b; } bool is_less(int a, int b) { return a < b; } bool is_equal(int a, int b) { return a == b;} void main() { int a[] = { 1, 3, 5, 2, 7, 9, 6 }; int* alast = a+7; int* p1 = find_elem(a,alast,4,is_greater); int* p2 = find_elem(a,alast,4,is_less); int* p3 = find_elem(a,alast,4,is_equal); if (p1 != alast) cout << "First number > 4 is " << *p1; if (p2 != alast) cout << "First number < 4 is " << *p2; if (p3 != alast) cout << "First number = 4 is at index " << p3 - a; char c; cin >> c; }
  • 8. 8Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t Tham s畛 khu担n m畉u cho ph辿p to叩n C坦 th畛 l m畛t hm, v鱈 d畛 bool is_greater(int a, int b){ return a > b; } bool is_less(int a, int b) { return a < b; } int add(int a, int b) { return a + b; } int sub(int a, int b) { return a - b; } ... Ho畉c t畛t h董n h畉t l m畛t 畛i t動畛ng thu畛c m畛t l畛p c坦 h畛 tr畛 (n畉p ch畛ng) to叩n t畛 g畛i hm => 畛i t動畛ng hm, v鱈 d畛 struct Greater { bool operator()(int a, int b) { return a > b; } }; struct Less { bool operator()(int a, int b) { return a < b; } }; struct Add { int operator()(int a, int b) { return a + b; } }; ...
  • 9. 9Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t V鱈 d畛 s畛 d畛ng 畛i t動畛ng hm void main() { int a[] = { 1, 3, 5, 2, 7, 9, 6 }; int* alast = a+7; Greater greater; Less less; int* p1 = find_elem(a,alast,4,greater); int* p2 = find_elem(a,alast,4,less); if (p1 != alast) cout << "First number > 4 is " << *p1; if (p2 != alast) cout << "First number < 4 is " << *p2; p1 = find_elem(a,alast,4,Greater()); p2 = find_elem(a,alast,4,Less()); char c; cin >> c; }
  • 10. 10Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t 働u i畛m c畛a 畛i t動畛ng hm 畛i t動畛ng hm c坦 th畛 ch畛a tr畉ng th叩i Hm to叩n t畛 () c坦 th畛 畛nh ngh挑a inline => tng hi畛u su畉t template <typename OP> void apply(int* first, int* last, OP& op) { while (first != last) { op(*first); ++first; } } class Sum { int val; public: Sum(int init = 0) : val(init) {} void operator()(int k) { val += k; } int value() const { return val; } };
  • 11. 11Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t class Prod { int val; public: Prod(int init=1): val(init) {} void operator()(int k) { val *= k; } int value() const { return val; } }; struct Negate {void operator()(int& k) { k = -k;} }; struct Print { void operator()(int& k) { cout << k << ' ';} }; void main() { int a[] = {1, 2, 3, 4, 5, 6, 7}; Sum sum_op; Prod prod_op; apply(a,a+7,sum_op); cout << sum_op.value() << endl; apply(a,a+7,prod_op); cout << prod_op.value() << endl; apply(a,a+7,Negate()); apply(a,a+7,Print()); char c; cin >> c; }
  • 12. 12Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t K畉t h畛p 2 b動畛c t畛ng qu叩t h坦a template <typename T, typename COMP> T* find_elem(T* first, T* last, T k, COMP comp) { while (first != last && !comp(*first, k)) ++first; return first; } template <typename T, typename OP> void apply(T* first, T* last, OP& op) { while (first != last) { op(*first); ++first; } }
  • 13. 13Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t Khu担n m畉u l畛p cho c叩c 畛i t動畛ng hm template <typename T> struct Greater{ bool operator()(const T& a, const T& b) { return a > b; } }; template <typename T> struct Less{ bool operator()(const T& a, const T& b) { return a > b; } }; template <typename T> class Sum { T val; public: Sum(const T& init = T(0)) : val(init) {} void operator()(const T& k) { val += k; } T value() const { return val; } };
  • 14. 14Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t template <typename T> struct Negate { void operator()(T& k) { k = -k;} }; template <typename T> struct Print { void operator()(const T& k) { cout << k << ' ';} }; void main() { int a[] = { 1, 3, 5, 2, 7, 9, 6 }; int* alast = a+7; int* p1 = find_elem(a,alast,4,Greater<int>()); int* p2 = find_elem(a,alast,4,Less<int>()); if (p1 != alast) cout << "nFirst number > 4 is " << *p1; if (p2 != alast) cout << "nFirst number < 4 is " << *p2; Sum<int> sum_op; apply(a,a+7,sum_op); cout<< "nSum of the sequence " << sum_op.value() << endl; apply(a,a+7,Negate<int>()); apply(a,a+7,Print<int>()); char c; cin >> c; }
  • 15. 15Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t 10.3 T畛ng qu叩t h坦a truy l畉p ph畉n t畛 V畉n 畛 1: M畛t thu畉t to叩n (t狸m ki畉m, l畛a ch畛n, ph但n lo畉i, t鱈nh t畛ng, ...) 叩p d畛ng cho m畛t m畉ng, m畛t vector, m畛t danh s叩ch h畛c m畛t c畉u tr炭c kh叩c th畛c ch畉t ch畛 kh叩c nhau 畛 c叩ch truy l畉p ph畉n t畛 V畉n 畛 2: Theo ph動董ng ph叩p truy畛n th畛ng, 畛 truy l畉p ph畉n t畛 c畛a m畛t c畉u tr炭c "container", n坦i chung ta c畉n bi畉t c畉u tr炭c 坦 動畛c x但y d畛ng nh動 th畉 no M畉ng: Truy l畉p qua ch畛 s畛 ho畉c qua con tr畛 Vector: Truy l畉p qua ch畛 s畛 List: Truy l畉p qua quan h畛 m坦c n畛i (s畛 d畛ng con tr畛) ...
  • 16. 16Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t V鱈 d畛 thu畉t to叩n copy p d畛ng cho ki畛u m畉ng th担 template <class T> void copy(const T* s, T* d, int n) { while (n--) { *d = *s; ++s; ++d; } } p d畛ng cho ki畛u Vector template <class T> void copy(const Vector<T>& s, Vector<T>& d) { for (int i=0; i < s.size(); ++i) d[i] = s[i]; } p d畛ng cho ki畛u List template <class T> void copy(const List<T>& s, List<T>& d) { ListItem<T> *sItem=s.getHead(), *dItem=d.getHead(); while (sItem != 0) { dItem->data = sItem->data; dIem = dItem->getNext(); sItem=sItem->getNext(); } }
  • 17. 17Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t V鱈 d畛 thu畉t to叩n find_max p d畛ng cho ki畛u m畉ng th担 template <typename T> T* find_max(T* first, T* last) { T* pMax = first; while (first != last) { if (*first > *pMax) pMax = first; ++first; } return pMax; } p d畛ng cho ki畛u Vector template <typename T> T* find_max(const Vector<T>& v) { int iMax = 0; for (int i=0; i < v.size(); ++ i) if (v[i] > v[iMax]) iMax = i; return &v[iMax]; }
  • 18. 18Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t p d畛ng cho ki畛u List (達 lm quen): template <typename T> ListItem<T>* find_max(List<T>& l) { ListItem<T> *pItem = l.getHead(); ListItem<T> *pMaxItem = pItem; while (pItem != 0) { if (pItem->data > pMaxItem->data) pMaxItem = pItem; pItem = pItem->getNext(); } return pMaxItem; } C畉n t畛ng qu叩t h坦a ph動董ng ph叩p truy l畉p ph畉n t畛!
  • 19. 19Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t B畛 truy l畉p (iterator) M畛c 鱈ch: T畉o m畛t c董 ch畉 th畛ng nh畉t cho vi畛c truy l畉p ph畉n t畛 cho c叩c c畉u tr炭c d畛 li畛u m kh担ng c畉n bi畉t chi ti畉t th畛c thi b棚n trong t畛ng c畉u tr炭c t動畛ng: M畛i c畉u tr炭c d畛 li畛u cung c畉p m畛t ki畛u b畛 truy l畉p ri棚ng, c坦 畉c t鱈nh t動董ng t畛 nh動 m畛t con tr畛 (trong tr動畛ng h畛p 畉c bi畛t c坦 th畛 l m畛t con tr畛 th畛c) T畛ng qu叩t h坦a thu畉t to叩n copy: template <class Iterator1, class Iterator2> void copy(Iterator1 s, Iterator2 d, int n) { while (n--) { *d = *s; ++s; ++d; } } C叩c ph辿p to叩n 叩p d畛ng 動畛c t動董ng t畛 con tr畛
  • 20. 20Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t T畛ng qu叩t h坦a thu畉t to叩n find_max: template <typename ITERATOR> ITERATOR find_max(ITERATOR first, ITERATOR last) { ITERATOR pMax = first; while (first != last) { if (*first > *pMax) pMax = first; ++first; } return pMax; } C叩c ph辿p to叩n 叩p d畛ng 動畛c t動董ng t畛 con tr畛
  • 21. 21Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t B畛 sung b畛 truy l畉p cho ki畛u Vector Ki畛u Vector l動u tr畛 d畛 li畛u d動畛i d畉ng m畛t m畉ng => c坦 th畛 s畛 d畛ng b畛 truy l畉p d動畛i d畉ng con tr畛! template <class T> class Vector { int nelem; T* data; public: ... typedef T* Iterator; Iteratator begin() { return data; } Iteratator end() { return data + nElem; } }; void main() { Vector<double> a(5,1.0),b(6); copy(a.begin(),b.begin(),a.size()); ... }
  • 22. 22Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t B畛 sung b畛 truy l畉p cho ki畛u List template <class T> class ListIterator { ListItem<T> *pItem; ListIterator(ListItem<T>* p = 0) : pItem(p) {} friend class List<T>; public: T& operator*() { return pItem->data; } ListIterator<T>& operator++() { if (pItem != 0) pItem = pItem->getNext(); return *this; } friend bool operator!=(ListIterator<T> a, ListIterator<T> b) { return a.pItem != b.pItem; } };
  • 23. 23Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t Khu担n m畉u List c畉i ti畉n template <class T> class List { ListItem<T> *pHead; public: ... ListIterator<T> begin() { return ListIterator<T>(pHead); } ListIterator<T> end() { return ListIterator<T>(0); } };
  • 24. 24Ch動董ng 10: Thu畉t to叩n t畛ng qu叩t Bi t畉p v畛 nh X但y d畛ng thu畉t to叩n s畉p x畉p t畛ng qu叩t 畛 c坦 th畛 叩p d畛ng cho nhi畛u c畉u tr炭c d畛 li畛u t畉p h畛p kh叩c nhau c滴ng nh動 nhi畛u ti棚u chu畉n s畉p x畉p kh叩c nhau. Vi畉t ch動董ng tr狸nh minh h畛a. X但y d畛ng thu畉t to叩n c畛ng/tr畛/nh但n/chia t畛ng ph畉n t畛 c畛a hai c畉u tr炭c d畛 li畛u t畉p h畛p b畉t k畛. Vi畉t ch動董ng tr狸nh minh h畛a.