String类

来源:互联网 发布:7号外设淘宝店网址 编辑:程序博客网 时间:2024/09/21 06:19

模拟实现string类:
头文件:

#ifndef _STRING_H__#define _STRING_H__#include<iostream>using namespace std;#include<cstring>#include<cassert>class String{    friend ostream& operator<<(ostream _cout, const String& s);public:    //构造函数    String(const char* str ="");    String(const String& str);    //拷贝构造函数    String& operator=(const String& s);    //析构函数    ~String();public:    //操作运算符的重载    char& operator[](size_t index);    const char& operator[](size_t index)const;    string operator+(const string& s);    bool operator==(const String&s);    bool operator>(const String&s);    bool operator>=(const String&s);    bool operator<(const String&s);    bool operator<=(const String&s);    bool operator!=(const String&s);public:    //尾插一个元素    void PushBack(char ch);    //尾插一个字符串    void Append(const char* str);    //尾删一个元素    void PopBack();    //尾删n个字符    void PopBackStr(size_t n);    //头插一个字符    void PushFront(char ch);    //头删一个字符    void PopFront();    //任意位置指定插入一个字符    void Insert(size_t pos, char ch);    //任意位置插入一个字符串    void Insert(size_t pos, const char* str);    //任意位置删除一个元素    void Erase(size_t pos);    //任意位置删除n个字符    void Erase(size_t pos, size_t n);public:    //扩容    char* ExpandCapacity(size_t n);    char* Getstr();    size_t Size()const;    void display();private:    char* _str;    size_t _size;    size_t _capacity;};#endif//_STRING_H__

实现函数:

#define _CRT_SECURE_NO_WARNINGS 1#include"String.h"String::String(const char* str):_str(new char[strlen(str)+1]){    strcpy(_str, str);    _size = strlen(str);    _capacity = _size;}////浅拷贝////浅拷贝会引发同一块内存被释放多次的危险//String::String(const String& str)//{//  _str = str._str;//_size = strlen(str);//_capacity = _size;//}////深拷贝<普通版>//String::String(const String& str)//{//  delete[]_str;//  _str = new char[strlen(str._str) + 1];//  strcpy(_str, str._str);// _size = strlen(str);//_capacity = _size;//}//深拷贝 简洁版String::String(const String& str){    String s(str._str);    std::swap(_str, s._str);    _size = strlen(str._str);    _capacity = _size;}////浅拷贝//String& String::operator=(const String& s)//{//  if (this != &s)//  {//      _str = s._str;//  }//  return *this;//}////深拷贝 普通版//String& String::operator=(const String& s)//{//  if (this != &s)//  {//      delete[]_str;//      _str = new char[strlen(s._str) + 1];//      strcpy(_str, s._str);//      _size = strlen(s._str);//      _capacity = _size;//  }//  return *this;//}//深拷贝 简洁版String& String::operator=(const String& s){    if (this != &s)    {        String str(s._str);        std::swap(_str, str._str);        _size = strlen(s._str);        _capacity = _size;    }    return *this;}String::~String(){    if (_str)    {        delete[]_str;        _str = NULL;    }}size_t my_strlen(char* dest){    assert(dest);    const char* p = dest;    while (*p++)    {        ;    }    return (p - dest-1);}//求字符串长度size_t String::Size()const{    return _size;}//运算符重载char& String::operator[](size_t index){    assert(index<Size());    return _str[index];}const char& String::operator[](size_t index)const{    assert(index<Size());    return _str[index];}bool String::operator==(const String&s){    size_t index = 0;    for (index=0; index<s._size && index<s._size; index++)    {        if (_str[index] != s._str[index])        {            return false;        }    }    if (index==_size && index==s._size)    {        return true;    }    return false;}bool String::operator>(const String&s){    size_t i = 0;    for (i = 0; ((i < _size) && (i < s._size)); i++)    {        if (_str[i] < s._str[i])        {            return false;        }        else if (_str[i] > s._str[i])        {            return true;        }    }    if ((i == s._size) && (i < _size))    {        return true;    }    return false;}bool String::operator>=(const String&s){    return(operator>(s) || operator==(s));}bool String::operator<(const String&s){    return(!(operator>=(s)));}bool String::operator<=(const String&s){    return((operator<(s)) || (operator==(s)));}bool String::operator!=(const String&s){    return (!(operator==(s)));}//获取字符串char* String::Getstr(){    return _str;}char* String:: ExpandCapacity(size_t n){    char* tmp = new char[n+1];    strcpy(tmp, _str);    delete[]_str;    _str = tmp;    tmp = NULL;    return _str;}//尾插一个字符串void String::PushBack(char ch){    if ((_size) == _capacity)    {        ExpandCapacity(_capacity * 2);        _capacity *= 2;    }    _str[_size] = ch;    _size++;    _str[_size] = '\0';}//尾插一个字符串void String:: Append(const char* str){    size_t len = strlen(str);    if (len + _size > _capacity)    {        ExpandCapacity(len + _size);        _capacity += len;    }    strcpy(_str + _size, str);    _size += len;    _capacity += len;}//尾删一个字符void String:: PopBack(){    assert(_size);    _str[_size-1] = '\0';    _size--;}//尾部删除n个字符void String:: PopBackStr(size_t n){    if (n > _size)    {        _str[0] = '\0';        _size = 0;    }    else    {        _str[_size - n - 1] = '\0';        _size -= n;    }}//头插一个字符void String:: PushFront(char ch){    if (_size == _capacity)    {        ExpandCapacity(_capacity * 2);        _capacity *= 2;    }    for (size_t i = 0; i <= _size; i++)    {        _str[_size+1-i] = _str[_size-i];    }    _str[0] = ch;    _size++;}//头删一个字符void String:: PopFront(){    for (size_t i = 0; i < _size; i++)    {        _str[i] = _str[i + 1];    }    _size--;}//任意位置指定插入一个字符void String:: Insert(size_t pos, char ch){    if (pos<0 || pos>_size)    {        return;    }    for (size_t i = 0; i <= _size-pos;i++)    {        _str[_size + 1-i] = _str[_size-i];    }    _str[pos] = ch;    _size++;}//任意位置插入一个字符串void String::Insert(size_t pos, const char* str){    if (pos<0 || pos>_size)    {        return;    }    size_t len = strlen(str);    if (len + _size > _capacity)    {        ExpandCapacity(_size + len);        _capacity += len;    }    for (size_t i = 0; i <= _size -pos; i++)    {        _str[_size +len- i] = _str[_size - i];    }    strncpy(_str + pos, str,len);    _size += len;}//任意位置删除一个元素void String::Erase(size_t pos){    if (pos<0 || pos>_size)    {        return;    }    for (size_t i = 0; i <= _size - pos; i++)    {        _str[pos + i] = _str[pos + i + 1];    }    _size--;}//任意位置删除n个字符void String:: Erase(size_t pos, size_t n){    if (pos<0 || pos>_size || n > _size - pos)    {        return;    }    for (size_t i = 0; i <_size - n; i++)    {        _str[pos+i] = _str[pos + n + i];    }    _size -= n;}void String::display(){    cout << _str << endl;}

测试函数:

#define _CRT_SECURE_NO_WARNINGS 1#include"String.h"void test1(){    String s1("hello");    String s2(s1);    String s3("hello");    String s4;    //s4=s3 + s1;    cout << s2.Size() << endl;    cout << s2[1] << endl;    s3.display();    s4.display();    //cout << strcmp("hello", "hell") << endl;    if (s1 < s3)    {        cout << "s1<s2" << endl;    }    s1.PushBack('a');    s1.PushBack('b');    s1.display();    s1.Append("world");    s1.display();    s1.PopBack();    s1.PopBackStr(5);    s1.display();    s1.PushFront('x');    s1.display();    s1.PopFront();    s1.display();    s1.Insert(1, 'a');    s1.display();    s1.Insert(6, "worlddddddddddddddddd");    s1.display();    s1.Erase(1);    s1.display();    s1.Erase(1, 5);    s1.display();}int main(){    test1();    system("pause");    return 0;}
原创粉丝点击