CUJ:标准库:Allocator能做什么?[16]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 taodm 的 blog

  typedef const value_type* const_pointer;

  typedef value_type&       reference;

  typedef const value_type& const_reference;

  typedef std::size_t       size_type;

  typedef std::ptrdiff_t    difference_type;

 

  template <class u>

  struct rebind { typedef malloc_allocator<u> other; };

 

  malloc_allocator() {}

  malloc_allocator(const malloc_allocator&) {}

  template <class u>

  malloc_allocator(const malloc_allocator<u>&) {}

  ~malloc_allocator() {}

 

  pointer address(reference x) const { return &x; }

  const_pointer address(const_reference x) const {

    return x;

  }

 

  pointer allocate(size_type n, const_pointer = 0) {

    void* p = std::malloc(n * sizeof(t));

    if (!p)

      throw std::bad_alloc();

    return static_cast<pointer>(p);

  }

 

  void deallocate(pointer p, size_type) { std::free(p); }

本文关键:CUJ、allocator、STL、Matt Austern
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top