1
template
<
typename T1,typename T2
>
2
class
mypair :
public
pair
<
T1,T2
>
3
{
4
public
:
5
inline mypair ():pair()
{}
;
6
inline mypair(
const
T1
&
a,
const
T2
&
b):pair
<
T1,T2
>
(a,b)
{}
;
7
inline mypair(
const
mypair
<
T1,T2
>
&
a):pair
<
T1,T2
>
(a)
{}
;
8
friend ostream
&
operator
<<
<
T1,T2
>
(ostream
&
os,mypair
<
T1,T2
>
&
p);
9
protected
:
10
private
:
11
}
;
12
template
<
typename T1,typename T2
>
13
ostream
&
operator
<<
(ostream
&
os,mypair
<
T1,T2
>&
p)
14
{
15
os
<<
"
\n cout pair
"
;
16
os
<<
endl
<<
"
"
<<
p.first;
17
os
<<
endl
<<
"
"
<<
p.second;
18
return
os;
19
}
20
21
22
int
_tmain(
int
argc, _TCHAR
*
argv[])
23
{
24
f();
25
mypair
<
int
,
float
>
a(
1
,
2.0333
);
26
mypair
<
int
,
float
>
b(
2
,
6.04343
);
27
mypair
<
int
,
float
>
c(b);
28
cout
<<
a;
29
cout
<<
b;
30
cout
<<
c;
31
a.swap(b);
32
cout
<<
a;
33
cout
<<
b;
34
35
cout
<<
(a
==
b);
36
37
return
0
;
38
}
39
#include <utility>
template <typename T1,typename T2>
class pair
{
1.空构造
2.双参数构造
3.拷贝构造
4.swap交换
5.内含2个T1,T2型 变量(first ,second);
}
pair 是struct ,所有成员公有
template <typename T1,typename T2>
pair<T1,T2> make_pair(const T1&,const T2&)
操作符重载模版
make_pair(a,b);//按值返回个pair< >对象;
make_pair<int,float >(a,b);
auto_ptr<T> ptr(new int);//智能指针类
辅助函数模板
max ,min, swap
template<class Type> void swap( Type& _Left, Type& _Right ); #include <algorithm>
4个比较算子
#include <utility>
using namespace rel_ops;
就重载了这几个算子
template<Class Type> bool operator!=( const Type& _Left, const Type& _Right);template<class Type1, class Type2> bool operator!=( const pair<Type1, Type2>& _Left, const pair<Type1, Type2>& _Right );
非pair<> 的在 std::rel_ops中
1
以下是迭代器基类
2
template<class _Category,
3
class _Ty,
4
class _Diff = ptrdiff_t,
5
class _Pointer = _Ty *,
6
class _Reference = _Ty&>
7
struct iterator
8
{ // base type for all iterator classes
9
typedef _Category iterator_category;
10
typedef _Ty value_type;
11
typedef _Diff difference_type;
12
typedef _Diff distance_type; // retained
13
typedef _Pointer pointer;
14
typedef _Reference reference;
15
};
16
1
template<class _Ty>
2
class allocator
3
//基类,是个空结构
4
: public _Allocator_base<_Ty>
5
{ // generic allocator for objects of class _Ty
6
public:
7
typedef _Allocator_base<_Ty> _Mybase;
8
typedef typename _Mybase::value_type value_type;
9
10
11
typedef value_type _FARQ *pointer;
12
typedef value_type _FARQ& reference;
13
typedef const value_type _FARQ *const_pointer;
14
typedef const value_type _FARQ& const_reference;
15
16
typedef _SIZT size_type;
17
typedef _PDFT difference_type;
18
19
template<class _Other>
20
struct rebind
21
{ // convert an allocator<_Ty> to an allocator <_Other>
22
typedef allocator<_Other> other;
23
};
24
25
pointer address(reference _Val) const
26
{ // return address of mutable _Val
27
return (&_Val);
28
}
29
30
const_pointer address(const_reference _Val) const
31
{ // return address of nonmutable _Val
32
return (&_Val);
33
}
34
35
allocator()
36
{ // construct default allocator (do nothing)
37
}
38
39
allocator(const allocator<_Ty>&)
40
{ // construct by copying (do nothing)
41
}
42
43
template<class _Other>
44
allocator(const allocator<_Other>&)
45
{ // construct from a related allocator (do nothing)
46
}
47
48
template<class _Other>
49
allocator<_Ty>& operator=(const allocator<_Other>&)
50
{ // assign from a related allocator (do nothing)
51
return (*this);
52
}
53
54
void deallocate(pointer _Ptr, size_type)
55
{ // deallocate object at _Ptr, ignore size
56
operator delete(_Ptr);
57
}
58
59
pointer allocate(size_type _Count)
60
{ // allocate array of _Count elements
61
return (_Allocate(_Count, (pointer)0));
62
}
63
64
pointer allocate(size_type _Count, const void _FARQ *)
65
{ // allocate array of _Count elements, ignore hint
66
return (allocate(_Count));
67
}
68
69
void construct(pointer _Ptr, const _Ty& _Val)
70
{ // construct object at _Ptr with value _Val
71
_Construct(_Ptr, _Val);
72
}
73
74
void destroy(pointer _Ptr)
75
{ // destroy object at _Ptr
76
_Destroy(_Ptr);
77
}
78
79
_SIZT max_size() const
80
{ // estimate maximum array size
81
_SIZT _Count = (_SIZT)(-1) / sizeof (_Ty);
82
return (0 < _Count ? _Count : 1);
83
}
84
};
以上是.net 2003 中 iterator 和 allocator 的代码
可以看出 stl是借助allocate 模板来分配元素 空间的
一个自定义迭代器,和自定义容器
1
template<typename item>
2
class maniter;
3
4
template <typename T>
5
class man
6

{
7
public:
8
typedef maniter<T> iterator ;
9
man()
10
{
11
array=NULL;
12
size=0;
13
}
14
iterator begin()
15
{
16
iterator *pt =new iterator;
17
pt->p=array;
18
return *pt;
19
}
20
iterator end()
21
{
22
iterator *pt=new iterator;
23
pt->p=array+sizeof(T)*size;
24
return *pt;
25
}
26
man(int count)
27
{
28
29
array=new T [count];
30
31
size=count;
32
for (int i=0;i<size;i++)
33
{
34
35
*(array+i)=i;
36
}
37
38
}
39
man(const man& a)
40
{
41
42
43
if(this!=&a)
44
{
45
size=a.size;
46
47
try
{
48
array=new T[a.size];
49
}
50
catch (bad_alloc e)
{
51
52
}
53
}
54
for (int i=0;i<a.size;i++)
55
{
56
57
*(array+i)=a.array[i];
58
}
59
60
61
}
62
void operator =(const man& a)
63
{
64
65
66
delete [] array;
67
array=new T [a.size];
68
size=a.size;
69
for (int i=0;i<a.size;i++)
70
{
71
*(array+i)=a.array[i];
72
}
73
74
//return *this;
75
}
76
~man()
77
{
78
delete [] array;
79
size=0;
80
}
81
man<T>& operator++()
82
{
83
this->array++;
84
return *this;
85
}
86
const man<T> operator++(int )
87
{
88
man<T> temp=*this;
89
(this->array)++;
90
return temp;
91
}
92
93
protected:
94
friend ostream& operator<< <T>(ostream& os,const man<T> & a);
95
private:
96
T * array;
97
int size;
98
};
99
template <typename T>
100
ostream& operator<<(ostream& os, const man<T>& a)
101

{
102
cout<<"\n in printing man"<<endl;
103
if (!a.size)
104
{
105
cout<<"\n"<<" array is null";
106
}
107
for (int i=0;i<a.size;i++)
108
{
109
110
os<<" "<<a.array[i];
111
}
112
cout<<endl;
113
return os;
114
}
115
116
template <typename it,typename T>
117
it myfind(it begin, it end , const T& a)
118

{
119
for (;begin!=end&&*begin!=a;++begin)
120
{}
121
return begin;
122
}
123
124
template<typename item>
125
class maniter
126

{
127
public:
128
item* p;
129
maniter():p(0)
130
{
131
132
}
133
item& operator*()
{return *p;};
134
item* operator->()
{return p;};
135
item& operator++()
{p++;return *p;};
136
item operator++(int)
{item temp=*this;this++;return temp;}
137
bool operator==(const maniter & ele)
138
{
139
return *p==*(ele.p);
140
}
141
bool operator!=(const maniter & ele)
142
{
143
return *p!=*(ele.p);
144
}
145
146
protected:
147
private:
148
149
};
150
int _tmain(int argc, _TCHAR* argv[])
151

{
152
/**//*
153
vector<man<int> > a;
154
for (int i=1;i<10;++i)
155
{
156
cout<<"\n "<<i;
157
man<int>* manb=new man<int> (i);
158
a.push_back(*manb);
159
delete manb;
160
161
}
162
*/
163
man<int > b(5);
164
cout<<(*myfind(b.begin(),b.end(),3));//very perfect,自定义迭代器成功实现
165
//迭代器就是包含容器节点指针的类模板对象,提供 *,->,++,--,==,!= (essential)操作的,这些操作需要迭代器
166
//对容器结构了解,如本列中maniter 就清楚知道man 类中的节点结构是个1维数组
167
return 0;
168
}
169
170
类型萃取技术(迭代器中广泛使用的技术)
template <typename T>
struct iterator
{
typedef T value_type;
};
template <typename T>
struct iterator_trait
{
typedef T typename T::iterator::value_type value_type;
};
template <typename T>
typename iterator_trait<T>::value_type
f(T)
{};
萃取机 iterator_trait<迭代器〉 快速的从迭代器中 取出 迭代器实际类型
(very important)
posted on 2006-05-31 16:53
黄大仙 阅读(1226)
评论(0) 编辑 收藏 引用 所属分类:
c++