复习(数据结构):动态数组:c++_stl写法
来源:互联网 发布:minidao sql 编辑:程序博客网 时间:2024/05/22 17:36
- 定义异常
//"dsexceptions.h"#ifndef DS_EXCEPTIONS_H#define DS_EXCEPTIONS_Hclass UnderflowException { };class IllegalArgumentException { };class ArrayIndexOutOfBoundsException { };class IteratorOutOfBoundsException { };class IteratorMismatchException { };class IteratorUninitializedException { };#endif
#ifndef _VECTOR_H#define _VECTOR_H#include "dsexceptions.h"template<typename Object>class vector{public: /* http://www.cnblogs.com/ymy124/p/3632634.html explicit修饰构造函数,防止发生隐形类型转换 */ //构造函数 explicit vector(int intSize=0) :theSize(initSize),theCapacity(intSize+SPACE_CAPACITY) {objects=new Object[theCapacity];} //拷贝构造函数 vector(const vector &rhs):objects(NULL) {operator=(rhs);} ~vector(); const vector& operator= (const vector& rhs){ if(this!=&rhs){ delete[] objects; theSize = rhs.size(); theCapacity = rhs.theCapacity; objects=new Object[capacity()]; for(int k=0;k<size();k++) objects[k]=rhs.objects[k]; } return *this; } void resize( int newSize){ if(newSize>theCapacity) reserve(newSize*2+1); theSize=newSize; } void reserve(int newCapactity){ if(newCapactity<thesize) retrun; objects *oldArray = objects; objects = new Object[newCapactity]; for(int k=0;k<theSize;k++) objects[k]=oldArray[k]; theCapacity = newCapactity; delete[] oldArray; Object& operator[] (int index){ return objects[index]; } const Object& operator[] (int index) const{ return objects[index]; } bool empty() const{ return size()==0; } int size() const{ return theSize; } int capacity() const { return theCapacity; } void push_back(const Object& x){ if(theSize==theCapacity) reserve(2*theCapacity+1); objects[theSize++]=x; } void pop_back(){ theSize--; } const Object& back() const{ return objects[theSize -1]; } typedef Object* iterator; typedef const Object* const_iterator; iterator begin(){ return &objects[0]; } const_iterator begin() const {return &objects[0];} iterator end() {return &objects[size()];} enum {SPACE_CAPACITY=16;} //:const类成员变量是在对象的生存期内是常量.以上enum常量的优点是不占用对象的存储空间,且在编译时全部求值. //上述数组值的大小头文件中求值只有依赖于枚举 // private: int theSize; int theCapacity; Object* objects; }}#endif
#ifndef VECTOR_CPP_#define VECTOR_CPP_#include "vector.h"#include "StartConv.h"template <class Object>const vector<Object> & vector<Object>::operator=( const vector<Object> & rhs ){ if( this != &rhs ) { delete [ ] objects; theSize = rhs.size( ); theCapacity = rhs.theCapacity; objects = new Object[ capacity( ) ]; for( int k = 0; k < size( ); k++ ) objects[ k ] = rhs.objects[ k ]; } return *this;}template <class Object>void vector<Object>::resize( int newSize ){ if( newSize > theCapacity ) reserve( newSize * 2 ); theSize = newSize;}template <class Object>void vector<Object>::reserve( int newCapacity ){ Object *oldArray = objects; int numToCopy = newCapacity < theSize ? newCapacity : theSize; newCapacity += SPARE_CAPACITY; objects = new Object[ newCapacity ]; for( int k = 0; k < numToCopy; k++ ) objects[ k ] = oldArray[ k ]; theSize = numToCopy; theCapacity = newCapacity; delete [ ] oldArray;}template <class Object>void vector<Object>::push_back( const Object & x ){ if( theSize == theCapacity ) reserve( 2 * theCapacity + 1 ); objects[ theSize++ ] = x;}template <class Object>void vector<Object>::pop_back( ){ if( empty( ) ) throw UnderflowException( "Cannot call pop_back on empty vector" ); theSize--;}template <class Object>const Object & vector<Object>::back( ) const{ if( empty( ) ) throw UnderflowException( "Cannot call back on empty vector" ); return objects[ theSize - 1 ];}#include "EndConv.h"#endif
* 测试程序
#include "Vector.h"#include <iostream>using namespace std;int main( ){ Vector<int> v; for( int i = 0; i < 10; i++ ) v.push_back( i ); for( int i = 0; i < 10; i++ ) cout << v[ i ] << endl; return 0;}
0 0
- 复习(数据结构):动态数组:c++_stl写法
- 复习(数据结构):动态数组:c++_常规写法
- 复习(数据结构):栈:c语言:动态数组
- 复习(数据结构):动态数组:c_语言数组
- 复习(数据结构:java):线性表(数组):接口的写法
- 复习(数据结构):栈:c语言:数组
- 复习(数据结构):线性表 : c++_标准写法
- C++_STL经典书单
- C++_STL 各种容器
- 复习(数据结构:java):线性表(数组):泛型的写法
- 数据结构(c语言)之动态数组
- 复习C语言系列二:动态调用函数指针数组
- Xc-C++_STL实例系列
- C++_STL之string用法
- 数据结构复习--数组的移动
- 数据结构复习--数组的移动
- [C] 数组另类写法
- C数组复习
- spring context标签
- 互联网自由工作者如何提高工作效率?
- fir.im Weekly - 如何写出零 bug 的代码
- pppcloud云主机内LINUX用户安全管理1
- 采用FLAG_ACTIVITY_CLEAR_TOP退出整个程序(多activity)
- 复习(数据结构):动态数组:c++_stl写法
- 动态链接库和静态链接库的区别
- scala与java的区别
- 实时系统建模与分析UML
- 2016年7月11日springmvc和Mybatis整合:
- html5的语义化标签
- Tkinter教程之Text(1)篇
- jdk环境变量配置
- 05-CoreData Scalar