一个类似vector的类

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; class StrVec { public: StrVec () : elements (nullptr), first_free (nullptr), cap (nullptr) {}; //add a construct function receive initializer_list 
        
          StrVec(initializer_list 
         
           ); StrVec(const StrVec&); StrVec(StrVec &&)_NOEXCEPT; StrVec &operator=(initializer_list 
          
            ); StrVec &operator=(const StrVec&); StrVec &operator=(StrVec&&); string &operator[](size_t); const string &operator[](size_t)const; ~StrVec (); void push_back (const string&); void push_back (string&&); size_t size ()const { return first_free - elements; } size_t capacity ()const { return cap - elements; } string *begin ()const { return elements; } string *end ()const { return first_free; } private: allocator 
           
             alloc; void cnk_n_alloc () { if (size () == capacity ())reallocate (); } pair 
            
              alloc_n_copy (const string*, const string*); void free (); void reallocate (); string *elements; string *first_free; string *cap; }; StrVec::StrVec (StrVec&& sv)_NOEXCEPT { elements = sv.elements; first_free = sv.first_free; cap = sv.cap; sv.elements = sv.cap = sv.first_free = nullptr; } StrVec &StrVec::operator=(StrVec &&rhs) { if (this != &rhs) { free (); elements = rhs.elements; first_free = rhs.first_free; cap = rhs.cap; rhs.elements = rhs.cap = rhs.first_free = nullptr; } return *this; } void StrVec::push_back (const string &s) { cnk_n_alloc (); alloc.construct (first_free++, s); } void StrVec::push_back (string &&s) { cnk_n_alloc (); alloc.construct (first_free++, std::move (s)); } StrVec::StrVec (initializer_list 
             
               lst):elements (nullptr), first_free (nullptr), cap (nullptr) { for (const auto elem : lst) push_back (elem); } pair 
              
                StrVec::alloc_n_copy(const string *b, const string *e) { auto data = alloc.allocate (e - b); return{ data, uninitialized_copy (b, e, data) }; } void StrVec::free () { if (elements) for (auto q = first_free; q != elements; /*empty*/) alloc.destroy (--q); /* 另一种方法为: for_each (elements, first_free, [&] (string& elem){alloc.destroy (&elem);}); */ alloc.deallocate (elements, cap - elements); } StrVec::StrVec (const StrVec &rhs) { auto data = alloc_n_copy (rhs.begin (), rhs.end ()); elements = data.first; first_free = cap = data.second; } StrVec::~StrVec () { free (); } StrVec &StrVec::operator=(initializer_list 
               
                 lst) { auto data = alloc_n_copy (lst.begin (), lst.end ()); free (); elements = data.first; first_free = cap = data.second; return *this; } string &StrVec::operator[](size_t n) { return elements[n]; } const string &StrVec::operator[](size_t n)const { return elements[n]; } StrVec &StrVec::operator=(const StrVec &rhs) { auto data = alloc_n_copy (rhs.begin (), rhs.end ()); free (); elements = data.first; first_free = cap = data.second; return *this; } void StrVec::reallocate () { auto newcapacity = size () ? 2 * size () : 1; auto data = alloc.allocate (newcapacity); auto dest = data; auto elem = elements; for (size_t i = 0; i != size (); ++i) alloc.construct (dest++, move (*elem++)); /*另一种方法为: auto first = alloc.allocate(newcapacity); auto last = uninitialized_copy (make_move_iterator (begin ()), make_move_iterator (end ()), first); */ free (); elements = data; first_free = dest; cap = elements + newcapacity; } void print (string name, const StrVec &sv) { cout << name << ": "; for (auto sv_it = sv.begin (); sv_it != sv.end (); ++sv_it) cout << *sv_it << " "; cout << "\n----------------" << endl; } void StrVec_move_test () { //use initializer_list initialize it StrVec sv{ "fuck" }; string line; while (cin >> line) { sv.push_back (line); } print ("sv", sv); StrVec new_sv (std::move (sv)); print ("moved sv", sv); print ("The new sv", new_sv); sv = std::move (new_sv); print ("moved new sv", new_sv); print ("final sv", sv); } int main () { ios::sync_with_stdio (false); StrVec svec{ "fuck" }; const StrVec cvec = svec; //if svec have element, empty the first element if (svec.size () && svec[0].empty ()) { svec[0] = "Zero"; } return 0; } 
                
               
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值