简单vector达到
得知c++于,看完这本书后,,最近苦于不知道下一步该怎么做了,在寻找STL在各种容器的源代码分析,我想一次又一次地实现它。
之前,很多问题看的时候不知道是怎么回事,意与理解的。这个vector类写得特别简单,仅仅实现了当中的一部分功能,由于没有使用iterator。发现一些函数不太好写,并且封装性极差。同一时候,仅仅能存放内置类型。在这些问题中,希望自己以后会好好重写一下。
这个程序有点小情况。进行插入操作时,在VC++6.0下执行是正常的,在codeblocks下最后一个数却是乱的,可是在codeblocks下进行调试时又是正常的,前面写了一个关于跳表的程序也是这样,都快疯了,要是有人知道原因,求告知。
Vector.h
#ifndef VECTOR_H_ #define VECTOR_H_ #include <iostream> #include <cstdlib> /** * 实现中仅仅能处理内置类型T */ namespace flysnow{ template<class T> class Vector { public: T* start; T* finish; T* end_of_storage; //void alloc public: Vector():start(NULL),finish(NULL),end_of_storage(NULL) {} Vector(size_t n,const T& value); //Vector(int n,const T& value); //Vector(long n,const T& value); explicit Vector(size_t n); ~Vector() {free(start);} //在这里发现得推断是内置类型还是class,这里仅仅支持内置类型 T& front() {return *start;} T& back() {return *finish;} void push_back(const T& t); void pop_back(); T* erase(T* position); T* erase(T* position,size_t n); void insert(T* pos,size_t n,const T& x); size_t size() {return finish-start;} bool empty() {return start == finish;} void resize(size_t n,const T& x); void resize(size_t n); void clear(); size_t capacity() {return end_of_storage-start;} T& operator[] (size_t index) {return *(start+index);} T* get_pointer() {return start;} }; template<class T> Vector<T>::Vector(size_t n,const T& value) { start = (T*)malloc(n*sizeof(T)); finish = start + n; end_of_storage = finish; for(size_t i=0; i<n; i++) { start[i] = value; } } template<class T> Vector<T>::Vector(size_t n) { start = (T*)malloc(n*sizeof(T)); finish = start + n; end_of_storage = finish; for(int i=0; i<n; i++) { //start[i] = T(); start[i] = 0; } } template<class T> void Vector<T>::push_back(const T& t) { if(end_of_storage != finish) { *finish = t; ++finish; } else { size_t old_size = size(); size_t new_size = old_size?(2*old_size):1; T* new_start = (T*)malloc(new_size*sizeof(T)); for(size_t i=0; i<old_size; i++) { *(new_start+i) = *(start+i); } *(new_start+old_size) = t; start = new_start; finish = start + old_size + 1; end_of_storage = start + new_size; } } template<class T> void Vector<T>::pop_back() { if(empty()) { return ; } --finish; // finish->~T(); } template<class T> T* Vector<T>::erase(T* pos) { T* temp = pos; while(pos != finish-1) { *pos = *(pos+1); pos++; } --finish; return temp; } template<class T> T* Vector<T>::erase(T* pos,size_t n) { T*p = pos+n; T* result = pos; if(p >= finish) { n = finish-pos+1; erase(pos,n); } else { while(p < finish) { *pos++ = *p++; } finish -= n; } return result; } template<class T> void Vector<T>::insert(T* pos,size_t n,const T& x) { size_t i; size_t old_size = size(); size_t index = pos-start; if(n <= end_of_storage-finish+1) { for( i=old_size-1;i>=index;i--) { start[i+n] = start[i]; } while(pos <= start+index+n-1) { *pos++ = x; } finish += n; std::cout<<*(finish-1)<<std::endl; for( i=0;i<size();i++) std::cout<<start[i]<<' '; std::cout<<std::endl; } else { size_t new_size = old_size; new_size += old_size>n?old_size:n; T* new_start = (T*)malloc(new_size*sizeof(T)); for( i=0;i<old_size;i++) { new_start[i] = start[i]; } start = new_start; finish = start + old_size; end_of_storage = start + new_size; insert(start+index,n,x); } } template<class T> void Vector<T>::resize(size_t n,const T& x) { size_t old_size = size(); if(n <= old_size) { erase(start+n,old_size-n); } else if(n <= capacity()){ for(int i=old_size;i<n;i++) { start[i] = x; } finish = start + n; } else { size_t new_size =old_size + old_size > n?
old_size:n; T* new_start = (T*)malloc(new_size*sizeof(T)); for(int i=0;i<old_size;i++) { new_start[i] = start[i]; } start = new_start; finish = start + old_size; end_of_storage = start + new_size; resize(n,x); } } template<class T> void Vector<T>::resize(size_t n) { resize(n,0); } template<class T> void Vector<T>::clear() { free(start); start = NULL; finish = NULL; end_of_storage = NULL; } } #endif
main.cpp
#include <iostream> #include "Vector.h" using namespace std; int main() { flysnow::Vector<int> vec; vec.push_back(4); vec.push_back(5); vec.push_back(9); vec.push_back(4); vec.push_back(2); vec.insert(vec.get_pointer()+2,4,10); // for(size_t i=0;i<vec.size();i++) // cout<<vec[i]<<' '; // cout<<endl; vec.insert(vec.get_pointer()+1,1,11); cout<<*(vec.erase(vec.get_pointer()+4,3))<<endl; // cout<<*(vec.finish-1)<<endl; for(size_t i=0;i<vec.size();i++) cout<<vec[i]<<' '; cout<<endl; return 0; }
版权声明:本文博主原创文章。博客,未经同意,不得转载。