zoukankan      html  css  js  c++  java
  • C++中STL中简单的Vector的实现

    该vector只能容纳标准库中string类,

    直接上代码了,StrVec.h文件内容为:

     1 #ifndef STRVEC_H
     2 #define STRVEC_H
     3 
     4 #include<iostream>
     5 #include<string>
     6 #include<memory>
     7 using namespace std;
     8 class StrVec{
     9 public:
    10     // 默认构造函数
    11     StrVec():elements(nullptr),first_free(nullptr),cap(nullptr)
    12     {
    13         
    14     }
    15     //拷贝构造函数
    16     StrVec(const StrVec& );
    17     //拷贝赋值运算符
    18     StrVec& operator=(const StrVec&);
    19     ~StrVec();
    20     void push_back(const string&);
    21     size_t size() const { return first_free - elements ; }
    22     size_t capacity() const {  return cap - elements  ;}
    23     string* begin() const { return elements ;}
    24     string* end() const { return first_free ;}
    25     
    26     
    27     
    28 private:
    29 static allocator<string> alloc;  // 分配元素
    30  // 被添加的元素的函数使用
    31  void chk_n_alloc()
    32  {
    33      if( size() == capacity() )
    34             reallocate();
    35 
    37  }
    38 // 工具函数,被拷贝构造函数、赋值运算符和析构函数所使用
    39 pair<string* ,string*> alloc_n_copy(const string* ,const string* );
    40 
    41   
    45 void free();
    46 void reallocate();
    47 string* elements; // 指向数组首元素的指针
    48 string* first_free; // 指向数组第一个空闲元素的指针
    49 string* cap; // 指向数组尾后的指针
    50 
    51     
    52 };
    53
    59 #endif

    StrVec.cpp文件内容为:

     1 #include "StrVec.h"
     2 
     3 std::allocator<string> StrVec::alloc;
     4 
     5 void StrVec::push_back( const string& s )
     6 {
     7     chk_n_alloc(); // 确保有空间容纳新元素
     8     // first_free指向的元素中构造s的副本
     9     alloc.construct(first_free++,s)  ; 
    10         
    11     
    12 }
    13 pair<string* ,string* > StrVec::alloc_n_copy(const string* b, const string* e )
    14 {
    15     auto data = alloc.allocate(e-b);
    16     return make_pair(data , uninitialized_copy(b,e,data)  );
    17     
    18     
    19 }
    20 void StrVec::free()
    21 {
    22     // 不能传递给deallcoate一个空指针
    23     if( elements)
    24     {
    25         for(auto p = first_free ; p != elements ; )
    26             alloc.destroy(--p);
    27         alloc.deallocate(elements,cap-elements) ;
    28     }
    29     
    30     
    31 }
    32 StrVec& StrVec::operator=( const StrVec& rhs )
    33 {
    34     auto data = alloc_n_copy(rhs.begin() , rhs.end() );
    35     free() ;
    36     elements = data.first ;
    37     first_free= cap=data.second;
    38     
    39     return *this ;
    40     
    41 }
    42 void StrVec::reallocate()
    43 {
    44     // 我们将分配当前大小两倍的内存空间;
    45     auto newcapacity  = size() ? 2*size() :1 ;
    46     //分配新内存
    47     auto newdata = alloc.allocate( newcapacity ) ;
    48     
    49     auto dest=newdata ;
    50     auto elem = elements ;
    51     for(size_t i=0; i != size() ; i++)
    52         alloc.construct(dest++,std::move( *elem++) );
    53     free();
    54     elements = newdata;
    55     first_free = dest ;
    56     cap = elements+ newcapacity ;
    57     
    58     
    59 }
    60 StrVec::~StrVec()
    61 {
    62     free();
    63 }


    测试代码为maintest.cpp

     1 #include "StrVec.h"
     2 
     3 int main()
     4 {
     5     StrVec vec1;
     6     vec1.push_back("ok1");
     7     vec1.push_back("ok2");
     8 
     9     auto begin = vec1.begin();
    10     auto end= vec1.end();
    11 
    12     while( begin != end )
    13     {
    14       cout<<*begin<<endl;
    15       // cout<<endl;
    16       begin++;
    17 
    18     }
    19 
    22  return 0;
    23 }
  • 相关阅读:
    水仙花数 题解
    数值统计 题解
    平方和和立方和 题解
    第几天? 题解
    Python网络爬虫——http和https协议
    Python网络爬虫——爬虫简介
    python学习——pandas的拼接操作
    python学习——pandas层次化索引
    python学习——pandas扩展:傅里叶变换
    python学习——pandas数据丢失处理
  • 原文地址:https://www.cnblogs.com/wanshuafe/p/5340122.html
Copyright © 2011-2022 走看看