1_c++实现简单vector
2013-11-21 00:41
253 查看
实现了一些大概的功能,增加,删除,判空,修改,尾部插入,注释很详细,直接贴代码
头文件:CustomVector.h
类文件:CustomVector.cpp
简单测试文件:main.cpp
输出:
初始化数据:
0 1 2 3 4 5 6 7 8 9
插入一个节点:
0 1 2 3 4 5 6 7 8 9
5
删除两个节点:
2 3 4 5 6 7 8 9 5
插入一个节点
2 3 4 5 3 6 7 8 9 5
修改一个节点
2 3 4 5 4 6 7 8 9 5
Press any key to continue . . .
头文件:CustomVector.h
#pragma once template<class T,int n> class CustomVector { private: T *p;//初始化数组p int cur_len;//记录现在有效长度的元素个数 int len;//记录总长度 public: CustomVector<T,n>(void); ~CustomVector<T,n>(void); void addNodeToLast(int val); void showVector(); void deleNode(int pos); void insert(int pos,int value); void update(int pos,int value); bool isEmpty(); };
类文件:CustomVector.cpp
#include "CustomVector.h" template<class T,int n> CustomVector<T,n>::CustomVector(void) { this->p=new T ; this->len=n; this->cur_len=len; for (int i=0;i<n;i++) { *(p+i)=i; } } template<class T,int n> CustomVector<T,n>::~CustomVector(void) { delete [] p; } template<class T,int n> void CustomVector<T,n>::addNodeToLast(int val){ if (this->cur_len<this->len)//当数组长度还没有满的时候 { *(p+(this->cur_len++))=val; }else{ //重新申请一片内存,进行数组拷贝 T *pt=p; p=new T[++this->len]; for (int i=0;i<this->cur_len;i++) { *(p+i)=*(pt+i); } p[this->cur_len]=val; this->cur_len++; } } template<class T,int n> void CustomVector<T,n>::showVector(){ for (int i=0;i<this->cur_len;i++) { printf("%d\t",*(p+i)); } printf("\n"); } template<class T,int n> void CustomVector<T,n>::deleNode(int pos){ if (pos<0||pos>this->cur_len) { return ; } //把后面的元素往前挪动一个单位,然后长度-1 for (int i=pos-1;i<cur_len;i++) { p[i]=p[i+1]; } cur_len--; }; template<class T,int n> void CustomVector<T,n>:: insert(int pos,int value){ if (pos<0||pos>cur_len) { return ; } //申请一片+1的内存 if (cur_len>=len) { T *pt=p; p=new T[++len]; //拷贝数据 for (int i=0;i<cur_len;i++) { *(p+i)=*(pt+i); } }else{ //往后挪动一个位置 for (int i=cur_len-1;i>=pos-1;i--) { p[i+1]=p[i];//把pos-1之后的元素都往后挪动一个单位 } p[pos-1]=value; cur_len++;//插入成功后+1 } } template<class T,int n> bool CustomVector<T,n>:: isEmpty(){ if (cur_len==0) { return false; }else{ return true; } } template<class T,int n> void CustomVector<T,n>::update(int pos,int value){ if (pos<0||pos>len) { return ; } p[pos-1]=value; }
简单测试文件:main.cpp
#include <stdlib.h> #include <stdio.h> using namespace std; #include "CustomVector.h" #include "CustomVector.cpp" void main(){ CustomVector<int ,10> vctor; printf("初始化数据:\n"); vctor.showVector(); vctor.addNodeToLast(5); printf("插入一个节点:\n"); vctor.showVector(); printf("删除两个节点:\n"); vctor.deleNode(0); vctor.deleNode(1); vctor.showVector(); printf("插入一个节点\n"); vctor.insert(5,3); vctor.showVector(); printf("修改一个节点\n"); vctor.update(5,4); vctor.showVector(); system("pause"); }
输出:
初始化数据:
0 1 2 3 4 5 6 7 8 9
插入一个节点:
0 1 2 3 4 5 6 7 8 9
5
删除两个节点:
2 3 4 5 6 7 8 9 5
插入一个节点
2 3 4 5 3 6 7 8 9 5
修改一个节点
2 3 4 5 4 6 7 8 9 5
Press any key to continue . . .
相关文章推荐
- Vector的简单C++实现
- c/c++ vector 的简单实现
- c++ stl库 vector简单的实现
- [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)
- 简单数据结构之顺序表(C++ vector 实现)
- SLT容器之vector简单实现C++运行时多态
- [置顶] 从零开始学C++之STL(二):实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)
- [置顶] 从零开始学C++之STL(二):实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)
- C++智能指针,指针容器原理及简单实现(auto_ptr,scoped_ptr,ptr_vector).
- C++智能指针,指针容器原理及简单实现(auto_ptr,scoped_ptr,ptr_vector).
- c++vector简单实现
- C++之STL(二):实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)
- 仿c++的vector简单实现
- c++ vector 简单实现。
- 简单数据结构之 vector 栈(C++ vector 实现)
- C++ vector 的简单实现
- C++自己实现一个简单地Vector
- c++ stl库 vector简单的实现
- [置顶] 从零开始学C++之STL(二):实现简单容器模板类Vec(vector capacity 增长问题、allocator 内存分配器)
- 必须要注意的 C++ 动态内存资源管理(六)——vector的简单实现