如何声明分配器?

发布于 2024-10-09 08:44:47 字数 2174 浏览 0 评论 0原文

我正在尝试在 cpp 中重新创建内置向量类,以便对类和内存管理进行更多练习。我不断收到一条错误消息,提示“ISO C++ 禁止声明没有类型的‘分配器’”,但我终其一生都不明白为什么。我有什么遗漏的吗?

#include <cstddef>
#include <memory>

template <class T>
class myvector{
public:
    typedef T* iterator;
    typedef const T* const_iterator;
    typedef size_t size_type;

    myvector(){ data = avail = limit = 0; }

    explicit myvector(size_type n, const T& t = T()) { create(n,t); }

    myvector(const myvector& v){  create(v.begin(), v.end());  }

    ~myvector() {  uncreate();  }

    myvector& operator=(const myvector& v)
    {
        if (&v != this){
            uncreate();
            create(v.begin(), v.end());
        }
        return *this;
    }


    T& operator[](size_type i) { return data[i];  }
    const T& operator[](size_type i) const {  return data[i];  }

    iterator begin(){  return data;  }
    const_iterator begin() const{  return data;  }

    iterator end(){  return limit;  }    
    const_iterator end() const{  return limit;  }

    size_type size(){  return avail - data;  }

    void push_back(T t)
    {
        if(avail == limit)
            size_type new_size = max(2*(limit-data),ptrdiff_t(1));
            iterator new_data = alloc.allocate(new_size);
            iterator new_avail = uninitialized_copy(data,avail,new_data);
            uncreate();
            data = new_data;
            avail = new_avail;
            limit = data + new_size;
        alloc.construct(avail++,t);
    }
private:
    iterator data;
    iterator avail;
    iterator limit;
    allocator<T> alloc;

    void create(size_type n, const T& t)
    {
        data = alloc.allocate(n);
        limit = avail = data+n;
        uninitialized_fill(data,limit,t);
    }

    void create(const_iterator i, const_iterator j)
    {
        data = alloc.allocate(j-i);
        limit = avail = uninitialized_copy(i,j,data);
    }

    void uncreate()
    {
        if(data){
            iterator it = avail;
            while(it != data) {   alloc.destroy(--it);    }
            alloc.deallocate(data,limit-data);
        }
        data = limit = avail = 0;
    }
};

I'm trying to recreate the inbuilt vector class in cpp to get a bit more practice with classes and memory management. I keep getting an error that says 'ISO C++ forbids declaration of ‘allocator’ with no type' and I can't figure out why for the life of me. Is there something I'm missing?

#include <cstddef>
#include <memory>

template <class T>
class myvector{
public:
    typedef T* iterator;
    typedef const T* const_iterator;
    typedef size_t size_type;

    myvector(){ data = avail = limit = 0; }

    explicit myvector(size_type n, const T& t = T()) { create(n,t); }

    myvector(const myvector& v){  create(v.begin(), v.end());  }

    ~myvector() {  uncreate();  }

    myvector& operator=(const myvector& v)
    {
        if (&v != this){
            uncreate();
            create(v.begin(), v.end());
        }
        return *this;
    }


    T& operator[](size_type i) { return data[i];  }
    const T& operator[](size_type i) const {  return data[i];  }

    iterator begin(){  return data;  }
    const_iterator begin() const{  return data;  }

    iterator end(){  return limit;  }    
    const_iterator end() const{  return limit;  }

    size_type size(){  return avail - data;  }

    void push_back(T t)
    {
        if(avail == limit)
            size_type new_size = max(2*(limit-data),ptrdiff_t(1));
            iterator new_data = alloc.allocate(new_size);
            iterator new_avail = uninitialized_copy(data,avail,new_data);
            uncreate();
            data = new_data;
            avail = new_avail;
            limit = data + new_size;
        alloc.construct(avail++,t);
    }
private:
    iterator data;
    iterator avail;
    iterator limit;
    allocator<T> alloc;

    void create(size_type n, const T& t)
    {
        data = alloc.allocate(n);
        limit = avail = data+n;
        uninitialized_fill(data,limit,t);
    }

    void create(const_iterator i, const_iterator j)
    {
        data = alloc.allocate(j-i);
        limit = avail = uninitialized_copy(i,j,data);
    }

    void uncreate()
    {
        if(data){
            iterator it = avail;
            while(it != data) {   alloc.destroy(--it);    }
            alloc.deallocate(data,limit-data);
        }
        data = limit = avail = 0;
    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如何视而不见 2024-10-16 08:44:47

应该是 std::allocatoralloc;,标准库中的所有内容都包含在命名空间 std 中。

It should be, std::allocator<T> alloc;, everything in the standard library is contained within the namespace std.

爱情眠于流年 2024-10-16 08:44:47

另请参阅 http://code .google.com/p/owasp-esapi-cplusplus/source/browse/trunk/esapi/util/zAllocator.h。它包括 GCC 错误的解决方法。

注意:标准 C++ 容器只需检查 vector::resize 上的溢出(希望现在已经改变)。 ESAPI 分配器在所有分配期间进行检查。

Alos see http://code.google.com/p/owasp-esapi-cplusplus/source/browse/trunk/esapi/util/zAllocator.h. It includes work arounds for GCC bugs.

Note: the standard C++ containers only have to check for overflow on vector::resize (hopefully that has changed by now). The ESAPI allocator checks during all allocations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文