用C++写线性容器Vector
来源:互联网 发布:alibaba的json jar包 编辑:程序博客网 时间:2024/06/06 20:19
MyVector.h内容如下:
#pragma oncetemplate <class T>class MyVector{public:typedef T value_type;typedef value_type * iterator;typedef value_type & reference;MyVector();MyVector(int nNum, const T & value);~MyVector();iterator Begin(){ return m_ItStart; }iterator End(){ return m_ItEnd; }int Size();int Capacity();bool Empty();reference operator[](int nIndex){ return *(m_ItStart + nIndex); }reference front() { return *(m_ItStart); }reference back(){ return *(m_ItEnd - 1); }void PushBack(const T & x);void PopBack();void Erase(iterator pos);void Clear();private:iterator m_ItStart;iterator m_ItEnd;iterator m_ItStorageEnd;};template<class T>MyVector<T>::MyVector(){m_ItStart = NULL;m_ItEnd = NULL;m_ItStorageEnd = NULL;}template<class T>MyVector<T>::MyVector(int nNum, const T & value){m_ItStart = (iterator)malloc(sizeof(T)* nNum);if (m_ItStart){m_ItEnd = m_ItStart + nNum;m_ItStorageEnd = m_ItStart + nNum;for (iterator it = m_ItStart; it != m_ItEnd; it++)new (it) value_type(value);}}template<class T>MyVector<T>::~MyVector(){int nSize = m_ItEnd - m_ItStart;if (m_ItStart){for (int i = 0; i < nSize; i++)(m_ItStart + i)->~T();free(m_ItStart);}m_ItStart = NULL;m_ItEnd = NULL;m_ItStorageEnd = NULL;}template<class T>int MyVector<T>::Size(){return (int)(m_ItEnd - m_ItStart);}template<class T>int MyVector<T>::Capacity(){return (int)(m_ItStorageEnd - m_ItStart);}template<class T>bool MyVector<T>::Empty(){return (m_ItStart == m_ItEnd);}template<class T>void MyVector<T>::PushBack(const T & x){int nOldSize = 0;int nNewSize = 0;if (m_ItEnd < m_ItStorageEnd){new (m_ItEnd) value_type(x);m_ItEnd++;return;}nOldSize = Size();if (nOldSize == 0)nNewSize = 1;elsenNewSize = 2 * nOldSize;if (m_ItStart)m_ItStart = (iterator)realloc(m_ItStart, nNewSize * sizeof(value_type));elsem_ItStart = (iterator)malloc(nNewSize * sizeof(value_type));if (m_ItStart){m_ItEnd = m_ItStart + nOldSize;m_ItStorageEnd = m_ItStart + nNewSize;new (m_ItEnd) value_type (x);m_ItEnd++;}return;}template<class T>void MyVector<T>::PopBack(){if (m_ItEnd > m_ItStart){(m_ItEnd - 1)->~T();m_ItEnd--;}}template<class T>void MyVector<T>::Erase(iterator pos){if (pos < m_ItStart)return;if (pos >= m_ItEnd)return;for (iterator it = pos + 1; it != End(); it++){*(it - 1) = *it;}m_ItEnd--;m_ItEnd->~T();return;}template<class T>void MyVector<T>::Clear(){for (iterator it = m_ItStart; it < m_ItEnd; it++){it->~T();}m_ItEnd = m_ItStart;return;}
MyVector测试文件内容如下:
// CustomVector.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include "MyVector.h"#include <iostream>#define _CRTDBG_MAP_ALLOC#include<stdlib.h>#include<crtdbg.h>#include <string>using namespace std;int _tmain(int argc, _TCHAR* argv[]){{MyVector<int> IntVector;int i = 0;for (i = 0; i < 10; i++)IntVector.PushBack(i + 1);for (i = 0; i < 10; i++)cout << IntVector[i] << " ";cout << endl;cout << "size:" << IntVector.Size() << endl;cout << "capacity:" << IntVector.Capacity() << endl;if (IntVector.Empty())cout << "empty" << endl;elsecout << "not empty" << endl;int nFront = IntVector.front();int nBack = IntVector.back();cout << "Front" << nFront << endl;cout << "Back" << nBack << endl;IntVector.PopBack();nBack = IntVector.back();cout << "new back" << nBack << endl;for (MyVector<int>::iterator it = IntVector.Begin(); it != IntVector.End(); it++)if (*it == 5)IntVector.Erase(it);for (MyVector<int>::iterator it = IntVector.Begin(); it != IntVector.End(); it++)cout << *it << " ";cout << endl;cout << "size:" << IntVector.Size() << endl;IntVector.Clear();cout << "size:" << IntVector.Size() << endl;cout << "capacity:" << IntVector.Capacity() << endl;for (i = 0; i < 20; i++)IntVector.PushBack(i + 1);for (MyVector<int>::iterator it = IntVector.Begin(); it != IntVector.End(); it++)cout << *it << " ";cout << endl;cout << "size:" << IntVector.Size() << endl;cout << "capacity:" << IntVector.Capacity() << endl;if (IntVector.Empty())cout << "empty" << endl;elsecout << "not empty" << endl;nFront = IntVector.front();nBack = IntVector.back();cout << "Front" << nFront << endl;cout << "Back" << nBack << endl;MyVector<int> IntVector2(10, 1);i = 0;for (i = 0; i < 10; i++)cout << IntVector2[i] << " ";cout << endl;cout << "size:" << IntVector2.Size() << endl;cout << "capacity:" << IntVector2.Capacity() << endl;if (IntVector2.Empty())cout << "empty" << endl;elsecout << "not empty" << endl;nFront = IntVector2.front();nBack = IntVector2.back();cout << "Front" << nFront << endl;cout << "Back" << nBack << endl;IntVector2.PopBack();nBack = IntVector2.back();cout << "new back" << nBack << endl;for (MyVector<int>::iterator it = IntVector2.Begin(); it != IntVector2.End(); it++)if (*it == 5)IntVector2.Erase(it);for (MyVector<int>::iterator it = IntVector2.Begin(); it != IntVector2.End(); it++)cout << *it << " ";cout << endl;cout << "size:" << IntVector2.Size() << endl;IntVector2.Clear();cout << "size:" << IntVector2.Size() << endl;cout << "capacity:" << IntVector2.Capacity() << endl;for (i = 0; i < 20; i++)IntVector2.PushBack(i + 1);for (MyVector<int>::iterator it = IntVector2.Begin(); it != IntVector2.End(); it++)cout << *it << " ";cout << endl;cout << "size:" << IntVector2.Size() << endl;cout << "capacity:" << IntVector2.Capacity() << endl;if (IntVector2.Empty())cout << "empty" << endl;elsecout << "not empty" << endl;nFront = IntVector2.front();nBack = IntVector2.back();cout << "Front" << nFront << endl;cout << "Back" << nBack << endl;MyVector<string> StrVector;for (i = 0; i < 10; i++)StrVector.PushBack("abc");for (i = 0; i < 10; i++)cout << StrVector[i] << " ";cout << endl;}_CrtDumpMemoryLeaks();return 0;}
0 0
- 用C++写线性容器Vector
- 用vector容器写职工管理系统
- 用C++写线性容器List
- 【c++】容器vector
- C++-vector(容器)
- 【C++】实现容器Vector
- C++vector(容器)用法
- 用c语言写 成一个vector
- 【C++】顺序容器 Vector 注意事项
- vector 容器类 (C++)
- C++vector容器用法详解
- C++vector基础容器2.0
- C++STL之vector容器
- 【c++】用c++的vector容器编写简单的通讯录
- C串分隔子串到vector容器内
- C++STL容器技术之Vector
- 初学者学习C++STL之vector容器
- C++STL中vector容器的用法
- 实现GridView中ImageView动态变换的方法
- JadClipse 配置
- php提示超过30秒最长执行时间错误的解决方法
- Turtlebot—kobuki安装启动流程
- Android开发之design库学习
- 用C++写线性容器Vector
- iOS中判断字符串中的字符
- kafka基准测试
- Linux命令----分析CPU的瓶颈
- 懒癌治疗仪 - 写在前面
- [算法学习]给定一个整型数组,找出两个整数为指定整数的和(1)
- TCP的哪些事(下)
- Linux tcpdump的命令大全
- hadoop 2.6.3 BlockPlacementPolicy分析