• 实现C++标准库string类的简单版本


      代码如下:

      1 #ifndef STRING_H
      2 #define STRING_H
      3 
      4 #include <cassert>
      5 #include <utility>
      6 #include <iostream>
      7 
      8 namespace jz
      9 {
     10 
     11     /************************************************************************/
     12     /* 重新实现C风格字符串处理函数                                          */
     13     /************************************************************************/
     14 
     15     //求C风格字符串长度
     16     size_t StrLen(const char *str)
     17     {
     18         assert(str != nullptr);
     19         size_t len = 0;
     20         while (*str++ != '')
     21         {
     22             ++len;
     23         }
     24         return len;
     25     }
     26 
     27     //复制C风格字符串
     28     char* StrCpy(char *dest, const char *src)
     29     {
     30         assert(dest != nullptr && src != nullptr);
     31         char *temp = dest;
     32         while ((*temp++ = *src++) != '');
     33         return dest;
     34     }
     35 
     36     //复制指定长度字符串
     37     char* StrNCpy(char *dest, const char *src, size_t n)
     38     {
     39         assert(dest != nullptr && src != nullptr);
     40         char *temp = dest;
     41         while (n-- && (*temp++ = *src++) != '');
     42         if (*temp != '')
     43         {
     44             *temp = '';
     45         }
     46         return dest;
     47     }
     48 
     49     //拼接字符串
     50     char* StrCat(char *dest, const char *src)
     51     {
     52         assert(dest != nullptr && src != nullptr);
     53         char *temp = dest;
     54         while (*temp != '')
     55         {
     56             ++temp;
     57         }
     58         while ((*temp++ = *src++) != '');
     59         return dest;
     60     }
     61 
     62     //比较字符串
     63     int StrCmp(const char *lhs, const char *rhs)
     64     {
     65         assert(lhs != nullptr && rhs != nullptr);
     66         int ret = 0;
     67         while (!(ret = *lhs - *rhs) && *rhs)
     68         {
     69             ++lhs;
     70             ++rhs;
     71         }
     72         if (ret > 0)
     73         {
     74             return 1;
     75         }
     76         else if (ret < 0)
     77         {
     78             return -1;
     79         }
     80         else
     81         {
     82             return 0;
     83         }
     84     }
     85 
     86     /************************************************************************/
     87     /* 实现标准库String类                                                   */
     88     /************************************************************************/
     89     class String
     90     {
     91         //重载==运算符
     92         friend bool operator==(const String &lhs, const String &rhs)
     93         {
     94             if (StrCmp(lhs.str, rhs.str) == 0)
     95             {
     96                 return true;
     97             }
     98             else
     99             {
    100                 return false;
    101             }
    102         }
    103 
    104         //重载!=运算符
    105         friend bool operator!=(const String &lhs, const String &rhs)
    106         {
    107             return !(lhs == rhs);
    108         }
    109 
    110         //重载+运算符
    111         friend String operator+(const String &lhs, const String &rhs)
    112         {
    113             String temp;
    114             delete[] temp.str;
    115             temp.str = new char[lhs.Size() + rhs.Size() + 1];
    116             StrCpy(temp.str, lhs.CStr());
    117             StrCat(temp.str, rhs.CStr());
    118             return temp;
    119         }
    120 
    121         //重载<<运算符
    122         friend std::ostream& operator<<(std::ostream &os, const String &rhs)
    123         {
    124             os << rhs.str;
    125             return os;
    126         }
    127 
    128     public:
    129         //默认构造函数
    130         String() : str(new char[1])
    131         {
    132             *str = '';
    133         }
    134 
    135         //C风格字符串作为参数的构造函数
    136         String(const char *cstr) : str(new char[StrLen(cstr) + 1])
    137         {
    138             StrCpy(str, cstr);
    139         }
    140 
    141         //拷贝构造函数(委托构造函数)
    142         String(const String &rhs) : String(rhs.str)
    143         {
    144 
    145         }
    146 
    147         //拷贝赋值运算符,使用传值参数,保证异常安全并可自我赋值
    148         String& operator=(String rhs)
    149         {
    150             Swap(rhs);
    151             return *this;
    152         }
    153 
    154         //析构函数
    155         ~String()
    156         {
    157             delete[] str;
    158         }
    159 
    160         //重载+=运算符
    161         String& operator+=(const String &rhs)
    162         {
    163             *this = *this + rhs;
    164             return *this;
    165         }
    166 
    167         //重载[]运算符
    168         char& operator[](size_t i)
    169         {
    170             assert(i >= 0 && i <= StrLen(str));
    171             return str[i];
    172         }
    173 
    174         //求字符串长度
    175         size_t Size() const
    176         {
    177             return StrLen(str);
    178         }
    179 
    180         //返回C风格字符串
    181         const char* CStr() const
    182         {
    183             return str;
    184         }
    185 
    186         //交换
    187         void Swap(String &rhs)
    188         {
    189             std::swap(str, rhs.str);
    190         }
    191 
    192     private:
    193         char *str;
    194     };
    195 
    196 }
    197 
    198 #endif
  • 相关阅读:
    Hitachi Programming Contest 2020 E Odd Sum Rectangle
    CF1060F Shrinking Tree
    UR #19
    AGC041F Histogram Rooks
    JOISC2020 Legendary Dango Maker
    Dinic 二分图匹配 / Hopcroft-Karp 算法 复杂度简单证明
    Codechef March Challenge 2020 Division 1 BREAK
    Tomorrow will be fine.
    JOISC2019 穿越时空 Bitaro
    POI2011 Periodicity
  • 原文地址:https://www.cnblogs.com/jzincnblogs/p/5316487.html
Copyright © 2020-2023  润新知