自定义Vector实现:

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // FileName : c2_vector.h
  4. // Author : Jimmy Han
  5. // Date : N.A v1
  6. // : 2014/07/13 09:30 v2
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #include <cassert>
  10.  
  11. template <class object>
  12. class Vector {
  13. public:
  14. explicit Vector(int initsize = )
  15. : _size(initsize), _capacity(initsize + SPARE_CAPACITY)
  16. {_object = new object[_capacity];}
  17.  
  18. Vector(const Vector &rhs) : _object(NULL)
  19. { operator=(rhs);}
  20.  
  21. ~Vector()
  22. {delete [] _object;}
  23.  
  24. const Vector& operator=(const Vector &rhs)
  25. {
  26. if(this != &rhs)
  27. {
  28. delete [] _object;
  29. _size = rhs._size;
  30. _capacity = rhs._capacity;
  31.  
  32. _object = new object[_capacity];
  33. for(int k = ; k < _size; k++)
  34. _object[k] = rhs._object[k];
  35. }
  36. return *this;
  37. }
  38.  
  39. void resize(int newSize)
  40. {
  41. if(newSize > _capacity)
  42. reserve(newSize * + );
  43. _size = newSize;
  44. }
  45.  
  46. void reserve(int newCapacity)
  47. {
  48. if(newCapacity < _size)
  49. return;
  50.  
  51. object *oldobject = _object;
  52. _object = new object[newCapacity];
  53. for (int n=; n<_size; n++)
  54. _object[n] = oldobject[n];
  55. delete [] oldobject;
  56. _capacity = newCapacity;
  57. }
  58.  
  59. object& operator[](const int index)
  60. {
  61. assert(index >= && index < _size);
  62. return _object[index];
  63. }
  64.  
  65. const object& operator[](const int index) const
  66. {
  67. assert(index >= && index < _size);
  68. return _object[index];
  69. }
  70.  
  71. bool empty() const
  72. {return _size == ;}
  73.  
  74. int size() const
  75. {return _size;}
  76.  
  77. int capacity() const
  78. {return _capacity;}
  79.  
  80. void push_back(const object &x)
  81. {
  82. if(_size == _capacity)
  83. reserve(*_capacity + );
  84. _object[_size++] = x;
  85. }
  86.  
  87. void pop_back()
  88. {_size--;}
  89.  
  90. const object& back() const
  91. { return _object[_size - ];}
  92.  
  93. typedef object* iterator ;
  94. typedef const object* const_iterator;
  95.  
  96. iterator begin()
  97. {return &_object[];}
  98.  
  99. iterator end()
  100. {return &_object[_size];}
  101.  
  102. const_iterator begin() const
  103. {return &_object[];}
  104.  
  105. const_iterator end() const
  106. {return &_object[_size];}
  107.  
  108. enum{ SPARE_CAPACITY = };
  109. private:
  110. int _size;
  111. int _capacity;
  112. object *_object;
  113. };

自定义Vector测试程序:

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // FileName : c2_vector.cpp
  4. // Author : Jimmy Han
  5. // Date : N.A v1
  6. // : 2014/07/13 09:30 v2
  7. // $ ./a.exe
  8. // Calculate the size of self made Vector class
  9. // sizeof(vint) is: 12
  10.  
  11. // Self made Vector size test:
  12. // size of vint is: 5
  13. // capacity of vint is: 7
  14. // size of vint2 is: 8
  15. // capacity of vint2 is: 17
  16. // the last element of vint is: 4
  17. // the 5 element of vint is: 4
  18.  
  19. // Self made iterator test:
  20. // 0 1 2 3
  21.  
  22. // Iterator called by others:
  23. // [0 1 2 3]
  24. // [0 1 2 3 4 1919249516 1547322171 1735357008]
  25. //
  26. ///////////////////////////////////////////////////////////////////////////////
  27. #include <iostream>
  28. #include "printCollection.h"
  29. #include "c2_vector.h"
  30. using namespace std;
  31. int main()
  32. {
  33. //default constructor
  34. Vector<int> vint;
  35. Vector<int> vint2;
  36.  
  37. //calc class size, two int, one pointer, so size is 12
  38. cout << "Calculate the size of self made Vector class " << endl;
  39. cout <<"sizeof(vint) is: " << sizeof(vint) << endl << endl;
  40.  
  41. //void push_back(const object &x)
  42. for (int n=0; n<5; n++)
  43. vint.push_back(n);
  44.  
  45. //const Vector& operator=(const Vector &rhs)
  46. vint2 = vint;
  47.  
  48. //void resize( int newSize)
  49. vint2.resize(8);
  50.  
  51. //int size(); int capacity();
  52. cout << "Self made Vector size test: " << endl;
  53. cout << "size of vint is: " << vint.size() << endl;
  54. cout << "capacity of vint is: " << vint.capacity() << endl;
  55. cout << "size of vint2 is: " << vint2.size() << endl;
  56. cout << "capacity of vint2 is: " << vint2.capacity() << endl;
  57.  
  58. //const object& back();
  59. cout << "the last element of vint is: " << vint.back() << endl;
  60.  
  61. //object& operator[](const int index)
  62. cout << "the 5 element of vint is: " << vint[4] << endl << endl;
  63.  
  64. //void pop_back()
  65. vint.pop_back();
  66.  
  67. //typedef object* iterator; typedef const ojbect* const_iterator
  68. //iterator begin(); iterator end();
  69. cout << "Self made iterator test: " << endl;
  70. for (Vector<int>::iterator iter = vint.begin(); iter != vint.end(); iter++)
  71. {
  72. cout << *iter << " ";
  73. }
  74. cout << endl << endl;
  75.  
  76. cout << "Iterator called by others: " << endl;
  77. printCollection(vint);
  78. //after resize, not initilized member got random number
  79. printCollection(vint2);
  80. }

数据结构-Vector的更多相关文章

  1. 浅谈数据结构vector

    vector: 又名 向量 1.C++中的一种数据结构. 2.是一个类. 3.相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. A.使用时, ...

  2. java数据结构-Vector

    1 Vector基础实现为数组 object[] synchronized线程安全 2 扩容使用  System.arraycopy(original, 0, copy, 0,Math.min(ori ...

  3. Java 集合系列06之 Vector详细介绍(源码解析)和使用示例

    概要 学完ArrayList和LinkedList之后,我们接着学习Vector.学习方式还是和之前一样,先对Vector有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.第1部分 Vec ...

  4. Java 集合系列 05 Vector详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  5. C++ vector和list的区别

    1.vector数据结构vector和数组类似,拥有一段连续的内存空间,并且起始地址不变.因此能高效的进行随机存取,时间复杂度为o(1);但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内存 ...

  6. 转载:C++ vector 类学习笔记

    声明:本文转载自http://blog.csdn.net/whz_zb/article/details/6827999 vector简介 vector是STL中最常见的容器,它是一种顺序容器,支持随机 ...

  7. 10、Cocos2dx 3.0游戏开发找小三之容器篇:Vector、Map、Value

    重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27705613 容器 3.0版本号之前Cocos2d- ...

  8. JAVA核心技术I---JAVA基础知识(数据结构基础)

    一:数组 (一)基本内容是与C一致的 (二)数组定义和初始化 (1)声明 int a[]; //a没有new操作,没有被分配内存,为null int[] b; //b没有new操作,没有被分配内存,为 ...

  9. java之vector详细介绍

    1 vector介绍 Vector简介 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口 ...

随机推荐

  1. 定义函数def

  2. DOM加载:浏览器渲染和操作顺序(转载 学习中。。。)

    DOM加载:浏览器渲染和操作顺序 1.HTML解析完毕 2.外部脚本和样式表加载完毕 3.脚本在文档内解析并执行 4.HTML DOM完全构造起来 5.图片和外部内容加载 6.网页完成加载 基于这个顺 ...

  3. Android GestureDetector方法详解

    为了加强点击.拖动响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single ...

  4. 转:Singleton模式

    C++完美实现Singleton模式  转自:http://www.cppblog.com/dyj057/archive/2005/09/20/346.html boost库的Singleton的实现 ...

  5. mybatis实战

    这篇教程不错,推荐:http://blog.csdn.net/techbirds_bao/article/details/9233599/

  6. idea项目部署

    idea新建项目: http://blog.csdn.net/wo541075754/article/details/46348135  详细 http://www.cnblogs.com/wql02 ...

  7. 深入理解JVM虚拟机-2垃圾收集器

    这里讨论的收集器基于JDK 1.7 Update 14之后的HotSpot虚拟机. 如果两个收集器之间存在连线,说明可以搭配使用.虚拟机所处的区域,则表示它是属于新生代收集器还是年老代收集器.在这里我 ...

  8. 项目解析- JspLibrary - part1

    http://rosspc:8080/JspLibrary/ 1. logon界面解析: JS 验证用户名.密码为空 <form name="form1" method=&q ...

  9. 在IIS7.5打开网页的时候,提示: HTTP 错误 500.0 - Internal Server Error 调用 LoadLibraryEx 失败,在 ISAPI 筛选器 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll" 上。解决方法

  10. 委托(C# 编程指南)

    原文地址:https://msdn.microsoft.com/zh-cn/library/ms173171.aspx delegate 是表示对具有特定参数列表和返回类型的方法的引用的类型. 在实例 ...