<STL>模拟实现Vector

首先,这里用到了类型萃取的知识,代码如下:

struct __TrueType
{
    bool Get()
    {
        return true;
    }
};
struct __FalseType
{
    bool Get()
    {
        return false;
    }
};
template <class _Tp>
struct TypeTraits
{
    typedef __FalseType __IsPODType;
};
template <>
struct TypeTraits< bool>
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< char>
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< unsigned char >
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< short>
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< unsigned short >
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< int>
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< unsigned int >
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< long>
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< unsigned long >
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< long long >
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< unsigned long long>
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< float>
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< double>
{
    typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< long double >
{
    typedef __TrueType __IsPODType;
};
template <class _Tp>
struct TypeTraits< _Tp*>
{
    typedef __TrueType __IsPODType;
};

模拟实现vector

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include <iostream>
#include <stdlib.h>
#include <cassert>
using namespace std;
#include "Typetraits.h"

template<class T>
class Vector
{
public:
    typedef T* Iterator;
    typedef const T* ConstIterator;
public:
    void Copy(Iterator first, Iterator last, Iterator pos)
    {
        // 基本类型
        if (TypeTraits<T>::__IsPODType().Get())
        {
            memcpy(pos,first,sizeof(T)*(last-first));
        }
        else
        {
            while (first != last)
            {
                *pos++ = *first++;
            }
        }
    }
    void Destory()
    {
        if (_start)
        {
            delete[] _start;
        }
        _start = NULL;
        _finish = NULL;
        _endofstorage = NULL;
    }
    Vector()
        :_start(0)
        , _finish(0)
        , _endofstorage(0)
    {}
    Vector(Vector<T>& v)
        :_start(new T[v.Size()])
    {
        _finish = _start + v.Size();
        _endofstorage = _start + v.Size();
        Copy(v._start,v._finish,_start);
    }
    Vector<T>& operator=(const Vector<T>& v)
    {
        if (this != &v)
        {
            //先开辟空间在进行删除,避免开辟失败空间销毁
            T* tmp = new T[v.Size];
            Destory();
            _start = tmp;
            _finish = _start + v.Size();
            _endofstorage = _start + v.Size();
            Copy(v._start, v._finish, _start);
        }
        return *this;
    }
    //Vector<T>& operator=(Vector<T> v)
    //{
    //  this->Swap(v);
    //  return *this;
    //}
    //Vector<T>& operator=(Vector<T>& v)
    //{
    //  if (this != &v)
    //  {
    //      Vector<T> tmp(v);
    //      this->Swap(tmp);
    //  }
    //  return *this;
    //}
    ~Vector()
    {
        Destory();
    }
    Iterator Begin()
    {
        assert(_start);
        return _start;
    }
    ConstIterator Begin()const
    {
        assert(_start);
        return _start;
    }
    Iterator End()
    {
        assert(_last);
        return _finish;
    }
    ConstIterator End()const
    {
        assert(_last);
        return _finish;
    }
    T& operator[](size_t pos)
    {
        assert(pos < Size());

        return _start[pos];
    }

    const T& operator[](size_t pos) const
    {
        assert(pos < Size());

        return _start[pos];
    }

    size_t Size() const
    {
        return _finish - _start;
    }

    size_t Capacity()
    {
        return _endofstorage - _start;
    }
    void Swap(Vector<T>& v)
    {
        swap(_start,v._start);
        swap(_finish,v._finish);
        swap(_endofstorage,v._endofstorage);
    }
    void PushBack(const T& x)
    {
        CheckStorage();
        *_finish = x;
        ++_finish;
    }
    void PopBack()
    {
        assert(Size());
        --_finish;
    }
    void Insert(Iterator pos, const T& x)
    {
        assert(pos >= _start&&pos <= _finish);
        int tmp = pos -_start;
        int i = 0;
        for (i = Size(); i > tmp; --i)
        {
            _start[i] = _start[i - 1];
        }
        _start[tmp] = x;
        _finish++;
    }
    void Erase(Iterator pos)
    {
        assert(pos<=_finish&&pos>=_start);
        for (size_t i = pos; i<Size()-1; ++i)
        {
            _start[pos] = _start[pos + 1];
        }
        --finish;
    }
    void Resize(size_t sz)
    {
        if (Size() > sz)
        {
            _finish = _start + sz;
        }
        else if (Size() == sz)
        {
            ;
        }
        else
        {
            _finish = _start + sz;
            CheckStorage();
            for (size_t i = Size(); i<sz; i++)
            {
                _start[i] = 0;//设默认值为0  
            }
        }
    }
    void CheckStorage()
    {
        if (_finish == _endofstorage)
        {
            size_t size = Size();
            size_t capacity = Capacity();
            capacity = capacity * 2 + 3;
            T*tmp = new T[capacity];
            if (_start)
            {
                for (size_t i = 0; i < Size(); i++)
                {
                    tmp[i] = _start[i];
                }
                delete[]_start;
            }
            _start = tmp;
            _finish = _start + size;
            _endofstorage = _start + capacity;
        }
    }
protected:
    Iterator _start;
    Iterator _finish;
    Iterator _endofstorage;
};
template<class T>
void PrintVector(Vector<T>& v)
{
    for (size_t i = 0; i < v.Size(); i++)
        cout << v[i] << " ";
    cout << endl;
}
void TestVector()
{
    Vector<int> v;
    v.PushBack(1);
    v.PushBack(2);
    v.PushBack(3);
    v.PushBack(4);
    v.PushBack(5);
    Vector<int>::Iterator it1 = v.Begin();
    v.Insert(it1,9);
    PrintVector(v);
}
int main()
{
    TestVector();
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值