本文参考了侯捷的 《STL 源码分析》一书,出于兴趣,自行实现了简单的 vector 容器。

之后会陆续上传 list, deque 等容器的代码,若有错误,欢迎留言指出。

vector 容易实现的几点注意事项:

1. 由于vector 是动态数组。

出于效率的考虑,在往vector 中加入元素时,内存的扩展遵循的规则是:

1> 如果当前可用内存不够,开 2倍大的内存,将原来的数组复制到新数组中,撤销原来的数组。

2> 加入新的元素

2. 通常当我们 int *p = new int(1)时, new 其实做了两件事情: 1> 分配内存   2>在分配的内存中调用类的构造函数。

出于效率的考虑,在 vector 的内存管理中,我们将这两个操作分开。

C++ 提供了 模板类 allocator 来实现上述的功能。

3. vector 中三个指针,分别是 start, finish, end_of_storage;

[start, finish) 就是数组元素;

[finish, end_of_storage) 是预先分配的空间(还没有调用构造函数)

参考书籍:

1. C++ primer, Lippman

2. STL 源码分析, 侯捷

// Last Update:2014-04-11 15:44:44
/**
* @file vector.h
* @brief a simple vector class
* @author shoulinjun@126.com
* @version 0.1.00
* @date 2014-04-09
*/ #ifndef MY_VECTOR_H
#define MY_VECTOR_H #include <iostream>
#include <algorithm>
#include <memory> template<class T>
void destroy(T* pointer)
{
pointer->~T();
} template<class ForwardIterator>
void destroy(ForwardIterator first, ForwardIterator last)
{
for(ForwardIterator it = first; it != last; ++ it)
{
destroy(&*it);
}
} template<class T>
class MyVector
{
public:
typedef T value_type;
typedef T* iterator;
typedef const T*const_iterator;
typedef T* pointer;
typedef const T* const_pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type; MyVector();
MyVector(size_type n, T value = T());
MyVector(iterator begin, iterator end);
~MyVector(); //copy control
MyVector(const MyVector&);
MyVector& operator=(const MyVector&); bool empty() const { return begin() == end(); }
size_type size() const {return (size_type)(finish - start);}
size_type capacity() const {return (size_type)(end_of_storage - start);} iterator begin() { return start; }
const_iterator begin() const{ return start; }
iterator end() { return finish;}
const_iterator end() const{ return finish; } reference operator[](size_type i){return *(start + i);}
const_reference operator[](size_type i)const {return *(start + i);} void insert(iterator position, size_type n, const T& value);
void push_back(const T& value);
void pop_back(); void erase(iterator first, iterator last);
void clear(); void reserve(size_type n);
protected:
iterator start; //空间的头
iterator finish; //空间的尾
iterator end_of_storage; //可用空间的尾巴
private:
static std::allocator<T> alloc; // object to get raw memory
}; // static class member needed to be defined outside of class
template<class T>
std::allocator<T> MyVector<T>::alloc; // default constructor
template<class T>
MyVector<T>::MyVector()
: start(NULL), finish(NULL), end_of_storage(NULL)
{
} template<class T>
MyVector<T>::MyVector(size_type n, T value)
{
start = alloc.allocate(n);
end_of_storage = finish = start + n; for(iterator i=start; i!=finish; ++i)
alloc.construct(i, value);
} template<class T>
MyVector<T>::MyVector(iterator begin, iterator end)
{
const size_type n = end - begin;
/* allocate space */
start = alloc.allocate(n);
finish = end_of_storage = start + n; /* call constructor */
std::uninitialized_copy(begin, end, start);
} template<class T>
MyVector<T>::~MyVector()
{
/* call destructor */
::destroy(start, finish); /* free space */
alloc.deallocate(start, end_of_storage - start);
} // copy control
template<class T>
MyVector<T>::MyVector(const MyVector& rhs)
{
start = alloc.allocate(rhs.capacity());
std::uninitialized_copy(rhs.start, rhs.finish, start);
finish = start + (rhs.finish - rhs.start);
end_of_storage = start + (rhs.end_of_storage - rhs.start);
} template<class T>
MyVector<T>& MyVector<T>::operator=(const MyVector& rhs)
{
start = alloc.allocate(rhs.capacity());
std::uninitialized_copy(rhs.start, rhs.finish, start);
finish = start + rhs.finish - rhs.start;
end_of_storage = start + rhs.end_of_storage - rhs.start; return *this;
} template<class T>
void MyVector<T>::insert(iterator position, size_type n, const T& value)
{
if(n <= end_of_storage - finish)
{/* enough memory */
if(n <= finish - position)
{
std::uninitialized_copy(finish-n, finish, finish);
std::copy(position, finish-n, position+n);
std::fill_n(position, n, value);
}
else
{
std::uninitialized_fill_n(finish, n - (finish - position), value);
std::uninitialized_copy(position, finish, position + n);
std::fill(position, finish, value);
}
finish += n;
}
else
{/* reallocate */
pointer new_start(NULL), new_finish(NULL);
size_type old_type = end_of_storage - start;
size_type new_size = old_type + std::max(old_type, n);
new_start = alloc.allocate(new_size); // copy old vector to new vector
new_finish = std::uninitialized_copy(start, position, new_start);
std::uninitialized_fill_n(new_finish, n, value);
new_finish += n;
new_finish = std::uninitialized_copy(position, finish, new_finish); alloc.deallocate(start, end_of_storage - start); start = new_start;
finish = new_finish;
end_of_storage = new_start + new_size;
}
} template<class T>
void MyVector<T>::push_back(const T &value)
{
insert(end(), 1, value);
} template<class T>
void MyVector<T>::pop_back()
{
alloc.destroy(--finish);
} template<class T>
void MyVector<T>::erase(iterator first, iterator last)
{
iterator old_finish = finish;
finish = std::copy(last, finish, first);
::destroy(finish, old_finish);
} template<class T>
void MyVector<T>::clear()
{
erase(start, finish);
} template<class T>
void MyVector<T>::reserve(size_type n)
{
if(capacity() < n)
{
iterator new_start = alloc.allocate(n);
std::uninitialized_copy(start, finish, new_start); ::destroy(start, finish);
alloc.deallocate(start, size()); const size_type old_size = finish - start;
start = new_start;
finish = new_start + old_size;
end_of_storage = new_start + n;
}
} #endif /*MY_VECTOR_H*/

动手实现自己的 STL 容器 《1》---- vector的更多相关文章

  1. STL容器之一vector

    STL中最简单也是最有用的容器之一是vector<T>类模板,称为向量容器,是序列类型容器中的一种. 1.vector<T> 对象的基本用法(1)声明:vector<ty ...

  2. STL 容器(vector 和 list )

    1.这个容器的知识点比较杂 迭代器的理解: 1.erase()函数的返回值,它的迭代器在循环遍历中的奇特之处: #define _CRT_SECURE_NO_WARNINGS #include < ...

  3. 动手实现自己的 STL 容器《2》---- list

    1. 序: 本文参考了侯捷的 <STL 源码分析>一书,出于兴趣,自行实现了简单的 list 容器. 学习了 STL 的 list 容器的源代码,确实能够提高写链表代码的能力.其中的 so ...

  4. STL—— 容器(vector)元素的删除

    1. clear() 将整个 vector 都删除 使用 vectorname.clear() 可以将整个vector 中的元素全部删除,但是内存不会释放,如下代码: 1 #include <i ...

  5. STL—— 容器(vector)数据插入insert()方法 的返回值

    vector 容器下的 insert() 方法拥有返回值,由于insert() 方法拥有4种重载函数,他的返回值不尽相同. 第一种,插入单个元素后的返回值: 1 #include <iostre ...

  6. STL—— 容器(vector)的数据插入之 insert()

    vector 容器可以使用 vectorName.insert() 方法插入元素,vectorName.insert() 函数一共有4种重载方法: 第一种 insert() 用法:在指定地址插入单个元 ...

  7. STL—— 容器(vector)的各种功能方法

    1. 获取容器的元素个数 size() 使用 vectorName.size() 可以输出这个容器中类型的个数,如下代码: 1 #include <iostream> 2 #include ...

  8. STL—— 容器(vector)的数据写入、修改和删除

    1. 通过 push_back() 尾部增加一个元素 : vector 可以通过 "push_back " 写入数据,通过 push_back 可以将数据直接写入至 vector ...

  9. STL—— 容器(vector)的内存分配,声明时的普通构造&带参构造

    vector 的几种带参构造 & 初始化与内存分配: 1. 普通的带参构造: vector 的相关对象可以在声明时通过 vector 的带参构造函数进行内存分配,如下: 1 #include ...

随机推荐

  1. 20160622001 GridView 删除列 用模板实现删除时提示确认框

    GridView在使用CommandField删除时弹出提示框,在.net2005提供的GridView中我们可以直接添加一个 CommandField删除列:<asp:CommandField ...

  2. [Linux] VIM 常用快捷键2

    如何使用MacVim 1.在插入模式之外 基本上来说,你应该尽可能少的呆在插入模式里面,因为在插入模式里面 VIM 就像一个“哑巴”编辑器一样.很多新手都会一直呆在插入模式里面,因为这样易于使用.但 ...

  3. 记一次动画的优化--requestAnimationFrame、webp

    需要写一个类似帧动画的东西,但是每一帧是一张全屏的图,而且量特别大,600都张,而且存在跳帧的问题,只有把速度调的很快还可以看着不跳帧.但是只用谷歌还真正常播放. 其实优化起来两个方面.一个是用req ...

  4. iOS,Xcode7 制作Framework,含资源和界面

    Xcode7 制作Framework  本文通过Demo方式介绍1)将含bundle和存代码编写界面打包进framework:2)将storyboard +assets.xcassets打包. (一) ...

  5. ArrayList代码示例

    package com.shushine.framework.第七章Java标准类库;import java.util.ArrayList;/** * * <p> * 描述该类情况 {@l ...

  6. 【leetcode❤python】 28. Implement strStr()

    #-*- coding: UTF-8 -*- #题意:大海捞刀,在长字符串中找出短字符串#AC源码:滑动窗口双指针的方法class Solution(object):    def strStr(se ...

  7. python2 到 python3 转换工具 2to3

    windows系统下的使用方法: (1)将python安装包下的Tools/Scripts下面的2to3.py拷贝到需要转换文件目录中. (2)dos切换到需要转换的文件目录下,运行命令2to3.py ...

  8. monkey中的一些问题

    一起来看下导致App Crash的那些原因: 1.     空指针异常:错误日志定位java.lang.NullPointerException,详细日志记录如下 2.     安全异常:错误日志定位 ...

  9. GitHub下载的 Git Shell中出现的字符支持警告,将字体修改为新宋体即可

    ——解决办法: 将命令行字体修改为新宋体就可以了. 文章引用:http://www.cnblogs.com/veryinf/archive/2012/10/26/2740899.html

  10. 新建web工程Jdk怎么不是自己安装的, 是自带的

    需要在eclipse中配置默认的jdk环境的,不要用它默认的那个,这个不能用的http://blog.csdn.net/clj198606061111/article/details/11881575 ...