stl smart指標

來源:互聯網
上載者:User
對於STL容器而言,並不直接支援這一特性(也就是:list<myint&>   mylist),  
  即便用raw   pointer(也就是普通的指標list<myint*>   mylist)也不能完全解決這個問題,  
  通常的解決方案是使用smart   pointer來替代普通的指標從而完全控制:  
  construction,destruction,copying,assignment,以及dereference。  
  具體內容恐怕要參考侯sir的書了。  
  我試著寫了一個,僅供參考:  
   
  #include   <list>  
  #include   <iostream>  
  #include   <functional>  
  //<string>  
  using   namespace   std;  
   
  struct   myint  
  {  
  int   key;  
  int   data;  
  //bool   operator   <   (const   myint&   _A){return   (data   <   _A.data);};  
  //bool   operator   >   (const   myint&   _A){return   (data   >   _A.data);};  
  myint::myint():key(0),data(0){};  
   
  friend   ostream&   operator   <<   (ostream&,   const   myint&);  
  friend   bool   operator   >   (const   myint&,   const   myint&);  
  friend   bool   operator   <   (const   myint&,   const   myint&);  
  };  
   
  ostream&   operator   <<   (ostream&   _O,   const   myint&   _C)  
  {  
  _O   <<   _C.key<<   "   "   <<   _C.data   <<   ",";  
  return   _O;  
  };  
   
  bool   operator   >   (const   myint&   _A,   const   myint&   _B)  
  {  
  return   (_A.data   >   _B.data);  
  };  
   
  bool   operator   <   (const   myint&   _A,   const   myint&   _B)  
  {  
  return   (_A.data   <   _B.data);  
  };  
   
  template   <class   T>  
  class   MySmartPt  
  {  
  public:  
  T*   ptr;  
  MySmartPt(T*   p   =   0):   ptr(p){}  
  MySmartPt(const   MySmartPt<T>&   p)   :   ptr(p.ptr){}  
  ~MySmartPt()   {}  
   
  MySmartPt<T>&   operator=   (const   MySmartPt<T>&   p)  
  {  
  if(this   !=   &p)  
  {  
  ptr   =   p.ptr;  
  }  
  return   *this;  
  };  
   
  T&   operator*   ()   const   {return   *ptr;};  
  T&   operator->   ()   const   {return   ptr;};  
  };  
   
  class   MyintPt   :   public   MySmartPt<myint>  
  {  
  public:  
  MyintPt(myint*   p)   :   MySmartPt<myint>(p){}  
  bool   operator   <   (const   MyintPt&   rhs)   const//  
  {return   (   ((*this).ptr)->data   <   (rhs.ptr)->data);}  
  bool   operator   >   (const   MyintPt&   rhs)   const  
  {return   (   ((*this).ptr)->data   >   (rhs.ptr)->data);}  
  };  
   
  int   main()  
  {  
  myint   array[10];  
  list<MyintPt>   mylist;  
   
  myint   temp;  
  for(int   i   =   0;   i   <   10;   ++i   )  
  {  
  temp.data   =   rand()%30;  
  array[i]   =   temp;  
  MyintPt   myintPt(&array[i]);  
  mylist.push_back(myintPt);  
  cout   <<   temp;  
  }  
   
  cout   <<   endl;  
   
  typedef   myint*   pmyint;    
  mylist.sort();  
   
  list<MyintPt>::iterator   iter;  
  for(iter   =   mylist.begin();   iter   !=   mylist.end();   ++iter)  
  cout   <<   *((*iter).ptr);  
   
  cout   <<   endl;  
   
  greater<MyintPt>   pt;  
  mylist.sort(pt);  
  for(iter   =   mylist.begin();   iter   !=   mylist.end();   ++iter)  
  cout   <<   *((*iter).ptr);  
   
  return   0;  

  }  

轉http://topic.csdn.net/t/20011112/10/366100.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.