侯老师说泛型技术学习分三层境界,最高境界自然就是自行扩展STL了,要自行扩展不单要清楚STL的实现及原理还要对STL有深刻理解,以达到能够编写无缝融入STL的自行扩展的组件,嘛,就是写库吧
下面就贴上一个自己写的简单的数组容器,当然迭代器为原始指针所以就不需要专门编写迭代器,STL也提供了原始指针的特化版本萃取机,所以并不需要为迭代器编写额外代码(要不然就麻烦了),为了更好理解STL空间配置器也自己写了个简单的并作了和STL差不多的封装(仅限于SGI STL)
- #include <iostream>
- #include <algorithm>
- using namespace std;
- //简单的空间配置器
- //template<int i>
- class alloc
- {
- public:
- static void* allocate(size_t n)
- {
- void *p = malloc(n);
- if (p)
- return p;
- return NULL;
- }
- static void deallocate(void *p)
- {
- if (p)
- free(p);
- }
- };
- //简单封装配置器
- template<class T, class Alloc=alloc>
- class simple_Alloc
- {
- public:
- static T* allocate(size_t n)
- {
- return (T*) Alloc::allocate(n * sizeof(T) );
- }
- static void deallocate(void *p)
- {
- Alloc::deallocate(p);
- }
- };
- template<class T, class Alloc=alloc>
- class MyArray
- {
- public:
- //内嵌型别的定义,容器都规范有如下定义,有些比这要多
- typedef T value_type;
- typedef value_type* iterator;
- typedef value_type& reference;
- typedef size_t size_type;
- typedef int defference_type; //迭代器步长
- typedef simple_Alloc<value_type, Alloc> data_Allocate; //空间配置器
- iterator start;
- iterator finish;
- size_t count;
- iterator begin() { return start; }
- iterator end() { return finish; }
- bool capacity() { return (finish-start) != count; }
- void push_back(const T& x) { if (capacity() ) *(finish++) = x; }
- void pop_back() { finish--; }
- value_type front() { return *start; }
- value_type back() { return *(finish-1); }
- value_type operator[](const size_t &i) { return start[i]; }
- MyArray(const size_type& n): count(n) { finish = start = data_Allocate::allocate(n); memset(start, 0, n); }
- MyArray(const size_type& n, const value_type& x):count(n) { finish = start = data_Allocate::allocate(n); memset(start, 0, n); }
- ~MyArray() { data_Allocate::deallocate(start); start = finish = NULL; }
- };
- int main()
- {
- MyArray<int> a(10);
- for (int i=0; i < 10; i++)
- a.push_back(i);
- copy(a.begin(), a.end(), ostream_iterator<int>(cout, " ") );
- getchar();
- return 0;
- }
#include <iostream>
#include <algorithm>
using namespace std;
//简单的空间配置器
//template<int i>
class alloc
{
public:
static void* allocate(size_t n)
{
void *p = malloc(n);
if (p)
return p;
return NULL;
}
static void deallocate(void *p)
{
if (p)
free(p);
}
};
//简单封装配置器
template<class T, class Alloc=alloc>
class simple_Alloc
{
public:
static T* allocate(size_t n)
{
return (T*) Alloc::allocate(n * sizeof(T) );
}
static void deallocate(void *p)
{
Alloc::deallocate(p);
}
};
template<class T, class Alloc=alloc>
class MyArray
{
public:
//内嵌型别的定义,容器都规范有如下定义,有些比这要多
typedef T value_type;
typedef value_type* iterator;
typedef value_type& reference;
typedef size_t size_type;
typedef int defference_type; //迭代器步长
typedef simple_Alloc<value_type, Alloc> data_Allocate; //空间配置器
iterator start;
iterator finish;
size_t count;
iterator begin() { return start; }
iterator end() { return finish; }
bool capacity() { return (finish-start) != count; }
void push_back(const T& x) { if (capacity() ) *(finish++) = x; }
void pop_back() { finish--; }
value_type front() { return *start; }
value_type back() { return *(finish-1); }
value_type operator[](const size_t &i) { return start[i]; }
MyArray(const size_type& n): count(n) { finish = start = data_Allocate::allocate(n); memset(start, 0, n); }
MyArray(const size_type& n, const value_type& x):count(n) { finish = start = data_Allocate::allocate(n); memset(start, 0, n); }
~MyArray() { data_Allocate::deallocate(start); start = finish = NULL; }
};
int main()
{
MyArray<int> a(10);
for (int i=0; i < 10; i++)
a.push_back(i);
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " ") );
getchar();
return 0;
}
可以看见一个自行编写的简陋容器就形成了,而且成功在STL算法中使用
用侯老师的话说:
源码之前,了无秘密!