3条回答 默认 最新
- Azhu阿朱 2017-06-05 14:45关注
#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;
}
}解决 1无用
悬赏问题
- ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
- ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
- ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
- ¥20 腾讯企业邮箱邮件可以恢复么
- ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
- ¥15 错误 LNK2001 无法解析的外部符号
- ¥50 安装pyaudiokits失败
- ¥15 计组这些题应该咋做呀
- ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
- ¥15 让node服务器有自动加载文件的功能