C++编程题,期末要答辩!

写一个有关集合的类实现缺省构造、复制构造、集合元素限定在整数、小数、字符以及字符串。同时实现一些基本运算图片说明

#include

#include

#include

using namespace std;
template
void destroy(T* pointer)
{
pointer->~T();
}
template
void destroy(Iterator1 first, Iterator1 last)
{
for (Iterator1
it = first; it != last; ++it)
{
destroy(&*it);
}
}
template
class Vector
{
public:
typedef T value_type;
typedef T* Iterator;
typedef const T*const_Iterator;
typedef T* pointer;
typedef const T* const_pointer;
typedef size_t size_type;
Vector()
: start(NULL), end(NULL), resum(NULL)
{}
~Vector()
{
::destroy(start, end);
alloc.deallocate(start, resum - start);
}
Iterator Begin() { return start; }
Iterator End() { return end; }
void insert(Iterator position, size_type n, const T& value);
void Push_back(const T& value)
{insert(End(), 1, value);}
protected:
Iterator start;

Iterator end;

Iterator resum;
private:
static std::allocator alloc;

};
template
std::allocator Vector::alloc;
template
void Vector::insert(Iterator position, size_type n, const T& value)
{
if (n <= resum - end)
{
if (n <= end - position)
{
std::uninitialized_copy(end - n, end, end);
std::copy(position, end - n, position + n);
std::fill_n(position, n, value);
}
else
{
std::uninitialized_fill_n(end, n - (end - position), value);
std::uninitialized_copy(position, end, position + n);
std::fill(position, end, value);
}
end += n;
}
else
{
pointer new_start(NULL), new_end(NULL);
size_type old_type = resum - start;
size_type new_size = old_type + std::max(old_type, n);
new_start = alloc.allocate(new_size);
new_end = std::uninitialized_copy(start, position, new_start);
std::uninitialized_fill_n(new_end, n, value);
new_end += n;
new_end = std::uninitialized_copy(position, end, new_end);
alloc.deallocate(start, resum - start);
start = new_start;
end = new_end;
resum = new_start + new_size;
}
}
int main()
{
Vector a;
for(int i=1;i<=5;i++)
a.Push_back(i);
Vector::Iterator it;
for(it=a.Begin();it!=a.End();++it)
{
cout<<*it<<endl;
}
}

这是我之前写的vecotor类,你可以看看

http://www.jb51.net/article/88225.htm