day7_C++

day7_C++

MyVector.h

#ifndef MYVECTOR_H
#define MYVECTOR_H
#include 

using namespace std;

typedef int size_type;

template
class MyVector
{
private:
    int num;
    TYPE *data;
    TYPE * headiterator;
    TYPE * enditerator;

public:
    //无参构造
    MyVector();

    //有参构造 数量(num) 和 值(val) - 构造一个初始放入num个值为val的元素的Vector
    MyVector(size_type num, const TYPE &val);

    //析构函数
    ~MyVector();

    // == 号重载 判断 是否相等
    bool operator==(const MyVector &R) const;

    //二倍扩容
    bool doubleAdd()
    {

    }

    TYPE* insert( TYPE loc, const TYPE &val );

    TYPE* end();

    TYPE at(int pos);

    void show();

};

#endif // MYVECTOR_H

myvector.c

#include "myvector.h"

template
MyVector::MyVector()
{}


template
MyVector::MyVector(size_type num, const TYPE &val):data(new TYPE[num]),num(num)
{

    for(int i = 0;idata[i] = val;
    }
    headiterator = data;
    enditerator = data+num;

}

template
MyVector::~MyVector()
{
    delete []data;
    headiterator = nullptr;
    enditerator = nullptr;
}

template
bool MyVector::operator==(const MyVector &R) const
{
    if(this->num == R.num)
    {
        for(int i = 0;inum;i++)
        {
            if(this->data[i] != R.data[i])
                return false;
        }
        return true;
    }
    else
        return false;
}

template
TYPE* MyVector::end()
{
    return enditerator;
}


template
TYPE* MyVector::insert( TYPE loc, const TYPE &val )
{
    int dataNum = 0;
    TYPE* headp = headiterator;
    TYPE* endp = enditerator;


    while(headp!=endp)
    {
        ++dataNum;
        headp++;
    }
    dataNum+=1;
    //未满不扩容
    if(dataNumloc;i--)
        {
            data[i] = data[i-1];
        }
        data[loc] = val;
        //尾迭代往后移动一个
        enditerator++;
    }

    headiterator = new TYPE[num*2];
      

}

template
TYPE MyVector::at(int pos)
{
    TYPE* p = headiterator;
    p+pos;
    return *p;
}


template
void MyVector::show()
{
    while(data!=enditerator)
    {
        cout<<*data<

你可能感兴趣的:(作业,c++,算法)