string实现C++


#include <iostream>

class String
{
public:
    String();
    String(char c, int n);
    String(const char* c_str);
    String(const String& other);
    String& operator=(const String& other);
    String& operator=(const char* c_str);
    ~String();
    String& operator+=(const String& other);
    String& operator+=(const char* c_str);
    char& operator[](int i)const;
/*
1)必须在类的说明中说明友元函数,说明时以关键字friend开头,后跟友元函数的函数原型,友元函数的说明可以出现在类的任何地方,包括在private和public部分;
2)注意友元函数不是类的成员函数,所以友元函数的实现和普通函数一样,在实现时不用"::"指示属于哪个类,只有成员函数才使用"::"作用域符号;
3)友元函数不能直接访问类的成员,只能访问对象成员,
4)友元函数可以访问对象的私有成员,但普通函数不行;
5)调用友元函数时,在实际参数中需要指出要访问的对象,
6)类与类之间的友元关系不能继承。
7)一个类的成员函数也可以作为另一个类的友元,但必须先定义这个类。
*/
    friend std::ostream& operator<<(std::ostream& out, const String& other);
    friend std::istream& operator>>(std::istream& in, const String& other);
    //friend bool operator<(const String& left, const String& right);
    //friend bool operator>(const String& left, const String& right);
    //friend bool operator==(const String& left, const String& right);
    //friend bool operator!=(const String& left, const String& right);

    void print()const;
    int length()const;
    char& back();
    char& front();
    void append(const String& str);
    void insert(int pos,const String& str);
    void earse(int index);
    void earse(char c);
    void replace(char oldch,char newch);
    char* c_str()const;
private:
    char* m_data;
    int m_size;
};

String::String()
{
    m_data = new char[1];
    *m_data = '\0';
    m_size = 0;
}

String::String(char c, int n)
{
    m_data = new char[n + 1];
    m_size = n;
    while (n > 0)
    {
        m_data[--n] = c;
    }
    m_data[m_size] = '\0';
}

String::String(const char* c_str)
{
    if (c_str == NULL)
    {
        m_data = new char[1];
        *m_data = '\0';
        m_size = 0;
    }
    else
    {
        m_size = strlen(c_str);
        m_data = new char[m_size + 1];
        strcpy_s(m_data, m_size+1, c_str);

    }
}

String::String(const String& other)
{
    m_size = other.m_size;
    m_data = new char[m_size+ 1];
    strcpy_s(m_data, m_size + 1, other.m_data);
}

String& String::operator=(const String& other)
{
    if (this == &other)
    {
        return *this;
    }
    if (m_data != NULL)
    {
        delete[] m_data;
    }
    m_size = other.m_size;
    m_data = new char[m_size+1];
    strcpy_s(m_data, m_size + 1, other.m_data);
    return *this;
}

String& String::operator=(const char* c_str)
{
    if (c_str == NULL)
    {
        return *this;
    }
    if (m_data != NULL)
    {
        delete[] m_data;
        m_data = NULL;
    }
    m_size = strlen(c_str);
    m_data = new char[m_size + 1];
    strcpy_s(m_data, m_size + 1, c_str);
    return *this;
}

char& String::operator[](int i)const
{
    if (i < m_size)
    {
        return m_data[i];
    }
    return m_data[m_size];;
}

String& String::operator+=(const String& other)
{
    m_size = strlen(other.m_data) + m_size;
    char* c_temp = m_data;
    m_data = NULL;
    m_data = new char[m_size+1];
    strcpy_s(m_data, m_size + 1, c_temp);
    strcat_s(m_data, m_size + 1, other.m_data);
    delete[] c_temp;
    return *this;
}

String& String::operator+=(const char* c_str)
{
    m_size = strlen(c_str) + m_size;
    char* c_temp = m_data;
    m_data = NULL;
    m_data = new char[m_size + 1];
    strcpy_s(m_data, m_size + 1, c_temp);
    strcat_s(m_data, m_size + 1, c_str);
    delete[] c_temp;
    return *this;
}

String::~String()
{
    if (m_data != NULL)
    {
        delete[] m_data;
        m_data = NULL;
        m_size = 0;
    }
}

void String::print()const
{
    if (m_data != NULL)
    {
        std::cout << m_data << std::endl;
    }
}

int String::length()const
{
    return m_size;
}

std::ostream& operator<<(std::ostream& out, const String& other)
{
    for (int i = 0; i < other.length(); i++)
    {
        out << other[i];
    }
    return out;
}

//std::istream& operator>>(std::istream& in, const String& other)

//bool operator<(const String& left, const String& right)

//bool operator>(const String& left, const String& right)

//bool operator==(const String& left, const String& right)

//bool operator!=(const String& left, const String& right)

char& String::back(){
    if(m_size>0){
        return m_data[m_size-1];
    }else{
        return m_data[0];
    }
}
char& String::front(){
    return m_data[0];
}
void String::append(const String& str){
    m_size = str.m_size + m_size;
    char* c_temp = m_data;
    m_data = NULL;
    m_data = new char[m_size + 1];
    strcpy_s(m_data, m_size + 1, c_temp);
    strcat_s(m_data, m_size + 1, str.m_data);
    delete[] c_temp;
}
void String::insert(int pos,const String& str){
    if(pos < 0 || pos > m_size){
        return ;
    }
    m_size += str.m_size;
    char* c_temp = m_data;
    m_data = NULL;
    m_data = new char[m_size + 1];
    int i = 0;
    for(; i<pos;++i){
        m_data[i] = c_temp[i];
    }
    for(int j = 0;j < str.m_size;++j){
        m_data[i++] = str[j];
    }
    while(c_temp[pos] != '\0'){
        m_data[i++] = c_temp[pos++];
    }
    m_data[i] = '\0';
    delete[] c_temp;
}
void String::earse(int index){
    if(index < m_size){
        char* p1 = m_data + index;
        char* p2 = p1 + 1;
        while(*p2 != '\0'){
            *p1++ = *p2++;
        }
        *p1 = '\0';
        m_size -= 1;
    }
}
void String::earse(char c){
    char* p1 = m_data;
    char* p2 = m_data;
    while(*p1 != '\0'){
        if(*p1 != c){
            *p2++ = *p1++;
        }else{
            ++p1;
        }
    }
    *p2 = '\0';
    m_size = int(p2 - m_data);
}
void String::replace(char oldch,char newch){
    for(int i = 0 ; i < m_size ; ++i){
        if(m_data[i] == oldch){
            m_data[i] = newch;
        }
    }
}
char* String::c_str()const{
    return m_data;
}
int main()
{
    String str1('1', 9);
    String str2("12345");
    std::cout << str1 << std::endl;
    str2.print();
    str1 += "23454";
    str1.print();
    str1[5] = '9';
    str1.print();
    std::cout << str1[15] <<std::endl;

    str1.replace('4','A');
    str1.print();
    str1.earse('A');
    str1.print();
    str1.earse(0);
    str1.print();
    str1.insert(0,"xiaolixi");
    str1.print();
    return 0;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值