STL源码剖析总结

侯老师说泛型技术学习分三层境界,最高境界自然就是自行扩展STL了,要自行扩展不单要清楚STL的实现及原理还要对STL有深刻理解,以达到能够编写无缝融入STL的自行扩展的组件,嘛,就是写库吧

下面就贴上一个自己写的简单的数组容器,当然迭代器为原始指针所以就不需要专门编写迭代器,STL也提供了原始指针的特化版本萃取机,所以并不需要为迭代器编写额外代码(要不然就麻烦了),为了更好理解STL空间配置器也自己写了个简单的并作了和STL差不多的封装(仅限于SGI STL)

  1. #include <iostream>   
  2. #include <algorithm>   
  3.   
  4. using namespace std;  
  5.   
  6. //简单的空间配置器   
  7. //template<int i>   
  8. class alloc  
  9. {  
  10. public:  
  11.     static void* allocate(size_t n)  
  12.     {  
  13.         void *p = malloc(n);  
  14.         if (p)  
  15.             return p;  
  16.         return NULL;  
  17.     }  
  18.   
  19.     static void deallocate(void *p)  
  20.     {  
  21.         if (p)  
  22.             free(p);  
  23.     }  
  24. };  
  25.   
  26. //简单封装配置器   
  27. template<class T, class Alloc=alloc>  
  28. class simple_Alloc  
  29. {  
  30. public:  
  31.     static T* allocate(size_t n)  
  32.     {  
  33.         return (T*) Alloc::allocate(n * sizeof(T) );  
  34.     }  
  35.   
  36.     static void deallocate(void *p)  
  37.     {  
  38.         Alloc::deallocate(p);  
  39.     }  
  40. };  
  41.   
  42. template<class T, class Alloc=alloc>  
  43. class MyArray  
  44. {  
  45. public:  
  46.     //内嵌型别的定义,容器都规范有如下定义,有些比这要多   
  47.     typedef T           value_type;  
  48.     typedef value_type* iterator;  
  49.     typedef value_type& reference;  
  50.     typedef size_t      size_type;  
  51.     typedef int         defference_type; //迭代器步长   
  52.   
  53.     typedef simple_Alloc<value_type, Alloc> data_Allocate; //空间配置器   
  54.     iterator    start;  
  55.     iterator    finish;  
  56.     size_t      count;  
  57.     iterator    begin() { return start;  }  
  58.     iterator    end()   { return finish; }  
  59.     bool        capacity() { return (finish-start) != count; }  
  60.     void        push_back(const T& x) { if (capacity() ) *(finish++) = x; }  
  61.     void        pop_back()  { finish--; }  
  62.     value_type  front() { return *start; }  
  63.     value_type  back()  { return *(finish-1); }  
  64.     value_type  operator[](const size_t &i) { return start[i]; }  
  65.     MyArray(const size_type& n): count(n)  { finish = start = data_Allocate::allocate(n); memset(start, 0, n); }  
  66.     MyArray(const size_type& n, const value_type& x):count(n) { finish = start = data_Allocate::allocate(n); memset(start, 0, n); }  
  67.     ~MyArray() { data_Allocate::deallocate(start); start = finish = NULL; }  
  68. };  
  69.   
  70.   
  71.   
  72. int main()  
  73. {  
  74.     MyArray<int> a(10);  
  75.     for (int i=0; i < 10; i++)  
  76.         a.push_back(i);  
  77.     copy(a.begin(), a.end(), ostream_iterator<int>(cout, " ") );  
  78.     getchar();  
  79.     return 0;  
  80. }  
#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算法中使用

用侯老师的话说:

源码之前,了无秘密!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值