日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

string类的基本实现

發(fā)布時間:2023/11/30 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 string类的基本实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

https://blog.csdn.net/qq_29503203/article/details/52265829

在面試中面試官常常會讓你寫出string類的基本操作,比如:構(gòu)造函數(shù),析構(gòu)函數(shù),拷貝構(gòu)造等等.下面是除此之外的一些操作,希望可以幫助你更好的理解string以便以后的運用:

  • String& operator=(const String& s);
  • char* c_str();
  • char& operator[](int index);
  • void PushBack(char c);
  • String operator+(const String& s);
  • String& operator+=(const String& s);
  • String& insert(int pos,const char* str);//在指定位置插入字符串
  • bool operator==(const String& s);



  • 為了讀者方便,下面給出完整代碼:

  • #include<iostream>
  • using namespace std;
  • #include<cstring>
  • #include<assert.h>
  • class String
  • {
  • friend ostream& operator<<(ostream& os,const String& s);
  • friend istream& operator>>(istream& is,String& s);
  • public:
  • String(const char* str=""):_sz(strlen(str))
  • ,_capacity(strlen(str)+1)
  • ,_str(new char[strlen(str)+1])
  • {
  • strcpy(_str,str);
  • }
  • String(const String& s):_sz(s._sz)
  • ,_capacity(strlen(s._str)+1)
  • ,_str(new char[strlen(_str)+1])
  • {
  • strcpy(_str,s._str);
  • }
  • ~String()
  • {
  • if(_str!=NULL)
  • {
  • delete[] _str;
  • _str=NULL;
  • _sz=0;
  • _capacity=0;
  • }
  • }
  • //String& operator=(const String& s)
  • //{
  • // if(this!=&s)
  • // {
  • // delete[] _str; //_str存放'\0',先將這塊空間釋放
  • // _str=new char[strlen(s._str)+1]; //再為_str開辟能存放s._str的足夠空間
  • // strcpy(_str,s._str);
  • // }
  • // return *this;
  • //}
  • String& operator=(String s)
  • {
  • std::swap(_str,s._str);
  • std::swap(_sz,s._sz);
  • std::swap(_capacity,s._capacity);
  • return *this;
  • }
  • char* c_str()
  • {
  • return _str;
  • }
  • char& operator[](int index)
  • {
  • return _str[index];
  • }
  • void PushBack(char c)
  • {
  • CheckCapacity(1);
  • _str[_sz]=c;
  • _sz++;
  • _str[_sz]='\0';
  • }
  • String operator+(const String& s)
  • {
  • String tmp;
  • tmp._str=new char[strlen(_str)+strlen(s._str)+1];
  • strcpy(tmp._str,_str);
  • strcat(tmp._str,s._str);
  • return tmp;
  • }
  • String& operator+=(const String& s)
  • {
  • char* tmp=_str;
  • _str=new char[strlen(_str)+strlen(s._str)+1];
  • if(NULL==tmp)
  • {
  • exit(EXIT_FAILURE);
  • }
  • strcpy(_str,tmp);
  • strcat(_str,s._str);
  • return *this;
  • }
  • String& insert(int pos,const char* str)//在指定位置插入字符串
  • {
  • assert(pos>=_sz); //條件為真繼續(xù)往下執(zhí)行
  • int len=strlen(str);
  • CheckCapacity(_sz+len+1);
  • int start=_sz;
  • while(start>=pos)
  • {
  • _str[start+1]=_str[start];
  • start--;
  • }
  • for(int i=0;i<len;i++)
  • {
  • _str[pos]=str[i];
  • pos++;
  • }
  • return *this;
  • }
  • bool operator==(const String& s)
  • {
  • if(strcmp(_str,s._str)==0)
  • {
  • return true;
  • }
  • else
  • return false;
  • }
  • private:
  • void CheckCapacity(int count)
  • {
  • if(_sz+count>=_capacity)
  • {
  • int newcapacity=(2*_capacity>_capacity+count)
  • ?(2*_capacity):(_capacity+count);
  • char* tmp=new char[newcapacity];
  • if(NULL==tmp)
  • {
  • exit(EXIT_FAILURE);
  • }
  • strcpy(tmp,_str);
  • delete[] _str;
  • _str=tmp;
  • _capacity=newcapacity;
  • }
  • }
  • private:
  • char* _str;
  • int _sz;
  • int _capacity;
  • };
  • ostream& operator<<(ostream& os,const String& s)
  • {
  • os<<s._str<<endl;
  • return os;
  • }
  • istream& operator>>(istream& is,String& s)
  • {
  • is>>s._str;
  • return is;
  • }
  • void test1()
  • {
  • String s1("abcdef");
  • String s2(s1);
  • String s3;
  • s3=s1;
  • cout<<s1<<endl;
  • cout<<s2<<endl;
  • cout<<s3<<endl;
  • }
  • void test2()
  • {
  • String s1="hello";
  • cout<<*(s1.c_str()+1)<<endl; //取出第二個字符
  • cout<<strlen(s1.c_str())<<endl;
  • cout<<s1[2]<<endl;
  • s1[4]='a';
  • cout<<s1<<endl;
  • }
  • void test3()
  • {
  • String s1="abcdef";
  • s1.PushBack('k');
  • cout<<s1<<endl;
  • }
  • void test4()
  • {
  • String s1("aacd");
  • String s2("mmnp");
  • String s3;
  • s3=s1+s2;
  • s1+=s2;
  • cout<<s1<<endl;
  • cout<<s3<<endl;
  • }
  • void test5()
  • {
  • String s="aaabb";
  • s.insert(2,"cd");
  • cout<<s.c_str()<<endl;
  • }
  • int main()
  • {
  • test5();
  • system("pause");
  • return 0;
  • }

  • 總結(jié)

    以上是生活随笔為你收集整理的string类的基本实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。