临时对象的产生与运用
1#include <iostream>
2#include <vector>
3#include <algorithm>
4using namespace std;
5
6template <typename T>
7class print
8{
9 public:
10 void operator()(const T& elem)
11 {
12 cout << elem << ' ';
13 }
14};
15
16int main(void)
17{
18 int ia[6] = {0, 1, 2, 3, 4, 5};
19 vector<int> iv(ia, ia + 6);
20
21 for_each(iv.begin(), iv.end(), print<int>());
22 return 0;
23}
24
25
for_each
将仿函数f施行于[first, last)区间的每一个元素身上。f不可以改变元素
内容,因为first和last都是InputIterators,不保证接受赋值行为
(assignment)。如果想要一一修改元素内容,应该使用算法transform()。
f可返回一个值,但该值会被忽略。
1template <typename InputIterator, typename Function>
2Function for_each(InputIterator first, InputIterator last, Function f)
3{
4 for ( ; first != last; ++first)
5 {
6 f(*first); //调用仿函数f的function call操作符。返回值忽略
7 }
8 return f;
9
10}
11
12
静态常量整数成员在class内部直接初始化
1#include <iostream>
2using namespace std;
3
4template <typename T>
5class testClass
6{
7 public:
8 static const int _datai = 5;
9 static const long _datal = 3L;
10 static const char _datac = 'c';
11};
12
13int main(void)
14{
15 cout << testClass<int>::_datai << endl; //5
16 cout << testClass<int>::_datal << endl; //3
17 cout << testClass<int>::_datac << endl; //c
18}
19
20
increment/decrement/dereference操作符
1#include <iostream>
2using namespace std;
3
4class INT
5{
6 friend ostream& operator<<(ostream& os, const INT& i);
7 public:
8 INT(int i):m_i(i)
9 {
10 ;
11 }
12 //prefix:increment and then fetch
13 INT& operator++()
14 {
15 ++(this->m_i);
16 return *this;
17 }
18 //postfix:fetch and then increment
19 const INT operator++(int)
20 {
21 INT temp = *this;
22 ++(*this);
23 return temp;
24 }
25 //prefix:decrement and then fetch
26 INT& operator--()
27 {
28 --(this->m_i);
29 return *this;
30 }
31 //postfix:fetch and then decrement
32 const INT operator--(int)
33 {
34 INT temp = *this;
35 --(*this);
36 return temp;
37 }
38 //dereference
39 int& operator*()const
40 {
41 return (int&)m_i;
42 }
43 private:
44 int m_i;
45};
46ostream& operator<<(ostream& os, const INT& i)
47{
48 os << '[' << i.m_i << ']';
49 return os;
50}
51
52int main(void)
53{
54 INT I(5);
55 cout << I++; //[5]
56 cout << ++I; //[7]
57 cout << I--; //[7]
58 cout << --I; //[5]
59 cout << *I; //5
60 return 0;
61}
62
63
前闭后开区间表示法[)
1template <typename InputIterator, typename T>
2InputIterator find(InputIterator first, InputIterator last, const T& value)
3{
4 while (first != last && *first != value)
5 {
6 ++first;
7 }
8 return first;
9}
10template <typename InputIterator, typename Function>
11Function for_each(InputIterator first, InputIterator last, Function f)
12{
13 for ( ; first != last; ++first)
14 {
15 f(*first);
16 }
17 return f;
18}
19
function call操作符
1#include <iostream>
2#include <algorithm>
3using namespace std;
4
5int fcmp(const void* elem1, const void* elem2);
6
7int main(void)
8{
9 int ia[10] = {32, 92, 67, 58, 10, 4, 25, 52, 59, 54};
10
11 for (int i = 0; i < 10; ++i)
12 {
13 cout << ia[i] << " "; //32 92 67 58 10 4 25 52 59 54
14 }
15
16 qsort(ia, sizeof(ia) / sizeof(int), sizeof(int), fcmp);
17
18 for (int i = 0; i < 10; ++i)
19 {
20 cout << ia[i] << " "; //4 10 25 32 52 54 58 59 67 92
21 }
22 return 0;
23}
24int fcmp(const void* elem1, const void* elem2)
25{
26 const int* i1 = (const int*)elem1;
27 const int* i2 = (const int*)elem2;
28
29 if (*i1 < *i2)
30 {
31 return -1;
32 }
33 else if (*i1 == *i2)
34 {
35 return 0;
36 }
37 else
38 {
39 return 1;
40 }
41}
42
43
重载operator()
1#include <iostream>
2using namespace std;
3
4template <typename T>
5struct Plus
6{
7 T operator()(const T& x, const T& y)const
8 {
9 return x + y;
10 }
11};
12
13template <typename T>
14struct Minus
15{
16 T operator()(const T& x, const T& y)const
17 {
18 return x - y;
19 }
20};
21
22int main(void)
23{
24 Plus<int> plusObj;
25 Minus<int> minusObj;
26 //以下使用仿函数,就像使用一般函数一样
27 cout << plusObj(3, 5) << endl; // 8
28 cout << minusObj(3, 5) << endl; // -2
29 //以下直接产生仿函数的临时对象(第一对小括号),并调用之(第二对小括号)
30 cout << plus<int>()(43, 50) << endl; //93
31 cout << minus<int>()(43, 50) << endl; //-7
32
33 return 0;
34}
35
posted on 2012-11-04 22:52
zhuxin 阅读(76)
评论(0) 编辑 收藏 引用 所属分类:
STL源码剖析