字符串类string类模板

来源:互联网 发布:mysql 事务 编辑:程序博客网 时间:2024/06/01 08:41
#include using namespace std;#define MAX 200class String{private:    char *m_data;    int count;//字符串长度public:    String();//构造函数    ~String();//析构函数    String(const String &s);//拷贝构造函数    String(const char *s);//有字符串作为参数传入构造函数    String(int x);//指定长度的构造函数    bool empty();//返回字符串是否为空    const int size();//返回字符串的长度    String operator+(const String &s) const;//重载+运算符,用于拼接字符串    const String &operator=(const String &s);//重载=运算符    friend ostream &operator<<(ostream &output,const String &s)//输出    {        output<>(istream &input,String &s)//输入    {        input>>s.m_data;        s.count=strlen(s.m_data);        return input;    }    char operator[](int x) const;//重载[]    friend String m_Substr(const String &s, int S, int len);//取子串操作};String::String() //构造函数{    m_data=new char[MAX];    count=0;}String::String(int x){    m_data=new char[x+1];    count=x;}String::~String() //析构函数{    delete []m_data;    count=0;}String::String(const char *s)//有字符串作为参数传入构造函数{    if(!s) {//如果传入字符串为空字符串        count = 0;        m_data=new char[1];        *m_data='\0';    }    else{        m_data=new char[strlen(s)+1];        count=strlen(s);        strcpy(m_data,s);    }}String::String(const String &s) //拷贝构造函数{    m_data=new char[s.count+1];//最后一位加上'\0'    count=s.count;    strcpy(m_data,s.m_data);    m_data[count]='\0';}bool String::empty() //返回字符串是否为空{    return count==0;}const int String::size() //返回字符串的长度{    return count;}String String::operator+(const String &s) const//重载+运算符,用于拼接字符串{    String str(count+s.count);    strcpy(str.m_data,m_data);    strcat(str.m_data,s.m_data);    return str;}const String &String::operator=(const String &s) //重载=运算符{    if(this==&s)        return *this;    delete[]m_data;//释放原有空间    count=s.count;    m_data=new char[count+1];    strcpy(m_data,s.m_data);    return *this;}char String::operator[](int x) const//重载[]{    return m_data[x];}String m_Substr(const String &s, int S, int len)//取子串操作{    String temp(len> s[1] >> s[2];    int i, j, k, l, S;    char order;    while (cin >> order) {        switch (order) {            case 'A':                cin >> i >> j;                s[j] = s[i];                break;            case 'C':                cin >> i >> j >> k;                s[k] = s[i] + s[j];                break;            case 'F':                cin >> i >> S >> l >> k;                s[k] = m_Substr(s[i], S, l);                break;            case 'P':                cin >> i;                cout << s[i] << endl;                break;        }    }    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 驾驶证过了年检怎么办 驾驶证审证逾期怎么办 d驾驶证3年没捡怎么办 c1驾驶证3年没审怎么办 驾驶证换证外地怎么办 考驾驶证快到期怎么办 驾照到期人在国外怎么办 驾驶证3年没审怎么办c3 驾驶证几年没审怎么办 驾照体检过期了怎么办 b2驾照超过年检怎么办 驾驶证过审一年怎么办 驾照一年未年审怎么办 b驾照年审过期怎么办 摩托车驾驶证过期一年怎么办 驾驶证过期一年半怎么办 驾照过期了几天怎么办 驾照过期超过一年怎么办 考试驾照过期了怎么办 驾校考试过期了怎么办 驾驶证明过期了怎么办 驾驶证年过期了怎么办 驾照过期六个月怎么办 移动预约号码取消怎么办 身份证换地址驾驶证怎么办 刚来成都怎么办居住证 我在外地怎么办身份证 身份证丢在外地怎么办 换领新身份证时旧证丢了怎么办 二代身份证重号怎么办 北京行驶证到期怎么办 北京驾驶证即将过期怎么办 去澳门没有网络怎么办 三个周期未年检怎么办 深圳驾照丢了怎么办 武汉驾照年审过期怎么办 武汉驾照过期了怎么办 科二过不了怎么办 南京身份证到期换新怎么办 过期身份证丢了怎么办 南京驾照过期了怎么办