数据结构-Vector
自定义Vector实现:
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FileName : c2_vector.h
- // Author : Jimmy Han
- // Date : N.A v1
- // : 2014/07/13 09:30 v2
- //
- ///////////////////////////////////////////////////////////////////////////////
- #include <cassert>
- template <class object>
- class Vector {
- public:
- explicit Vector(int initsize = )
- : _size(initsize), _capacity(initsize + SPARE_CAPACITY)
- {_object = new object[_capacity];}
- Vector(const Vector &rhs) : _object(NULL)
- { operator=(rhs);}
- ~Vector()
- {delete [] _object;}
- const Vector& operator=(const Vector &rhs)
- {
- if(this != &rhs)
- {
- delete [] _object;
- _size = rhs._size;
- _capacity = rhs._capacity;
- _object = new object[_capacity];
- for(int k = ; k < _size; k++)
- _object[k] = rhs._object[k];
- }
- return *this;
- }
- void resize(int newSize)
- {
- if(newSize > _capacity)
- reserve(newSize * + );
- _size = newSize;
- }
- void reserve(int newCapacity)
- {
- if(newCapacity < _size)
- return;
- object *oldobject = _object;
- _object = new object[newCapacity];
- for (int n=; n<_size; n++)
- _object[n] = oldobject[n];
- delete [] oldobject;
- _capacity = newCapacity;
- }
- object& operator[](const int index)
- {
- assert(index >= && index < _size);
- return _object[index];
- }
- const object& operator[](const int index) const
- {
- assert(index >= && index < _size);
- return _object[index];
- }
- bool empty() const
- {return _size == ;}
- int size() const
- {return _size;}
- int capacity() const
- {return _capacity;}
- void push_back(const object &x)
- {
- if(_size == _capacity)
- reserve(*_capacity + );
- _object[_size++] = x;
- }
- void pop_back()
- {_size--;}
- const object& back() const
- { return _object[_size - ];}
- typedef object* iterator ;
- typedef const object* const_iterator;
- iterator begin()
- {return &_object[];}
- iterator end()
- {return &_object[_size];}
- const_iterator begin() const
- {return &_object[];}
- const_iterator end() const
- {return &_object[_size];}
- enum{ SPARE_CAPACITY = };
- private:
- int _size;
- int _capacity;
- object *_object;
- };
自定义Vector测试程序:
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FileName : c2_vector.cpp
- // Author : Jimmy Han
- // Date : N.A v1
- // : 2014/07/13 09:30 v2
- // $ ./a.exe
- // Calculate the size of self made Vector class
- // sizeof(vint) is: 12
- // Self made Vector size test:
- // size of vint is: 5
- // capacity of vint is: 7
- // size of vint2 is: 8
- // capacity of vint2 is: 17
- // the last element of vint is: 4
- // the 5 element of vint is: 4
- // Self made iterator test:
- // 0 1 2 3
- // Iterator called by others:
- // [0 1 2 3]
- // [0 1 2 3 4 1919249516 1547322171 1735357008]
- //
- ///////////////////////////////////////////////////////////////////////////////
- #include <iostream>
- #include "printCollection.h"
- #include "c2_vector.h"
- using namespace std;
- int main()
- {
- //default constructor
- Vector<int> vint;
- Vector<int> vint2;
- //calc class size, two int, one pointer, so size is 12
- cout << "Calculate the size of self made Vector class " << endl;
- cout <<"sizeof(vint) is: " << sizeof(vint) << endl << endl;
- //void push_back(const object &x)
- for (int n=0; n<5; n++)
- vint.push_back(n);
- //const Vector& operator=(const Vector &rhs)
- vint2 = vint;
- //void resize( int newSize)
- vint2.resize(8);
- //int size(); int capacity();
- cout << "Self made Vector size test: " << endl;
- cout << "size of vint is: " << vint.size() << endl;
- cout << "capacity of vint is: " << vint.capacity() << endl;
- cout << "size of vint2 is: " << vint2.size() << endl;
- cout << "capacity of vint2 is: " << vint2.capacity() << endl;
- //const object& back();
- cout << "the last element of vint is: " << vint.back() << endl;
- //object& operator[](const int index)
- cout << "the 5 element of vint is: " << vint[4] << endl << endl;
- //void pop_back()
- vint.pop_back();
- //typedef object* iterator; typedef const ojbect* const_iterator
- //iterator begin(); iterator end();
- cout << "Self made iterator test: " << endl;
- for (Vector<int>::iterator iter = vint.begin(); iter != vint.end(); iter++)
- {
- cout << *iter << " ";
- }
- cout << endl << endl;
- cout << "Iterator called by others: " << endl;
- printCollection(vint);
- //after resize, not initilized member got random number
- printCollection(vint2);
- }
数据结构-Vector的更多相关文章
- 浅谈数据结构vector
vector: 又名 向量 1.C++中的一种数据结构. 2.是一个类. 3.相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. A.使用时, ...
- java数据结构-Vector
1 Vector基础实现为数组 object[] synchronized线程安全 2 扩容使用 System.arraycopy(original, 0, copy, 0,Math.min(ori ...
- Java 集合系列06之 Vector详细介绍(源码解析)和使用示例
概要 学完ArrayList和LinkedList之后,我们接着学习Vector.学习方式还是和之前一样,先对Vector有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.第1部分 Vec ...
- Java 集合系列 05 Vector详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- C++ vector和list的区别
1.vector数据结构vector和数组类似,拥有一段连续的内存空间,并且起始地址不变.因此能高效的进行随机存取,时间复杂度为o(1);但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内存 ...
- 转载:C++ vector 类学习笔记
声明:本文转载自http://blog.csdn.net/whz_zb/article/details/6827999 vector简介 vector是STL中最常见的容器,它是一种顺序容器,支持随机 ...
- 10、Cocos2dx 3.0游戏开发找小三之容器篇:Vector、Map、Value
重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27705613 容器 3.0版本号之前Cocos2d- ...
- JAVA核心技术I---JAVA基础知识(数据结构基础)
一:数组 (一)基本内容是与C一致的 (二)数组定义和初始化 (1)声明 int a[]; //a没有new操作,没有被分配内存,为null int[] b; //b没有new操作,没有被分配内存,为 ...
- java之vector详细介绍
1 vector介绍 Vector简介 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口 ...
随机推荐
- 定义函数def
- DOM加载:浏览器渲染和操作顺序(转载 学习中。。。)
DOM加载:浏览器渲染和操作顺序 1.HTML解析完毕 2.外部脚本和样式表加载完毕 3.脚本在文档内解析并执行 4.HTML DOM完全构造起来 5.图片和外部内容加载 6.网页完成加载 基于这个顺 ...
- Android GestureDetector方法详解
为了加强点击.拖动响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single ...
- 转:Singleton模式
C++完美实现Singleton模式 转自:http://www.cppblog.com/dyj057/archive/2005/09/20/346.html boost库的Singleton的实现 ...
- mybatis实战
这篇教程不错,推荐:http://blog.csdn.net/techbirds_bao/article/details/9233599/
- idea项目部署
idea新建项目: http://blog.csdn.net/wo541075754/article/details/46348135 详细 http://www.cnblogs.com/wql02 ...
- 深入理解JVM虚拟机-2垃圾收集器
这里讨论的收集器基于JDK 1.7 Update 14之后的HotSpot虚拟机. 如果两个收集器之间存在连线,说明可以搭配使用.虚拟机所处的区域,则表示它是属于新生代收集器还是年老代收集器.在这里我 ...
- 项目解析- JspLibrary - part1
http://rosspc:8080/JspLibrary/ 1. logon界面解析: JS 验证用户名.密码为空 <form name="form1" method=&q ...
- 在IIS7.5打开网页的时候,提示: HTTP 错误 500.0 - Internal Server Error 调用 LoadLibraryEx 失败,在 ISAPI 筛选器 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\\aspnet_filter.dll" 上。解决方法
- 委托(C# 编程指南)
原文地址:https://msdn.microsoft.com/zh-cn/library/ms173171.aspx delegate 是表示对具有特定参数列表和返回类型的方法的引用的类型. 在实例 ...