首先是vector的定义 template <typename T> class vector { };让我们先来看看vector中的一些别名 public: typedef T value_type; typedef T* pointer; typedef T& reference; typedef const T& const_reference; typedef s... Read More
posted @ 2012-07-15 20:25 lwch Views(628) Comments(0) Diggs(0) Edit
traits技术被广泛应用于STL中,通过它您可以轻松的萃取出一个对象的特性。在STL中也是通过它来实现性能的最优化,比如一个对象是个POD对象(Plain Old Data),则在拷贝过程中直接可以通过memcpy等函数拷贝,而无需调用拷贝构造函数或operator=。先来看看STL中最基本的对象iterator template <typename T, typename Size = size_t, typename Difference = ptrdiff_t> struct iterator { typedef T ... Read More
posted @ 2012-07-15 20:15 lwch Views(760) Comments(0) Diggs(0) Edit
内存池的作用:减少内存碎片,提高性能。首先不得不提的是Win32和x64中对于指针的长度是不同的,在Win32中一个指针占4字节,而在x64中一个指针占8字节。也正是不清楚这一点,当我在x64中将指针作为4字节修改造成其他数据异常。首先我们先来定义三个宏 #define ALIGN sizeof(void*) #define MAX_BYTES 128 #define MAX_COUNT (MAX_BYTES / ALIGN)正如前面所说的,为了兼容Win32与x64应此我们将要申请的内存按void*的大小来对齐。正如前... Read More
posted @ 2012-07-15 20:09 lwch Views(1821) Comments(0) Diggs(0) Edit
作为一个山寨的STL,那么不得不提的是其中的allocator(空间配置器)。顾名思义,它是负责空间分配用的,下面代码十分简单,仅对C函数malloc和free进行了薄薄的一层封装,同时给定了自定义函数free_handler用于在空间分配时候由于内存被占满了而导致的分配失败。这里值得注意的是:这个释放函数的函数指针应该是由调用方来负责指定,并且它仅有一个参数表明至少需要释放多少字节的内存。下面来看代码,代码非常简单,应此这里就不逐一展开说明了。 template <typename T> class allocator { public... Read More
posted @ 2012-07-15 19:35 lwch Views(648) Comments(0) Diggs(0) Edit
数据结构allocator内存池内存池V2traits,construct&destructvectorlistrbtreemap&sethashtablehash_map&hash_set算法 Read More
posted @ 2012-07-15 19:34 lwch Views(475) Comments(0) Diggs(0) Edit