[c++]字符串类各种操作的实现以及遇到的bug

字符串类的结构如下:

Class String:

public:

构造函数:1:未给定初始化的内容则默认指向只包含’\0’的字符串(即空串)—->开辟一个字节的空间,并用’\0’初始化 2:给定初始化的内容则开辟相应的空间并初始化为相应内容;

析构函数:释放动态开辟的空间,即m_data指向的内存空间

初始化函数

赋值函数

private:

char * m_data;——->>>指向动态开辟的空间

对字符串类实现如下操作:

String operator+(const String &s); //s = s1 + s2String operator+=(const String &s); //s1 += s2char& operator[](int index);bool operator==(String &s);bool operator!=(String &s);bool operator>(String &s); //s1 > s2bool operator<=(String &s);bool operator<(String &s);bool operator>=(String &s);ostream& operator<<(ostream &out, const String &s);istream& operator>>(istream &in, String &s);

代码如下:#include<iostream>using namespace std;//友元函数声明class String;ostream& operator<<(ostream &out,const String &str);istream& operator>>(istream &in,String &str);class String{//默认的构造函数public:String(const char *str = NULL){if(NULL == str){m_data = new char[1];m_data[0] = '\0';}else{m_data = new char[strlen(str) + 1];strcpy(m_data,str);}}~String(){delete []m_data;}String(const String &str){m_data = new char[strlen(str.m_data) + 1];strcpy(m_data,str.m_data);}String& operator=(const String &str){if(this != &str){delete []m_data;//此步骤易忘记,从而可能造成内存泄露(原因,未考虑对像可能已经过存在,开辟过内存)如:String s("123");s = s1;m_data = new char[strlen(str.m_data) + 1];strcpy(m_data,str.m_data);}return *this;}//运算符重载//连接:对像 = 对像 + 对像String operator+(const String &str){ char *s = new char[strlen(m_data) + strlen(str.m_data) + 1]; strcpy(s,m_data); strcat(s,str.m_data); String tmp(s); //构造临时变量//return String(s);!!!!!!错误!! delete []s; //释放开辟的内存,防止内存泄露 return tmp;}//连接:对像 += 对像 s1 += sString& operator+=(const String &str){char *s = new char[strlen(m_data) + strlen(str.m_data) + 1];strcpy(s,m_data);strcat(s,str.m_data);delete []m_data;//释放原来的空间m_data = s;//指向新开辟的空间return *this;}//字符串是否相等,,相等返回真,否则返回假 s1 == s?bool operator==(const String &str){if(strcmp(m_data,str.m_data) == 0)return true;elsereturn false;}//字符串是否不相等,不相等返回真,否则返回假 s 1!= s?bool operator!=(const String &str){if(strcmp(m_data,str.m_data) == 0)return false;elsereturn true;}//s1 > sbool operator>(const String &str){if(strcmp(m_data,str.m_data) > 0)return true;elsereturn false;}//s1 >= sbool operator>=(const String &str){if(strcmp(m_data,str.m_data) >= 0)return true;elsereturn false;}//s1 < sbool operator<(const String &str){if(strcmp(m_data,str.m_data) < 0)return true;elsereturn false;}//s1 <= sbool operator<=(const String &str){if(strcmp(m_data,str.m_data) <= 0)return true;elsereturn false;}char& operator[](int index){return m_data[index];}friend ostream& operator<<(ostream &out,const String &str);friend istream& operator>>(istream &in,String &str);private:char *m_data;};ostream& operator<<(ostream &out,const String &str){ out<<str.m_data<<endl; return out;}istream& operator>>(istream &in,String &str){//std::cout<<"你可以输入"<<strlen(str)<<"个字符"<<endl;in>>str.m_data;return in;}int main(){String s("_helen"); <span style="white-space:pre"></span> //测试构造String s1("zyh");s1 += s;<span style="white-space:pre"></span>//测试加等:+=cout<<s1;//测试输出:<<String s2 = s1;//测试赋值:=cout<<s2;String s5 ="1123";//测试下标:[]s5[1] = 'd';cout<<s5;String s6 ("zyh");String s7 ("zyh_helen");if(s6 > s7)<span style="white-space:pre"></span>//测试比较{cout<<"s6 > s7"<<endl;}else {cout<<"s6 < s7"<<endl;}String s4("zyh_helen");cin>>s4;//测试输入cout<<s4;return 0;}

人,也总是很难发现自己的错误,

[c++]字符串类各种操作的实现以及遇到的bug

相关文章:

你感兴趣的文章:

标签云: