allocator写这个的时候出错了但是不明白哪里错了,报错信息很奇怪
#include <iostream>#include <stdio.h>
using namespace std;
template<typename T>
class Allocator {
public:
T* allocate(size_t _size = 10)
{
return (T*)malloc(sizeof(T) * _size);
}
void freeallocate(void* p)
{
free(p);
}
void construct(T* p, const T& value)
{
new (p) T(value);
}
void deleteallocate(T* p)
{
p->~T();
}
};
template<typename T, typename Alloc = Allocator<T>>
class vector {
private:
T* _first;
T* _last;
T* _end;
Alloc _alloc;
void expand()
{
int _size = _end - _first;
_size = _size * 2;
//T* _new_first = new T ;
T* _new_first = _alloc.allocate(_size);
int _len = _last - _first;
for (int i = 0; i < _len; i++)
{
//_new_first = _first;
_alloc.construct(_new_first, _first);
}
for (T* p = _first; p != _last; ++p) {
_alloc.deleteallocate(p);
}
_alloc.freeallocate(_first);
_first = _new_first;
_last = _first + _len;
_end = _first + _size;
}
public:
vector<T>(int _size = 10)
{
//_first = new T ;
_first = _alloc.allocate(_size);
_last = _first;
_end = _first + _size;
}
vector<T>(const vector<T>& st)
{
int _size = st._end - st._first;
//_first = new T ;
_first = _alloc.allocate(_size);
int _len = st._last - st._first;
for (int i = 0; i < _len; i++)
{
//_first = st._first;
_alloc.construct(_first, st._first);
}
_last = _first + _len;
_end = _first + _size;
}
vector<T>& operator=(const vector<T>& st)
{
if (st == *this)
return *this;
//delete[] _first;
for (T* p = _first; p != _last; ++p) {
_alloc.deleteallocate(p);
}
_alloc.freeallocate(_first);
int _size = st._end - st._first;
//_first = new T ;
_first = _alloc.allocate(_size);
int _len = st._last - st._first;
for (int i = 0; i < _len; i++)
{
//_first = st._first;
_alloc.construct(_first, st._first);
}
_last = _first + _len;
_end = _first + _size;
return *this;
}
~vector<T>()
{
for (T* p = _first; p != _last; ++p) {
_alloc.deleteallocate(p);
}
_alloc.freeallocate(_first); // 释放内存
_first = nullptr;
_last = nullptr;
_end = nullptr;
}
void push(T val)
{
if (_end == _last)
{
expand();
}
//*_last++ = val;
_alloc.construct(_last++, val);
}
void popback()
{
if (_first == _last)
return;
_last--;
_alloc.deleteallocte(_last);
}
T back()
{
return *(_last - 1);
}
bool empty()
{
return _first == _last;
}
int size()
{
return _last - _first;
}
};
int main()
{
vector<int> p;
for (int i = 0; i < 15; i++)
{
p.push(rand() % 100);
}
cout << p.size() << endl;
while (!p.empty())
{
cout << p.back() << "";
p.popback();
}
cout << endl;
} 你好,鱼油。你提供的代码有一些小问题导致编译错误或运行时错误。以下是改进的代码以及一些注意事项:
1. 确保包含所需的头文件。
2. 修复模板定义中的语法错误。
3. 修复删除析构函数时的拼写错误。
4. 修复分配和构造对象的代码。
5. 修正比较运算符中的逻辑错误,以及打印输出的部分。
#include <iostream>
#include <cstdlib>
using namespace std;
template <typename T>
class Allocator {
public:
T* allocate(size_t _size = 10)
{
return (T*)malloc(sizeof(T) * _size);
}
void freeallocate(void* p)
{
free(p);
}
void construct(T* p, const T& value)
{
new (p) T(value);
}
void deleteallocate(T* p)
{
p->~T();
}
};
template <typename T, typename Alloc = Allocator<T>>
class vector {
private:
T* _first;
T* _last;
T* _end;
Alloc _alloc;
void expand()
{
int _size = _end - _first;
_size = _size * 2;
T* _new_first = _alloc.allocate(_size);
int _len = _last - _first;
for (int i = 0; i < _len; i++)
{
_alloc.construct(_new_first + i, _first);
}
for (T* p = _first; p != _last; ++p) {
_alloc.deleteallocate(p);
}
_alloc.freeallocate(_first);
_first = _new_first;
_last = _new_first + _len;
_end = _new_first + _size;
}
public:
vector(int _size = 10)
{
_first = _alloc.allocate(_size);
_last = _first;
_end = _first + _size;
}
vector(const vector& st)
{
int _size = st._end - st._first;
_first = _alloc.allocate(_size);
int _len = st._last- st._first;
for (int i = 0; i < _len; i++)
{
_alloc.construct(_first + i, st._first);
}
_last = _first + _len;
_end = _first + _size;
}
vector& operator=(const vector& st)
{
if (this == &st)
return *this;
for (T* p = _first; p != _last; ++p) {
_alloc.deleteallocate(p);
}
_alloc.freeallocate(_first);
int _size = st._end - st._first;
_first = _alloc.allocate(_size);
int _len = st._last - st._first;
for (int i = 0; i < _len; i++)
{
_alloc.construct(_first + i, st._first);
}
_last = _first + _len;
_end = _first + _size;
return *this;
}
~vector()
{
for (T* p = _first; p != _last; ++p) {
_alloc.deleteallocate(p);
}
_alloc.freeallocate(_first);
}
void push(T val)
{
if (_end == _last)
{
expand();
}
_alloc.construct(_last++, val);
}
void popback()
{
if (_first == _last)
return;
_alloc.deleteallocate(--_last);
}
T back()
{
return *(_last - 1);
}
bool empty()
{
return _first == _last;
}
int size()
{
return _last - _first;
}
};
int main()
{
vector<int> p;
for (int i = 0; i < 15; i++)
{
p.push(rand() % 100);
}
cout << "Vector contains: ";
while (!p.empty())
{
cout << p.back() << " ";
p.popback();
}
cout << endl;
return 0;
}
几个要点说明:
1. 使用 `template <typename T>` 来定义模板。
2. 确认使用 `new (p) T(value)` 来调用构造函数。
3. 注意将 `deleteallocte` 拼写改为 `deleteallocate`。
4. 在拷贝构造函数和赋值运算符中,正确地构造新的元素。
5. 在 `expand` 方法中,正确地将 `_first` 的元素迁移到新分配的内存。
希望这些改动可以解决你的问题!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]