码迷,mamicode.com
首页 > 其他好文 > 详细

简单的vector实现

时间:2014-11-01 17:51:23      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:stl源码剖析   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;
}



简单的vector实现

标签:stl源码剖析   vector   

原文地址:http://blog.csdn.net/u012637838/article/details/40681827

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!