152.字符串模板库

  • string.h
     1 #pragma once
     2 #include <string>
     3 #include <cstdlib>
     4 #include <functional>
     5 using namespace std;
     6 
     7 class outofrange
     8 {
     9 
    10 };
    11 
    12 template <class T>
    13 class String
    14 {
    15 public:
    16     //构造
    17     String();
    18     //析构
    19     ~String();
    20     //拷贝构造
    21     String(const String &str);
    22     //字符串构造
    23     String(const T *p);
    24     //重载=
    25     void operator=(const String &str);
    26     //重载+=
    27     void operator+=(const String &str);
    28     //重载+
    29     String operator+(const String &str);
    30     //重载[]
    31     //T operator[] (int id) const;
    32     //重载[],返回的是副本
    33     T& operator[] (int id) const;
    34     //重载宽字符输出
    35     template<class T>
    36     friend wostream & operator << (wostream &out, const String<T> &str);
    37 
    38     //重载窄字符输出
    39     template<class T>
    40     friend ostream & operator << (ostream &out, const String<T> &str);
    41 
    42     template<class T>
    43     friend istream & operator >>(istream &i, const String<T> &str);
    44     void replacefirst(const String &oldstr,const String &newstr);
    45     /*void replaceall(const String &oldstr, const String &newstr);
    46 
    47     void findfirst(const String &oldstr, const String &newstr);
    48     void findall(const String &oldstr, const String &newstr)
    49     void insertfirst(const String &oldstr, const String &newstr)
    50     void insertall(const String &oldstr, const String &newstr)
    51     void deletefirst(const String &oldstr, const String &newstr)
    52     void deleteall(const String &oldstr, const String &newstr)
    53     ;*/
    54 
    55     bool operator ==(const String &str);
    56     bool operator !=(const String &str);
    57     bool operator >=(const String &str);
    58     bool operator <=(const String &str);
    59     bool operator >(const String &str);
    60     bool operator <(const String &str);
    61     T* operator*();
    62     
    63     //重载->
    64     String* operator->();
    65     //重载->*
    66     String* operator->*(int i);
    67     T *getdata();
    68     //获取长度
    69     int getlength();
    70 private:
    71     T * pstart;
    72     int length;
    73     //等号默认自带this指针
    74     function<void(void)> fun = [=]() {cout << pstart << endl; };
    75 };
  • string.cpp
      1 #include "String.h"
      2 
      3 template<class T>
      4 inline String<T>::String() :pstart(nullptr), length(0)
      5 {
      6     if (strcmp(typeid(T).name(), "char") == 0)
      7     {
      8 
      9     }
     10     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
     11     {
     12 
     13     }
     14     else
     15     {
     16         cout << "类型不匹配,请认真检查" << endl;;
     17     }
     18 }
     19 
     20 template<class T>
     21 String<T>::~String()
     22 {
     23     if (this->pstart != nullptr)
     24     {
     25         delete[] this->pstart;
     26     }
     27 }
     28 
     29 template<class T>
     30 String<T>::String(const String & str)
     31 {
     32     if (this->pstart != nullptr)
     33     {
     34         delete[] this->pstart;
     35     }
     36     //长度归零
     37     this->length = 0;
     38 
     39     this->length = str.length;
     40     this->pstart = new T[this->length + 1 ];
     41     if (strcmp(typeid(T).name(), "char") == 0)
     42     {
     43         strcpy(reinterpret_cast<char *>(this->pstart), reinterpret_cast<const char *>(str.pstart));
     44     }
     45     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
     46     {
     47         wcscpy(reinterpret_cast<wchar_t *>(this->pstart), reinterpret_cast<const wchar_t *>(str.pstart));
     48     }
     49 }
     50 
     51 template<class T>
     52 String<T>::String(const T * p)
     53 {
     54     if (strcmp(typeid(T).name(), "char") == 0)
     55     {
     56         this->length = strlen(reinterpret_cast<const char*>(p));
     57         this->pstart = new T[this->length + 1 ];
     58         //初始化
     59         strcpy(reinterpret_cast<char*>(this->pstart), reinterpret_cast<const char*>(p));
     60     }
     61     else if(strcmp(typeid(T).name(), "wchar_t") == 0)
     62     {
     63         this->length = wcslen(reinterpret_cast<const wchar_t*>(p));
     64         this->pstart = new T[this->length + 1 ];
     65         wcscpy(reinterpret_cast<wchar_t*>(this->pstart), reinterpret_cast<const wchar_t*>(p));
     66     }
     67     else
     68     {
     69         cout << "类型不匹配,请认真检查" << endl;;
     70     }
     71 }
     72 
     73 template<class T>
     74 void String<T>::operator=(const String & str)
     75 {
     76     if (this->pstart != nullptr)
     77     {
     78         delete[] this->pstart;
     79     }
     80     //长度归零
     81     this->length = 0;
     82 
     83     this->length = str.length;
     84     this->pstart = new T[this->length + 1];
     85     if (strcmp(typeid(T).name(), "char") == 0)
     86     {
     87         strcpy(reinterpret_cast<char *>(this->pstart), reinterpret_cast<const char *>(str.pstart));
     88     }
     89     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
     90     {
     91         wcscpy(reinterpret_cast<wchar_t *>(this->pstart), reinterpret_cast<const wchar_t *>(str.pstart));
     92     }
     93 }
     94 
     95 template<class T>
     96 void String<T>::operator+=(const String & str)
     97 {
     98     this->length += str.length;
     99     T *temp = new T[this->length + 1];
    100     if (strcmp(typeid(T).name(), "char") == 0)
    101     {
    102         strcpy(reinterpret_cast<char*>(temp), reinterpret_cast<const char*>(this->pstart));
    103         strcat(reinterpret_cast<char*>(temp), reinterpret_cast<const char*>(str.pstart));
    104         delete[] this->pstart;
    105         this->pstart = temp;
    106     }
    107     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
    108     {
    109         wcscpy(reinterpret_cast<wchar_t*>(temp), reinterpret_cast<const wchar_t*>(this->pstart));
    110         wcscat(reinterpret_cast<wchar_t*>(temp), reinterpret_cast<const wchar_t*>(str.pstart));
    111         delete[] this->pstart;
    112         this->pstart = temp;
    113     }
    114     else
    115     {
    116         cout << "类型不匹配,请认真检查" << endl;;
    117     }
    118 }
    119 
    120 template<class T>
    121 String<T> String<T>::operator+(const String & str)
    122 {
    123     String<T> temp;
    124     temp.length = this->length + str.length;
    125     temp.pstart = new T[temp.length+1];
    126 
    127     if (strcmp(typeid(T).name(), "char") == 0)
    128     {
    129         strcpy(reinterpret_cast<char*>(temp.pstart), reinterpret_cast<const char*>(this->pstart));
    130         strcat(reinterpret_cast<char*>(temp.pstart), reinterpret_cast<const char*>(str.pstart));
    131     }
    132     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
    133     {
    134         wcscpy(reinterpret_cast<wchar_t*>(temp.pstart), reinterpret_cast<const wchar_t*>(this->pstart));
    135         wcscat(reinterpret_cast<wchar_t*>(temp.pstart), reinterpret_cast<const wchar_t*>(str.pstart));
    136     }
    137     else
    138     {
    139         cout << "类型不匹配,请认真检查" << endl;;
    140     }
    141     return temp;
    142 }
    143 
    144 //template<class T>
    145 //T String<T>::operator[](int id) const 
    146 //{
    147 //    if (id < 0 || id >= this->length)
    148 //    {
    149 //        //抛出匿名异常
    150 //        throw outofrange();
    151 //    }
    152 //    return this->pstart[id];
    153 //}
    154 
    155 template<class T>
    156 T&  String<T>::operator[] (int id) const
    157 {
    158     if (id < 0 || id >= this->length)
    159     {
    160         //抛出匿名异常
    161         throw outofrange();
    162     }
    163     return this->pstart[id];
    164 }
    165 
    166 template<class T>
    167 void String<T>::replacefirst(const String & oldstr, const String & newstr)
    168 {
    169     if (strcmp(typeid(T).name(), "char") == 0)
    170     {
    171         char *p = strstr(reinterpret_cast<char*>(this->pstart), reinterpret_cast<char*>(oldstr.pstart));
    172         if (p != nullptr)
    173         {
    174             int oldlength = strlen(reinterpret_cast<char*>(oldstr.pstart));
    175             int newlength = strlen(reinterpret_cast<char*>(newstr.pstart));
    176             if (oldlength == newlength)
    177             {
    178                 memcpy(p, newstr.pstart, newstr.length);
    179             }
    180             else if (oldlength > newlength)
    181             {
    182                 memcpy(p, newstr.pstart, newstr.length);
    183                 char *pstr = (char *)(p +  strlen(reinterpret_cast<char*>(newstr.pstart)));
    184                 int mlength = oldlength - newlength;
    185                 while (*(pstr + mlength) != '')
    186                 {
    187                     *pstr = *(pstr + mlength);
    188                     pstr++;
    189                 }
    190                 *pstr = '';
    191             }
    192             else
    193             {
    194                 //分配内存与拷贝
    195                 T* ptemp = new T[(this->length + newlength + 1) * sizeof(T)];
    196                 memcpy(ptemp, this->pstart, sizeof(T)*(p - reinterpret_cast<char*>(this->pstart)));
    197                 memcpy(ptemp + (p - reinterpret_cast<char*>(this->pstart)), newstr.pstart, sizeof(T)*newlength);
    198                 memcpy(ptemp + (p - reinterpret_cast<char*>(this->pstart) + newlength), p + oldlength, sizeof(T)*(this->length - oldlength - (p - reinterpret_cast<char*>(this->pstart)) + 1));
    199                 this->pstart = ptemp;
    200             }
    201         }
    202     }
    203     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
    204     {
    205         wchar_t *p = wcsstr(reinterpret_cast<wchar_t *>(this->pstart), reinterpret_cast<wchar_t *>(oldstr.pstart));
    206         if (p != nullptr)
    207         {
    208             int oldlength = wcslen(reinterpret_cast<wchar_t*>(oldstr.pstart));
    209             int newlength = wcslen(reinterpret_cast<wchar_t*>(newstr.pstart));
    210             if (oldlength == newlength)
    211             {
    212                 memcpy(p, newstr.pstart, newstr.length);
    213             }
    214             else if (oldlength > newlength)
    215             {
    216                 memcpy(p, newstr.pstart, newstr.length);
    217                 wchar_t *pstr = (wchar_t *)(p + wcslen(reinterpret_cast<wchar_t*>(newstr.pstart)));
    218                 int mlength = oldlength - newlength;
    219                 while (*(pstr + mlength) != '')
    220                 {
    221                     *pstr = *(pstr + mlength);
    222                     pstr++;
    223                 }
    224                 *pstr = '';
    225             }
    226             else
    227             {
    228                 //分配内存与拷贝
    229                 T* ptemp = new T[(this->length + newlength + 1) * sizeof(T)];
    230                 memcpy(ptemp, this->pstart, sizeof(T)*(p - reinterpret_cast<wchar_t*>(this->pstart)));
    231                 memcpy(ptemp + (p - reinterpret_cast<wchar_t*>(this->pstart)), newstr.pstart, sizeof(T)*newlength);
    232                 memcpy(ptemp + (p - reinterpret_cast<wchar_t*>(this->pstart) + newlength), p + oldlength, sizeof(T)*(this->length - oldlength - (p - reinterpret_cast<wchar_t*>(this->pstart)) + 1));
    233                 this->pstart = ptemp;
    234             }
    235         }
    236     }
    237     else
    238     {
    239         cout << "类型不匹配,请认真检查" << endl;;
    240     }
    241 }
    242 
    243 template<class T>
    244 bool String<T>::operator==(const String & str)
    245 {
    246     if (strcmp(typeid(T).name(), "char") == 0)
    247     {
    248         return( strcmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) == 0);
    249     }
    250     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
    251     {
    252         return (wcscmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) == 0);
    253     }
    254     else
    255     {
    256         cout << "类型不匹配,请认真检查" << endl;;
    257     }
    258 }
    259 
    260 template<class T>
    261 bool String<T>::operator!=(const String & str)
    262 {
    263     if (strcmp(typeid(T).name(), "char") == 0)
    264     {
    265         return(strcmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) != 0);
    266     }
    267     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
    268     {
    269         return (wcscmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) != 0);
    270     }
    271     else
    272     {
    273         cout << "类型不匹配,请认真检查" << endl;;
    274     }
    275 }
    276 
    277 template<class T>
    278 bool String<T>::operator>=(const String & str)
    279 {
    280     if (strcmp(typeid(T).name(), "char") == 0)
    281     {
    282         return(strcmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) >= 0);
    283     }
    284     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
    285     {
    286         return (wcscmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) >= 0);
    287     }
    288     else
    289     {
    290         cout << "类型不匹配,请认真检查" << endl;;
    291     }
    292 }
    293 
    294 template<class T>
    295 bool String<T>::operator<=(const String & str)
    296 {
    297     if (strcmp(typeid(T).name(), "char") == 0)
    298     {
    299         return(strcmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) <= 0);
    300     }
    301     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
    302     {
    303         return (wcscmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) <= 0);
    304     }
    305     else
    306     {
    307         cout << "类型不匹配,请认真检查" << endl;;
    308     }
    309 }
    310 
    311 template<class T>
    312 bool String<T>::operator>(const String & str)
    313 {
    314     if (strcmp(typeid(T).name(), "char") == 0)
    315     {
    316         return(strcmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) > 0);
    317     }
    318     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
    319     {
    320         return (wcscmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) > 0);
    321     }
    322     else
    323     {
    324         cout << "类型不匹配,请认真检查" << endl;;
    325     }
    326 }
    327 
    328 template<class T>
    329 bool String<T>::operator<(const String & str)
    330 {
    331     if (strcmp(typeid(T).name(), "char") == 0)
    332     {
    333         return(strcmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) < 0);
    334     }
    335     else if (strcmp(typeid(T).name(), "wchar_t") == 0)
    336     {
    337         return (wcscmp(reinterpret_cast<char *>(this->pstart), reinterpret_cast<char *>(str.pstart)) < 0);
    338     }
    339     else
    340     {
    341         cout << "类型不匹配,请认真检查" << endl;;
    342     }
    343 }
    344 
    345 template<class T>
    346 T * String<T>::operator*()
    347 {
    348     return this->pstart;
    349 }
    350 
    351 template<class T>
    352 String<T> * String<T>::operator->()
    353 {
    354     return this;
    355 }
    356 
    357 template<class T>
    358 String<T> * String<T>::operator->*(int i)
    359 {
    360     return this;
    361 }
    362 
    363 template<class T>
    364 T * String<T>::getdata()
    365 {
    366     return this->pstart;
    367 }
    368 
    369 template<class T>
    370 int String<T>::getlength()
    371 {
    372     return this->length;
    373 }
    374 
    375 template<class T>
    376 wostream & operator<<(wostream & out, const String<T>& str)
    377 {
    378         out << str.pstart << endl;
    379         return out;    
    380 }
    381 
    382 template<class T>
    383 ostream & operator<<(ostream & out, const String<T>& str)
    384 {
    385     out << str.pstart;
    386     return out;
    387 }
    388 
    389 template<class T>
    390 istream & operator>>(istream & i, const String<T> & str)
    391 {
    392     // TODO: 在此处插入 return 语句
    393     i >> str.pstart;
    394     return i;
    395 }
  • test.cpp
     1 #define _CRT_SECURE_NO_WARNINGS
     2 
     3 #include <iostream>
     4 #include <cstdlib>
     5 #include "String.h"
     6 #include "String.cpp"
     7 #include <locale>
     8 using namespace std;
     9  
    10 void main()
    11 {
    12     ////要输出宽字符,要加上这句话
    13     //wcout.imbue(locale("", LC_CTYPE));
    14 
    15     //String<char> str("hello");
    16 
    17     //cout << str << endl;
    18     //String<wchar_t> str2(L"你好");
    19     //wcout << str2;
    20 
    21     //String<char> str3(str);
    22     //cout << str3 << endl;
    23 
    24     //str += str3;
    25     //cout << str << endl;
    26 
    27     //String<char> str4 = str3 + str;
    28     //cout << str4 << endl;
    29 
    30     //String<wchar_t> str5(L"吗");
    31     //str2 += str5;
    32     //wcout << str2 << endl;
    33 
    34     String<wchar_t> str(L"hello");
    35 
    36     /*cout << *str << endl;
    37 
    38     cout << str->getdata() << endl;
    39 
    40     cout << (str->*3)->getdata() << endl;*/
    41     str.replacefirst(L"he", L"tuu");
    42     wcout << str << endl;
    43     cin.get();
    44 }
原文地址:https://www.cnblogs.com/xiaochi/p/8710480.html