C++的STL很强大,里面实现很多功能

就平时经常会用到的vector之类的,自己实现一下,

有哪块错误的希望大神给予指点

vector类模板

template<typename T>
class Cvector
{
public://公有方法
typedef T value_type;//类型的萃取
Cvector() //vector构造函数不开辟内存
:mpvec(NULL),msize(0),mcur(0)
{
mpvec = NULL;
msize = 0;
mcur = 0;
}

//析构函数,开辟的是个数组,所以delete[];
~Cvector()
{
delete []mpvec;
mpvec = NULL;
}
Cvector(int size,const T &val = T())//带有参数的构造函数
:msize(size),mcur(size)
{
mpvec = new T[size];
msize = size;
mcur = 0;
for (int i =0;i<size;i++)
{
mpvec[i] = val;
}
}
vector有两种插入元素的方法,一种是尾插push_back,一种是inset按位置插入,在这里实现了尾插
void push_back(const T &val)//插入元素
{
if(full())
{
resize();
}
mpvec[mcur++] = val;
}
void pop_back()//删除元素
{
if (empty())
{
return ;
}
mcur--;
}
T& operator[](int index)//[]运算符的重载
{
return mpvec[index];
}
int size()const{return mcur;}//vector 当前所使用的数组的大小
bool empty()const{return mcur==0;}//判断是否为空
bool full() //判断是否为满
{
return mcur == msize;

}

class iterator//自己实现的迭代器
{//根据使用迭代器时所需要的然后写出它的成员方法
public:
typedef T value_type;
iterator(T *ptr,int pos)
{
mit = ptr +pos;

}
void operator++()
{
mit++;
}

bool operator!=(const iterator &src)
{
return mit != src.mit;
}
T &operator *()
{
return *mit;
}
iterator operator+(int size)
{
return iterator(mit+size,0);
}
iterator operator-(int size)
{
return iterator(mit-size,0);
}

private:
T *mit;
};

iterator begin()
{
return iterator(mpvec,0);
}
iterator end()
{
return iterator(mpvec,mcur);
}

private://成员变量

T *mpvec;//指针指向一个数组
int msize;//数组的总大小

int mcur;//当前所用的大小

void resize()//当数组满了之后自己将数组的大小放大至二倍

{
if (mpvec = NULL)
{
mpvec = new T[1];
msize = 1;
mcur =0;
}
else
{
T *ptmp = new T[msize*2];
memcpy(ptmp,mpvec,sizeof(T)*mcur);
delete []mpvec;
mpvec = ptmp;

}

}
Cvector<T>& operator=(const Cvector<T>&src);
Cvector(const Cvector<T>&src);
};

//自己实现的打印函数模板

template<typename Container>
void showContainer(Container &con)
{
Container::iterator it = con.begin();
for (;it != con.end();++it)
{
cout<< *it <<" ";
}
cout<<endl;
}

//排序函数,最后一个参数是函数指针,但是调用函数的时候开销太大不建议使用
template<typename InputIterator>
void mysort(InputIterator first,InputIterator last
,bool (*pfunc)(typename InputIterator::value_type,
typename InputIterator::value_type))
{
typedef InputIterator::value_type T;
InputIterator it1 = first;
InputIterator it2 = first;
int k=0;
for (;it1 != last;++it1,++k)
{
for (it2 = first;it2!=last-1-k;++it2)
{

if (*it2 > *(it2+1))
{
T tmp = *it2;
*it2 = *(it2+1);
*(it2+1) = tmp;

}
}
}

}
template<typename T>
bool _greater(T a,T b)
{
return a>b;
}
template<typename T>
bool _less(T a,T b)
{
return a<b;
}

//sort函数的第二个版本
template<typename InputIterator,typename Compare>
void mysort(InputIterator first,InputIterator last
,Compare &comp)
{
typedef InputIterator::value_type T;
InputIterator it1 = first;
InputIterator it2 = first;
int k=0;
for (;it1 != last;++it1,++k)
{
for (it2 = first;it2!=last-1-k;++it2)
{

if (*it2 > *(it2+1))
{
T tmp = *it2;
*it2 = *(it2+1);
*(it2+1) = tmp;

}
}
}

}

template<typename T>
class Mygreater
{
public:
bool operator()(T a,T b)
{
return a>b;
}

};
template<typename T>
class Myless
{
public:
bool operator()(T a,T b)
{
return a<b;
}
};

//自己实现的myfind_if函数
template<typename InputIterator,typename Compare>
InputIterator myfind_if(InputIterator first,InputIterator last
,Compare &comp)
{

for (;first!=last;++first)
{
if (comp(*first))
{
return first;
}
}
return last;

}

template<typename T>
class My_mless
{
public:
My_mless(T Val = T()):val(Val){}
bool operator()(T a)
{
return a<val;
}
private:
T val;
};

//mycopy函数
template<typename InputIterator, typename OutputIterator>
void mycopy(InputIterator first, InputIterator last,
OutputIterator dest)
{
InputIterator it = first;
for (; it != last; ++first)
{
*dest = *it;
++dest;
}
}

//插入型迭代器
template<typename Container>
class myback_insert_iterator
{
public:
typedef typename Container::value_type T;
myback_insert_iterator(Container &Con):con(Con){}
void operator=(const T&val)
{
con.push_back(val);

}
myback_insert_iterator<Container>& operator*()
{
return *this;
}
myback_insert_iterator<Container>& operator++()
{
return *this;
}

private:
Container &con;
};

template<typename Container>
myback_insert_iterator<Container>myback_insert(Container &com)
{
return myback_insert_iterator<Container>(com);
}

template<typename Container>
class myback_front_iterator
{
public:
typedef typename Container::value_type T;
myback_front_iterator(Container &Con):con(Con){}
void operator=(const T&val)
{
con.push_front(val);

}
myback_front_iterator<Container>& operator*()
{
return *this;
}
myback_front_iterator<Container>& operator++()
{
return *this;
}

private:
Container &con;
};

template<typename Container>
myback_insert_iterator<Container>myfront_insert(Container &com)
{
return myback_front_iterator<Container>(com);
}

自己实现了一个myfind函数模板

参数给定两个迭代器,用迭代器遍历容器,找到val以后返回val当前所处的迭代器,找不到返回first

template<typename InputIterator,typename T>
InputIterator myfind(InputIterator first,InputIterator last,const T &val)
{

for (;first != last;++first)
{
if (*first == val)
{
return first;
}

}
return last;
}

05-11 10:54