#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::allocator; template <typename T>
class Vector
{
public:
typedef T* iterator;
typedef const T * const_iterator; Vector()
: _start(0)
, _finish(0)
, _end_of_storage(0)
{} ~Vector(); iterator begin(){ return _start; }
iterator end(){ return _finish; } void push_back(const T & t);
void pop_back(); size_t size() const
{ return _finish - _start; } size_t capacity() const
{ return _end_of_storage - _start; }
private:
void reallocate(); private:
static allocator<T> _alloc; T * _start;
T * _finish;
T * _end_of_storage;
}; template <typename T>
allocator<T> Vector<T>::_alloc; template <typename T>
Vector<T>::~Vector()
{
//销毁对象
while(_start != _finish)
_alloc.destroy(--_finish);
//回收开辟的空间
if(_start)
_alloc.deallocate(_start, capacity());
} //动态数组的实现:
// 当当前元素的个数与能容纳元素的个数相等时,
//先开辟新的空间(原来的2倍),然后把原来空间的元素
//复制到新空间上,再回收原来的空间,最后在新开的空间上添加
//新的元素 template <typename T>
void Vector<T>::push_back(const T & t)
{
if(size() == capacity())
{
//动态扩容
reallocate();
} //构造对象
_alloc.construct(_finish++, t);
} template <typename T>
void Vector<T>::pop_back()
{
if(_start != _finish)
_alloc.destroy(--_finish);
} template <typename T>
void Vector<T>::reallocate()
{
size_t oldCapacity = capacity();
size_t newCapacity = oldCapacity ? 2 * oldCapacity : 1; T * tmp = _alloc.allocate(newCapacity);//申请是原始的内存
if(_start)
{
///memcpy();// 内置类型的数据
//copy();//调用对象的赋值运算符函数,意味着对象存在
std::uninitialized_copy(_start, _finish, tmp);//复制原来空间的数据 //回收原来空间的数据
while(_start != _finish)
_alloc.destroy(--_finish);
//回收原来空间
_alloc.deallocate(_start, oldCapacity);
}
_start = tmp;
_finish = _start + oldCapacity;
_end_of_storage = _start + newCapacity;
} class Point
{
public:
Point(int ix = 0, int iy = 0)
: _ix(ix)
, _iy(iy)
{ cout << "Point(int=0,int=0)" << endl;} Point(const Point & rhs)
: _ix(rhs._ix)
, _iy(rhs._iy)
{
cout << "Point(const Point &)" << endl;
} ~Point()
{ cout << "~Point()" << endl; } friend std::ostream & operator<<(std::ostream & os, const Point & rhs);
private:
int _ix;
int _iy;
}; std::ostream & operator<<(std::ostream & os, const Point & rhs)
{
os << "(" << rhs._ix
<< "," << rhs._iy
<< ")";
return os;
} template <typename Container>
void display(Container & c)
{
cout << "c's size = " << c.size() << endl
<< "c's capacity = " << c.capacity() << endl << endl;
#if 0
#endif
} int main(void)
{
Vector<Point> points;
display(points);
points.push_back(Point(1, 2));
display(points); points.push_back(Point(3, 4));
display(points);
points.push_back(Point(5, 6));
display(points);
#if 0
points.push_back(Point(1, 2));
display(points);
points.push_back(Point(1, 2));
display(points);
points.push_back(Point(1, 2));
display(points);
#endif Vector<Point>::iterator it = points.begin();
while(it != points.end())
{
cout << *it << endl;
++it;
}
cout << endl;
return 0;
}

  

自己实现的vector的更多相关文章

  1. c++ vector 使用

    1. 包含一个头文件: 1 #include <vector> 2. 申明及初始化: std::vector<int> first; // empty vector of in ...

  2. Vector Tile

    Mapbox Vector Tile Specification A specification for encoding tiled vector data. <?XML:NAMESPACE ...

  3. ArrayList、Vector、LinkedList的区别联系?

    1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...

  4. ArrayList、Vector、HashMap、HashSet的默认初始容量、加载因子、扩容增量

    当底层实现涉及到扩容时,容器或重新分配一段更大的连续内存(如果是离散分配则不需要重新分配,离散分配都是插入新元素时动态分配内存),要将容器原来的数据全部复制到新的内存上,这无疑使效率大大降低. 加载因 ...

  5. Java中Vector和ArrayList的区别

    首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...

  6. C++使用vector

    #include <iostream> #include <string> #include <vector> using namespace std; void ...

  7. [LeetCode] Flatten 2D Vector 压平二维向量

    Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...

  8. C++ 数组array与vector的比较

    转:http://blog.csdn.net/yukin_xue/article/details/7391897 1. array 定义的时候必须定义数组的元素个数;而vector 不需要: 且只能包 ...

  9. vector定义初始化

    头文件 #include<vector> using std::vector; vector<T> v1; vector<T> v2(v1); vector< ...

  10. vector迭代器用法

    #include<iostream> #include<vector> using namespace std; int main() { vector<int> ...

随机推荐

  1. MVC中的 @helper

    ASP.NET MVC 3支持一项名为“Razor”的新视图引擎选项(除了继续支持/加强现有的.aspx视图引擎外).当编写一个视图模板时,Razor将所需的字符和击键数减少到最小,并保证一个快速.通 ...

  2. FileInputStream 的读取操作

    package xinhuiji_day07; import java.io.File;import java.io.FileInputStream;import java.io.FileNotFou ...

  3. vim与sublime

    vim与sublime 对程序员来说,写代码是再熟悉不过的事情了,windows系统自带有记事本软件,能写写小规模的代码,可是代码量大了,它的局限性就暴露得很明显了:没有语法高亮,没有自动提示,不支持 ...

  4. 小程序框架MpVue踩坑日记(一)

    小程序也做了几个小功能模块了,总觉得需要总结一下,踩坑什么的还是得记录一下啊. 好吧,其实是为了方便回顾 首先,说到小程序框架,大家都知道wepy,不过,我是没用过 美团开发团队到mpvue到是个实在 ...

  5. javascript的defer和async(转载)

    http://ued.ctrip.com/blog/?p=3121 我们常用的javascript标签,有两个和性能.js文件下载执行相关的属性:defer和async defer的含义[摘自http ...

  6. 模式识别之svm()---支持向量机svm 简介1995

    转自:http://www.blogjava.net/zhenandaci/archive/2009/02/13/254519.html 作者:Jasper 出自:http://www.blogjav ...

  7. SQLServer中游标实例介绍(转)

    引言 我们先不讲游标的什么概念,步骤及语法,先来看一个例子: 表一 OriginSalary                      表二 AddSalary 现在有2张表,一张是OriginSal ...

  8. Notepad工具使用小技巧

    工欲善其事必先利其器 Notepad++是个很不错的文本编辑工具,掌握它的使用技巧可以提高我们工作的效率.见如下: 比较常用的罗列如下:(如果有更好的建议可以留言哈) 1: 添加书签 CTRL+F2 ...

  9. power law 幂定律

    y=cx∧a 卖品销量排名与销量

  10. Delphi String的散漫记录,真是知识无数,陷阱无数

    真是膜拜Delphi C++ Builder编译器的作者们,要下多少苦功夫才能解决如此之多的问题,制造出一个神级作品给世人享用.另外以我的编程经验所能想到很麻烦但却是必须的还有两个地方,一个是Form ...