求牛人帮忙注释程序请尽量详细点,实在看不懂了//Vector.h#ifndef _QT_VECTOR_H_INCLUDED_#define _QT_VECTOR_H_INCLUDED_#include /*templateclass Vectorassume T has trial destructor*/templateclass Vector{public:typedef T* Iterator;privat
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/17 00:47:08
求牛人帮忙注释程序请尽量详细点,实在看不懂了//Vector.h#ifndef _QT_VECTOR_H_INCLUDED_#define _QT_VECTOR_H_INCLUDED_#include /*templateclass Vectorassume T has trial destructor*/templateclass Vector{public:typedef T* Iterator;privat
求牛人帮忙注释程序
请尽量详细点,实在看不懂了
//Vector.h
#ifndef _QT_VECTOR_H_INCLUDED_
#define _QT_VECTOR_H_INCLUDED_
#include
/*
templateclass Vector
assume T has trial destructor
*/
template
class Vector
{
public:
typedef T* Iterator;
private:
T* elem_;
int size_;
int capacity_;
protected:
int next_capacity(void) const
{
return capacity_ 0)
{
elem_ = (T*) malloc(n*sizeof(T));
//assume(elem_ = 0);
std::uninitialized_fill(elem_,elem_ + n,t);
}
}
virtual Vector(void)
{
free(elem_);
elem_ = 0; //not necessary but recommended
}
int size(void) const
{
return size_;
}
int capacity(void) const
{
return capacity_;
}
bool empty(void) const
{
return size_ == 0;
}
void clear(void)
{
size_ = 0;
}
T* begin(void) const
{
return elem_;
}
T* end(void) const
{
return elem_ + size_;
}
T& front(void) const
{
return elem_[0];
}
T& back(void) const
{
return elem_[size_ -1];
}
void reserve(int n)
{
if(n > capacity_)
{
elem_ = (T*)realloc(elem_,n*sizeof(T));
//assume(elem_ = 0);
capacity_ = n;
}
}
void erase(Iterator pos)
{
memmove(pos,pos+1,sizeof(T)*(elem_ + --size_ - pos));
}
T& operator[](int n) const
{ // 0 erase(first);
}
};//~class Vector
//#include "Vector_impl.h"
#endif //~#ifndef _QT_VECTOR_H_INCLUDED_
//~Vector.h
求牛人帮忙注释程序请尽量详细点,实在看不懂了//Vector.h#ifndef _QT_VECTOR_H_INCLUDED_#define _QT_VECTOR_H_INCLUDED_#include /*templateclass Vectorassume T has trial destructor*/templateclass Vector{public:typedef T* Iterator;privat
//Vector.h
#ifndef _QT_VECTOR_H_INCLUDED_ //编译预处理 让此头文件只能被加入一次,效果同微软的:#pragma once
#define _QT_VECTOR_H_INCLUDED_
#include //对内存操作的头文件
/*提供了内存操作相关的一些函数及声明,如:
extern void *memchr(const void *,int,size_t);
extern void *memccpy();
extern void *memchr();
extern void *memcpy();
extern void *memset();
extern int memcmp();
如果你的程序中有用到这些函数的话,必须包含该头文件.
*/
/*
templateclass Vector
assume T has trial destructor
*/
template //定义模板类
class Vector //向量类
{
public:
typedef T* Iterator; //T指针变量的别名Iterator 专业点来说叫迭代器(参见STL)
private://成员变量
T* elem_; //element成员
int size_; //size 大小
int capacity_; //容量
protected:
int next_capacity(void) const
{
//容量是否小于等于0呢?是,返回1.否,返回2*容量
return capacity_ 0)
{
elem_ = (T*) malloc(n*sizeof(T)); //为其分配 T*类型的n倍大小的空间
//assume(elem_ != 0);
std::uninitialized_fill(elem_,elem_ + n,t);
/*意外安全和正确地摧毁对象.标准函数std::uninitialized_copy和std::uninitialized_fill是两个很有用的工具.*/
}
}
virtual Vector(void) //析构函数
{
free(elem_); //释放内存
elem_ = 0; //not necessary but recommended
}
int size(void) const //返回大小
{
return size_;
}
int capacity(void) const //返回容量
{
return capacity_;
}
bool empty(void) const //查看是否为空
{
return size_ == 0;
}
void clear(void) //将大小设置为0
{
size_ = 0;
}
T* begin(void) const //返回成员指针
{
return elem_;
}
T* end(void) const //返回最后一个指针
{
return elem_ + size_;
}
T& front(void) const //返回第一个数据成员
{
return elem_[0];
}
T& back(void) const //返回最后一个(从0开始所以要-1)
{
return elem_[size_ -1];
}
void reserve(int n) //重新分配
{
if(n > capacity_) //当传入的数据已经超过总容量时
{
elem_ = (T*)realloc(elem_,n*sizeof(T)); //重新分配
//assume(elem_ != 0);
capacity_ = n; //分配大小
}
}
void erase(Iterator pos) //迭代器
{
//刚开始插入的头文件开始用了.
//将一块缓冲区中的数据移动到另一块缓冲区中.
memmove(pos,pos+1,sizeof(T)*(elem_ + --size_ - pos));
}
T& operator[](int n) const //重载[]
{ // 0 erase(first);
}
};//~class Vector
//#include "Vector_impl.h"
#endif //~#ifndef _QT_VECTOR_H_INCLUDED_
//~Vector.h