博客
关于我
C++Dynamic_Array(vector)实现
阅读量:383 次
发布时间:2019-03-05

本文共 7218 字,大约阅读时间需要 24 分钟。

头文件

#include 
#include "Dynamic_Num.h"using namespace std;#define Dynamic_Numtemplate
class DynamicVector {public: template
friend ostream &operator<<(ostream &out, const DynamicVector
&obj); DynamicVector() : array(NULL), mallocSize(0), numOfItems(0), virtualZero(0) {} DynamicVector(int index) : array(NULL), mallocSize(0), numOfItems(0), virtualZero(index) {} DynamicVector(T * const begin, T * const end, int index = 0) : numOfItems(end - begin), virtualZero(index) { Reset(end - begin); int i = 0; for (T *ptr = begin; ptr <= end; ++ptr) { array[i++] = *ptr; } } DynamicVector(const DynamicVector &obj) { Reset(obj.mallocSize); Copy_Basic(obj); } template
DynamicVector(t * const begin, t * const end, int index = 0) { Reset(end - begin); int i = 0; for (T *ptr = begin; ptr <= end; ++ptr) { array[i++] = *ptr; } } DynamicVector &operator=(const DynamicVector &obj) { Reset(obj.mallocSize); Copy_Basic(obj); return *this; } unsigned int length() const { return numOfItems; } unsigned int capacity() const { return mallocSize; } int firstIndex() const { return virtualZero; } T *begin() const { return &array[0]; } T *end() const { return &array[numOfItems]; } T &operator[](int index) { int pos = index - virtualZero; if (pos < 0 || pos >= numOfItems) { cerr << "Invalid index value."; exit(1); } return array[pos]; } bool operator==(const DynamicVector &obj) const { if (numOfItems == obj.numOfItems && virtualZero == obj.virtualZero) { for (int i = 0; i < numOfItems; ++i) { if (array[i] != obj.array[i]) { return false; } } return true; } return false; } void swap(DynamicVector &obj) { T *temp = array; array = obj.array; obj.array = temp; unsigned int t1 = obj.numOfItems; obj.numOfItems = numOfItems; numOfItems = t1; unsigned int t2 = obj.mallocSize; obj.mallocSize = mallocSize; mallocSize = t2; int v = virtualZero; virtualZero = obj.virtualZero; obj.virtualZero = v; } void insert(int index, const T &obj) { T *temp = new T[numOfItems]; for (int i = 0; i < numOfItems; ++i) { temp[i] = array[i]; } if (numOfItems + 1 > mallocSize) { Reset(2 * mallocSize); } for (int i = 0; i < index - virtualZero; ++i) { array[i] = temp[i]; } array[index - virtualZero] = obj; for (int i = index - virtualZero, j = i + 1; i < numOfItems; ++i, ++j) { array[j] = temp[i]; } delete[] temp; ++numOfItems; } inline void push_back(const T &obj) { if (numOfItems < mallocSize) { array[numOfItems++] = obj; } else { T *temp = new T[mallocSize]; for (int i = 0; i < numOfItems; ++i) { temp[i] = array[i]; } Reset(mallocSize + 1); for (int i = 0; i < numOfItems; ++i) { array[i] = temp[i]; } delete[] temp; temp = NULL; ++numOfItems; } } void push_back(const DynamicVector &obj) { if (mallocSize <= numOfItems + obj.numOfItems) { T *temp = new T[mallocSize]; for (int i = 0; i < numOfItems; ++i) { temp[i] = array[i]; } Reset(mallocSize + obj.mallocSize); for (int i = 0; i < numOfItems; ++i) { array[i] = temp[i]; } delete[] temp; temp = NULL; } for (int i = numOfItems; i < numOfItems + obj.numOfItems; ++i) { array[i] = obj.array[i - numOfItems]; } numOfItems += obj.numOfItems; } void Reset(unsigned int size) { if (numOfItems && array) { delete[] array; } array = new T[size]; mallocSize = size; } void Copy_Basic(const DynamicVector &obj) { mallocSize = obj.mallocSize; numOfItems = obj.numOfItems; virtualZero = obj.virtualZero; for (int i = 0; i < numOfItems; ++i) { array[i] = obj.array[i]; } } DynamicVector operator()(int begin, int end) { DynamicVector temp(virtualZero); for (int i = begin - virtualZero; i < end - virtualZero; ++i) { temp.push_back(array[i]); } return temp; } void remove() { --numOfItems; } void remove(int index) { for (int i = index - virtualZero; i < numOfItems; ++i) { array[i] = array[i + 1]; } --numOfItems; } void remove(int begin, int end) { for (int i = begin - virtualZero; i < numOfItems; ++i) { array[i] = array[i + end - begin]; } numOfItems -= (end - begin); }private: T *array; unsigned int mallocSize, numOfItems; int virtualZero;}; using namespace Dynamic_Num;#endif

main.cpp

int main() {    DynamicVector
ra(-2); int i, n; cin >> n; cout << "\n"; ra.push_back(-3); ra.push_back(-2); ra.push_back(-1); for (i = 0; i < n; ++i) { ra.push_back(i); } cout << "malloSize is " << ra.capacity() << endl; cout << "numOfItems is " << ra.length() << endl; cout << "startindex is " << ra.firstIndex() << endl; for (i = 0; i < n + 1; ++i) { cout << ra[i] << " "; } cout << endl; DynamicVector
raCopy(ra); cout << "\nmalloSize is " << raCopy.capacity() << endl; cout << "numOfItems is " << raCopy.length() << endl; cout << "startindex is " << raCopy.firstIndex() << endl; for (i = 0; i < "2"; ++i) { cout << raCopy[i] << " "; } if (ra == raCopy) { cout << "\nra equals raCopy" << endl; } else { cout << "\nra != raCopy" << endl; } raCopy = ra; raCopy(-1, 3); if (raCopy != ra) { cout << "\nraCopy changed" << endl; } ra[-2] = 100; raCopy.push_back(ra); int firstIndex = raCopy.firstIndex(); raCopy.insert(-2, 6); raCopy.insert(-1, 7); cout << raCopy << endl; raCopy.remove(); cout << "raCopy after remove()" << endl; raCopy.remove(-1); cout << "ra after remove(-1)" << endl; raCopy.remove(-1, 1); cout << "raCopy after remove(-1, 1)" << endl; cout << ra << endl; DynamicVector
tmpx(100) = {0}; for (i = 0; i < n; ++i) { tmpx[i] = i * 0.1; } DynamicVector
doubleVector1(&tmpx[1], &tmpx[n]); cout << "\nnumOfItems is " << doubleVector1.length() << endl; cout << "startindex is " << doubleVector1.firstIndex() << endl; cout << doubleVector1 << endl; DynamicVector
doubleVector2(doubleVector1.begin(), doubleVector1.end(), -3); cout << "\nnumOfItems is " << doubleVector2.length() << endl; cout << "startindex is " << doubleVector2.firstIndex() << endl; cout << doubleVector2 << endl;}

转载地址:http://yquwz.baihongyu.com/

你可能感兴趣的文章
mysql-connector-java.jar乱码,最新版mysql-connector-java-8.0.15.jar,如何愉快的进行JDBC操作...
查看>>
mysql-connector-java各种版本下载地址
查看>>
mysql-EXPLAIN
查看>>
MySQL-Explain的详解
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>
MySQL-数据目录
查看>>
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>
MySQL-连接
查看>>
mysql-递归查询(二)
查看>>
MySQL5.1安装
查看>>