c++之封装——string类型的实现
2016-08-04 01:00
316 查看
以前在C语言中并没有string这个内置类型,但是到了c++里面就可以使用了,c++中可以定义string变量,对string类型的变量可以进行各种操作,包括相加、相减、赋值等等,就和int ,double类型相似,但string是一个字符串。要实现这些操作并不像C语言的内置类型一样那么简单,直接数据进行运算就可以。
string的实现得益与c++的封装特性,封装是c++的三大特性之一,string类型其实是在char*类型的基础上进行一层封装。其实质还是基于C语言的内置类型数据上的操作。
简单的创建一个类,类名为string,类的私有成员为char*类型指针,为指针动态指针开辟一段空间,就可以在这段空间上进行操作了。
对于string的操作有很多,这里主要实现string的一些运算和比较,比如比较两个字符串是否相等,在以前没有string类的时候,我们比较两个字符串需要写一个函数,对字符串进行遍历,在string中原理大致相同,但有了string类之后,就可以随时调用类的方法,在类中实现“==“运算符的重载,给出公用接口就可以了。
此时,我们便可以轻松比较两个字符串是否相等
在进行string赋值操作时,由于字符串所占的空间可能超出为原指针开辟的空间,所以要进行判断并重新开辟空间
下面是一个比较完整的string类的实现
string类有多种实现方式,前面只是比较常规的一种,仅供参考。
string的实现得益与c++的封装特性,封装是c++的三大特性之一,string类型其实是在char*类型的基础上进行一层封装。其实质还是基于C语言的内置类型数据上的操作。
简单的创建一个类,类名为string,类的私有成员为char*类型指针,为指针动态指针开辟一段空间,就可以在这段空间上进行操作了。
class String { public: String(char *str = NULL) //构造 { if (str == NULL) { _pstr = new char[1]; *_pstr = '\0'; } else { _pstr = new char[strlen(str) + 1]; strcpy(_pstr, str); } ~String() { if (this->_pstr != NULL) { delete[] _pstr; _pstr = NULL; } } private: char *_pstr; };
对于string的操作有很多,这里主要实现string的一些运算和比较,比如比较两个字符串是否相等,在以前没有string类的时候,我们比较两个字符串需要写一个函数,对字符串进行遍历,在string中原理大致相同,但有了string类之后,就可以随时调用类的方法,在类中实现“==“运算符的重载,给出公用接口就可以了。
class String { public: String(char *str = NULL) //构造 { if (str == NULL) { _pstr = new char[1]; *_pstr = '\0'; } else { _pstr = new char[strlen(str) + 1]; strcpy(_pstr, str); } ~String() { if (this->_pstr != NULL) { delete[] _pstr; _pstr = NULL; } } bool operator==(const String&s) { char* s1 = _pstr; char* s2 = s._pstr; while (*s1!='\0'&&*s2!='\0') { if (*s1 == *s2) { s1++; s2++; if (*s1 == '\0'&&*s2 == '\0') { return true; } } else return false; } if (*s1 == '\0'&&*s2 == '\0') { return true; } else { return false; } } private: char *_pstr; };
此时,我们便可以轻松比较两个字符串是否相等
int main() { string s1("abcd"); string s2("cdef"); int a=(s1==s2); return 0; }
在进行string赋值操作时,由于字符串所占的空间可能超出为原指针开辟的空间,所以要进行判断并重新开辟空间
class String { public: String(char *str = NULL) { if (str == NULL) { _pstr = new char[1]; *_pstr = '\0'; } else { _pstr = new char[strlen(str) + 1]; strcpy(_pstr, str); } } ~String() { if (this->_pstr != NULL) { delete[] _pstr; _pstr = NULL; } } String& operator=(const String& s) { if (this != &s) { char *tmp = new char[strlen(s._pstr) + 1]; strcpy(tmp, s._pstr); delete[] _pstr; //释放原来的内存 this->_pstr = tmp; } return *this; } private: char *_pstr; };
下面是一个比较完整的string类的实现
class String { public: String(char *str = NULL) { if (str == NULL) { _pstr = new char[1]; *_pstr = '\0'; } else { _pstr = new char[strlen(str) + 1]; strcpy(_pstr, str); } } String(const String& s) :_pstr(new char[strlen(s._pstr)+1]) { strcpy(_pstr, s._pstr); } ~String() { if (this->_pstr != NULL) { delete[] _pstr; _pstr = NULL; } } String& operator=(const String& s) { if (this != &s) { char *tmp = new char[strlen(s._pstr) + 1]; strcpy(tmp, s._pstr); delete[] _pstr; this->_pstr = tmp; } return *this; } bool operator>(const String&s) { char* s1 = _pstr; char* s2 = s._pstr; while (*s1) { if (*s1 > *s2) { return true; } else if (*s1 < *s2) { return false; } else { s1++; s2++; } } return false; } bool operator>=(const String&s) { char* s1 = _pstr; char* s2 = s._pstr; while (*s1) { if (*s1 >= *s2) { return true; } else { return false; } } } bool operator<(const String&s) { char* s1 = _pstr; char* s2 = s._pstr; while (*s1) { if (*s1 < *s2) { return true; } else if (*s1 > *s2) { return false; } else { s1++; s2++; } } return true; } bool operator<=(const String&s) { char* s1 = _pstr; char* s2 = s._pstr; while (*s1) { if (*s1 <= *s2) { return true; } else { return false; } } } bool operator==(const String&s) { char* s1 = _pstr; char* s2 = s._pstr; while (*s1!='\0'&&*s2!='\0') { if (*s1 == *s2) { s1++; s2++; if (*s1 == '\0'&&*s2 == '\0') { return true; } } else return false; } if (*s1 == '\0'&&*s2 == '\0') { return true; } else { return false; } } bool operator!=(const String&s) { if (_pstr == s._pstr) return false; else return true; } size_t Size() // 模拟strlen { int count = 0; char* s = _pstr; while (*s) { count++; s++; } return count; } // 检测s是否是当前对象的子串 bool StrStr(const String& s) // 模拟strstr { char *Str = _pstr; char* Substr = s._pstr; char *TStr; char *TSub; while (*Str) { TStr = Str; TSub = Substr; while(*Str == *Substr) { Str++; Substr++; if (*Substr == '\0') { return true; } } Str = TStr; Substr = TSub; Str++; } return false; } // 将当前对象从pos位置开始复制count个字符到p所指向的空间中 // 返回复制到p中有效字符的个数 size_t Copy(char* p, size_t pos, size_t count) // 模拟strcpy { assert(p); if (count <= 0) { return 0; } char *s1 = _pstr; while (pos--) { s1++; } if (*s1 = '\0'&&count>0) { *p = *s1; return 1; } int CopyNumber = 0; while (--count) { if (*s1 != '\0') { *p = *s1; p++; s1++; CopyNumber++; } else { return CopyNumber; } } } private: char *_pstr; };
string类有多种实现方式,前面只是比较常规的一种,仅供参考。
相关文章推荐
- 用标准c++实现string与各种类型之间的转换
- c++实现一个比较两个string类型的版本号的小demo
- C++中实现从std::string类型到bool型的转换
- 利用C++实现从std::string类型到bool型的转换
- C++通过ostringstream实现任意类型转string
- C++ hashmap实现(Key, Value为string类型)
- C++封装C#的类库DLL,并c++中C#的string类型的转换使用
- C++通过ostringstream实现任意类型转string
- C++通过ostringstream实现任意类型转string
- C++--String类型各种重载函数的实现
- C++ stringstream实现不同类型数据的转换
- C++如何通过ostringstream实现任意类型转string
- C++中实现从std::string类型到bool型的转换
- C++--String类型各种重载函数的实现
- C++中实现从std::string类型到bool型的转换
- c++实现一个比较两个string类型的版本号的小demo
- 用标准c++实现string与各种类型的转换
- C++ 实现任意基本类型转换为 string 类型
- c++数据类型--string
- C++实现CString和string的互相转换