<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      c++簡單String類實現(xiàn)

       

      #include <iostream>
      #include <string>
      using namespace std;
      
      class String
      {
      public:
          String(const char* str = NULL);//通用構(gòu)造函數(shù),String("abc")
          String(const String &str);//拷貝構(gòu)造
          ~String();
      
          String& operator=(const String &str);//賦值運算符。返回引用
          String operator+(const String &str) const;
          String& operator+=(const String &str);//+=操作符。返回引用
          char& operator[](int n) const;//下標(biāo)操作符。返回引用
          bool operator==(const String &str) const;
      
          int size() const;//字符串實際大小,不包括結(jié)束符
          const char *c_str() const;//將string轉(zhuǎn)為char *
      
      private:
          char *data;
          int length;
      };
      
      String::String(const char* str)//通用構(gòu)造
      {
          if (!str)
          {//為空。String a()
              length = 0;
              data = new char[1];
              *data = '\0';
          }
          else
          {
              length = strlen(str);
              data = new char[length + 1];
              strcpy(data, str);//會拷貝源的結(jié)束符
          }
      }
      
      
      String::String(const String &str)//拷貝構(gòu)造,深拷貝
      {
          length = str.size();
          data = new char[length + 1];
          strcpy(data, str.c_str());
      }
      
      String::~String()
      {
          delete[] data;
          length = 0;
      }
      
      String& String::operator=(const String &str)//賦值操作符4步
      {
          if (this == &str) return *this;//1 自我賦值,返回自身引用
      
          delete[] data;//2 刪除原有數(shù)據(jù)
          
          length = str.size();//3 深拷貝
          data = new char[length + 1];
          strcpy(data, str.c_str());
      
          return *this;//4 返回自身引用
      }
      String String::operator+(const String &str) const//+操作符3步
      {//新建對象包括新空間,拷貝兩個數(shù)據(jù),返回新空間
          String newString;
          newString.length = length + str.size();
          newString.data = new char[newString.length + 1];
          strcpy(newString.data, data);
          strcat(newString.data, str.data);
          return newString;
      }
      
      String& String::operator+=(const String &str)//+=操作符5步
      {//重分配新空間,拷貝兩個數(shù)據(jù),刪除自己原空間,賦值為新空間,返回引用
          length += str.size();//成員length是實際長度
          char *newdata = new char[length + 1];
          strcpy(newdata, data);
          strcat(newdata, str.c_str());
          delete[] data;
          data = newdata;
          return *this;
      }
      
      char& String::operator[](int n) const
      {//下標(biāo)操作符,返回引用
          if (n >= length) return data[length - 1];//如果越界,返回最后一個字符
          else return data[n];
      }
      
      bool String::operator==(const String &str) const
      {
          if (length != str.size()) return false;
          return strcmp(data, str.c_str()) ? false : true;
      }
      
      int String::size() const
      {
          return length;
      }
      
      const char *String::c_str() const
      {
          return data;
      }
      
      int main()
      {
          char a[] = "Hello", b[] = "World!";
          String s1(a), s2(b);
          cout << s1.c_str() << endl;
          cout << s2.c_str() << endl;
          s1 += s2;
          cout << s1.c_str() << endl;
          s1 = s2;
          cout << s1.c_str() << endl;
          cout << (s1 + s2).c_str() << endl;
          cout << s1.size() << endl;
          cout << s1[1] << endl;
      
          if (s1 == s2)
              cout << "相等" << endl;
      }

       

      posted @ 2019-05-23 20:20  前進的code  閱讀(11920)  評論(1)    收藏  舉報
      主站蜘蛛池模板: 1区2区3区4区产品不卡码网站 | 国产AV福利第一精品| 国产美女裸身网站免费观看视频| 亚洲国产成人va在线观看天堂 | 国产国语一级毛片| 开心五月深深爱天天天操| 免费av网站| a片免费视频在线观看| 亚洲免费观看一区二区三区| 欧美性色黄大片www喷水| 日本欧美大码aⅴ在线播放| 国产精品亚洲欧美大片在线看| 亚洲精品综合网中文字幕| 国自产拍偷拍精品啪啪模特| 亚洲乱熟女一区二区三区| 亚洲高潮喷水无码AV电影| 女人扒开的小泬高潮喷小| 国产第一页浮力影院入口| 国产精品久久久久影院老司| 国产精品无码av在线一区| 色窝窝免费播放视频在线| 青草青草久热精品视频在线播放 | 国产精品激情av在线播放| 天天澡日日澡狠狠欧美老妇| 丁香婷婷综合激情五月色| 91久久天天躁狠狠躁夜夜| 日韩女同在线二区三区| 少妇被黑人到高潮喷出白浆| 久久精品国产亚洲av亚| 久久综合国产精品一区二区| 色综合久久久久综合体桃花网| 亚洲综合久久一区二区三区| 国产极品精品自在线不卡| 亚洲狠狠婷婷综合久久久| 国产线播放免费人成视频播放| 成人无码潮喷在线观看| 粉嫩一区二区三区粉嫩视频| 成人免费亚洲av在线| 国内精品久久久久精免费| 亚洲精品无码久久毛片| 久久久一本精品99久久精品36|