allocarors的部分用法

类别:编程语言 点击:0 评论:0 推荐:

#include <iostream>
using namespace std;
template<typename Ty,
              typename Allocator = allocator<Ty> >
class coll {
  public:
     explicit coll(const size_t size = 10,
                   const Allocator& a = Allocator())
              : alloc(a), sizenum(zize) {}
     explicit coll(const size_t size,
                   const Ty& val,
                   const Allocator& a = Allocator())
              : alloc(a)
     {
         sizenum = size;
         root = alloc.allocate(sizenum);
   uninitialized_fill_n(root, sizenum, val);        
     }
  coll(const coll<Ty>& c)
   : alloc(c.alloc)
     {
         sizenum = c.sizenum;
         root = alloc.allocate(sizenum);
   uninitialized_copy(c.root, c.root+sizenum,
                   root);        
     }
  virtual ~coll()
  {
   Ty* pout = root;
         for(size_t i = 0; i < sizenum; ++i)
         {
             alloc.destroy(pout);
             ++pout;
         }
   alloc.deallocate(root,sizenum);
  }
  const coll<Ty>& operator= (const coll<Ty>& c)
  {
   alloc = c.alloc;
   sizenum = c.sizenum;
         root = alloc.allocate(sizenum);
   uninitialized_copy(c.root, c.root+sizenum,
                   root);       
   return c;
  }
     virtual void output(ostream& strem) const
     {
         Ty* pout = root;
         for(size_t i = 0; i < sizenum; ++i)
         {
             strem << *pout << " ";
             ++pout;
         }
     }   
  private:
      Allocator alloc;
      size_t    sizenum;
      Ty*       root;          
};

int main(void)
{
    coll<int> c(10,100);
    coll<int> a(c);
    coll<int> b;
    b = a;
    b.output(cout);
    cin.get();
    return 0;
}

本文地址:http://com.8s8s.com/it/it24400.htm