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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

string实现

發布時間:2025/4/16 10 豆豆
生活随笔 收集整理的這篇文章主要介紹了 string实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include<iostream> using namespace std;class String{friend ostream& operator<< (ostream&,String&); public:String(const char* str=NULL); //賦值構造兼默認構造函數(char)String(const String &other); //賦值構造函數(String)String& operator=(const String&other); //operator=String operator+(const String &other)const; //operator+bool operator==(const String&); //operator==char& operator[](unsigned int); //operator[]size_t size(){return strlen(m_data);};~String(void) {delete[] m_data;} private:char *m_data; }; inline String::String(const char* str) {if (!str) m_data=0;else{m_data = new char[strlen(str)+1];strcpy(m_data,str);} } inline String::String(const String& other) {if(!other.m_data) m_data=0;else{m_data=new char[strlen(other.m_data)+1];strcpy(m_data,other.m_data);} } inline String& String::operator=(const String& other) {if (this!=&other){delete[] m_data;if(!other.m_data) m_data=0;else{m_data = new char[strlen(other.m_data)+1];strcpy(m_data,other.m_data);}}return *this; } inline String String::operator+(const String &other)const {String newString;if(!other.m_data)newString = *this;else if(!m_data)newString = other;else{newString.m_data = new char[strlen(m_data)+strlen(other.m_data)+1];strcpy(newString.m_data,m_data);strcat(newString.m_data,other.m_data);}return newString; } inline bool String::operator==(const String &s) {if ( strlen(s.m_data) != strlen(m_data) )return false;return strcmp(m_data,s.m_data)?false:true; } inline char& String::operator[](unsigned int e) {if (e>=0&&e<=strlen(m_data))return m_data[e]; } ostream& operator<<(ostream& os,String& str) {os << str.m_data;return os; } void main() {String str1="Hello!";String str2="Teacher!";String str3 = str1+str2;cout<<str3<<"\n"<<str3.size()<<endl;cin.ignore(100, '\n');cin.get(); }

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。