c++制作string类

    科技2022-07-11  92

    c++ string 简易版

    #include<iostream> using namespace std; //检测字符串长度 int strlen(const char *p){ int size = 0; while(*p++){++size;} return size; } //字符串连接 char* strcat(char a[], const char b[]) { char* buff1 = a; const char* buff2 = b; while(*buff1){ buff1++; } while(*buff2){ *buff1++ = *buff2++; } buff1[strlen(buff2)] = '\0'; return a; } //对字符串进行拷贝 inline void strcpy(char *buff1, const char *buff2){ while(*buff2){ *buff1++ = *buff2++; } buff1[strlen(buff2)] = '\0'; } //寻找字符 int strfind(const char* a, const char b){ int size = 0; const char *p = a; while(*p++){ ++size; if(*p == b)return size; } return size; } //字符串匹配 bool strequal(const char* a, const char b[]){ if(strlen(a) != strlen(b)){ return false; } int size = strlen(a); for(int i=0;i<size+1;i++){ if(a[i] != b[i]){ return false; } } return true; } class String { inline char* c_str() const { return str; } public: inline int len()const{ return strlen(this->str); } explicit String(const char* p = nullptr) { if (nullptr != p) { str = new char[strlen(p) + 1]; strcpy(str, p); } else { str = new char[1]; *str = '\0'; } } inline int find(const char a){ return strfind(this->str, a); } String(const String& a); inline ~String() { delete[] str; } bool operator == ( const char b[]){ return strequal(this->str, b); } bool operator == (const String &a){ return strequal(this->str, a.str); } String &operator = (const char a[]) { delete[] str;//??????????????????char?????????????? ?????? str = new char[strlen(a) + 1];//???????????????? strcpy(str, a); return *this; } String& operator += (const String& a){ String f(*this), f2(a);//临时对象拿自己当模板做一个临时对象 delete this->str; this->str = new char[strlen(f.str)+strlen(f2.str)-1]; strcpy(this->str, f.str); strcpy(this->str+strlen(f.str), f2.str); return *this; } String& operator += (const char a[]){ String f(*this);//临时对象拿自己当模板做一个临时对象 delete this->str; this->str = new char[strlen(f.str)+strlen(a)-1]; strcpy(this->str, f.str); strcpy(this->str+strlen(f.str), a); return *this; } //实现相加 String operator + (const String& a){ String f; f.str = new char[strlen(f.str)+strlen(a.str)-1]; strcpy(f.str, this->str); strcpy(f.str+strlen(this->str), a.str);//对已经赋值的区域避开, 再去把指针指向this->str的结尾对a.str进行拷贝 return f; } String operator + (const char a[]){ String f; f.str = new char[strlen(this->str)+strlen(a)-1]; strcpy(f.str, this->str); strcpy(f.str+strlen(this->str), a);//对已经赋值的区域避开, 再去把指针指向this->str的结尾对a进行拷贝 return f; } inline char operator[] (int size){ if(strlen(this->str) < size) {return '\0';} return str[size]; } String& operator = (const String& a); inline friend ostream& operator<<(ostream& os, const String& a) { return os << a.c_str(); } inline friend istream& operator >> (istream &in, String &a){ char* snew = new char; *snew = '\0'; in>>snew; a = snew; *snew = '\0'; delete snew; return in; } String &operator ()(const String &a); inline int getline(){ size_t size = strlen(this->str);//记录结束位置 scanf("%[^\n]", this->str); String b(this->str); this->str[size] = '\0'; *this = b; return b.len(); } private: char* str; }; String& String::operator ()(const String &a){ if(&a == this){ return *this; } delete[] this->str; this->str = new char[this->len()+1]; strcpy(this->str, a.str); return *this; } String& String::operator=(const String& a) { if (&a == this) { return *this; } delete[] this->str; str = new char[strlen(a.str) + 1]; strcpy(this->str, a.str); return *this; } inline String::String(const String& a) { str = new char[strlen(a.str) + 1]; strcpy(str, a.str); }

    实现了基本的加, 判断, 输入等.

    感谢您对此代码的指点 !

    Processed: 0.020, SQL: 8