生活随笔
收集整理的這篇文章主要介紹了
string类的基本实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://blog.csdn.net/qq_29503203/article/details/52265829
在面試中面試官常常會讓你寫出string類的基本操作,比如:構造函數,析構函數,拷貝構造等等.下面是除此之外的一些操作,希望可以幫助你更好的理解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=(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); 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;}
總結
以上是生活随笔為你收集整理的string类的基本实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。