C++:string模板实现

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<string>
#include<assert.h>
using namespace std;

//快速模拟string类
namespace String
{
    class string
    {
    public:
        typedef char* iterator;
        /构造

        string(const char* str = "")
        {
            if (str == nullptr)
                str = "";
            _size = strlen(str);
            _capacity = _size;
            _str = new char[_capacity + 1];
            strcpy(_str, str);
        }
        string(const string& s)
        {
            string temp(s._str);
            this->swap(temp);
        }
        //void* memset(void* ptr,int value,size_t num);//将ptr里面的值生成num个value
        string(size_t n, char val)
        {
            _str = new char[n + 1];
            memset(_str, val, n);
            _str[n] = '\0';
            _size = n;
            _capacity = n;
        }
        string& operator=(string s)
        {
            this->swap(s);
        }
        ~string()
        {
            if (_str)
            {
                delete[] _str;
                _str = nullptr;
                _size = 0;
                _capacity = 0;
            }
        }
        //迭代器
        iterator begin()
        {
            return _str;
        }
        iterator end()
        {
            return _str + _size;
        }

        //容量
        size_t size()const
        {
            return _size;
        }
        size_t length()const
        {
            return _size;
        }
        size_t capacity()const
        {
            return _capacity;
        }
        bool empty()const
        {
            return _size == 0;
        }
        void clear()
        {
            _size = 0;
        }

        void resize(size_t newsize, char val)//修改空间
        {
            size_t oldsize = size();
            if (newsize > oldsize)
            {
                //有效元素增多
                if (newsize > capacity())
                    reserve(newsize);
                //多余位置使用val填充
                memset(_str + _size, val, newsize - _size);
            }
            _size = newsize;
            _str[_size] = '\0';
        }
        void resize(size_t newsize)
        {
            resize(newsize, '\0');
        }
        void reserve(size_t newcapacity)//扩容
        {
            size_t oldcapacity = capacity();
            if (newcapacity > oldcapacity)
            {
                //申请新空间 
                char* temp = new char[newcapacity + 1];
                //拷贝元素
                strncpy(temp, _str, _size);//从_str中拷贝_size个元素
                //释放旧空间
                delete[] _str;
                //使用新空间
                _str = temp;

                _capacity = newcapacity;
                _str[_size] = '\0';
            }
        }


        //元素访问
        char& operator[](size_t index)
        {
            assert(index < _size);
            return _str[index];
        }
        const char& operator[](size_t index)const
        {
            assert(index < _size);
            return _str[index];
        }
        char& at(size_t index)
        {
            //如果越界,抛出out_of_range的异常
            return _str[index];
        }
        const char& at(size_t index)const
        {
            assert(index < _size);
            return _str[index];
        }

        //修改
        void push_back(char ch)//尾插
        {
            *this += ch;
        }
        string& operator+=(char ch)//尾插
        {
            if (_size == _capacity)
                reserve((size_t)_capacity * 1.5 + 3);
            _str[_size] = ch;
            _size++;
            _str[_size] = '\0';
            return *this;
        }
        string& operator+=(const char* str)//两个字符串拼接
        {
            size_t needSpace = strlen(str);
            size_t leftSpace = capacity() - size();
            if (needSpace > leftSpace)
                reserve(capacity() + needSpace);//在旧容量扩充需要的

            strcat(_str, str);//拼接
            _size += needSpace;
            _str[_size] = '\0';
            return *this;
        }
        string& operator+=(const string& s)
        {
            *this += s.c_str();
            return *this;
        }

        const char* c_str()const
        {
            return _str;
        }

        ///
        string& append(const char* str)
        {
            *this += str;
            return *this;
        }
        string& append(const string& s)
        {
            *this += s;
            return *this;
        }

        pos插入s
        string& insert(size_t pos, const string& s);
        pos开始删除n个
        string& erase(size_t pos = 0, size_t n = npos);

        交换
        void swap(string& s)
        {
            std::swap(_str, s._str);
            std::swap(_size, s._size);
            std::swap(_capacity, s._capacity);
        }

        查找
        size_t find(char ch, size_t pos = 0)const
        {
            if (pos >= _size)
                return npos;
            for (size_t i = 0; i < _size; i++)
            {
                if (_str[i] == ch)
                    return i;
            }
            return npos;
        }
        size_t rfind(char c, size_t pos = npos)const
        {
            if (pos >= _size)
            {
                pos = _size - 1;
                for (int i = pos; i >= 0; i--)
                    if (_str[i] == c)
                        return i;
                return npos;
            }
        }
        string substr(size_t pos = 0, size_t n = npos)const
        {
            //从pos位置开始,截取n个字符  如果n没有传递,则从pos一直到末尾,如果pos和n都没有传递,则将字符串整个截取
            if (pos >= _size)
                return string();

            n = min(n, _size - pos);//取二者最小值,截取超范围,则取范围内的

            string temp(n, '\0');
            size_t index = 0;
            for (size_t i = pos; i < pos + n; i++)
            {
                temp[index++] = _str[i];
            }
            return temp;
        }

    private:
        char* _str;
        size_t _size;
        size_t _capacity;
        const static size_t npos = -1;

        friend ostream& operator<<(ostream& out, const string& s);
    };

    ostream& operator<<(ostream& out, const string& s)
    {
        for (size_t i = 0; i < s.size(); i++)
        {
            cout << s[i];
        }
        return out;
    }
}
//构造 迭代器模块
void TestMyString01()
{
    String::string s1;
    String::string s2("hello");
    String::string s3(10, 'A');
    String::string s4(s3);

    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;

    for (size_t i = 0; i < s2.size(); i++)
    {
        cout << s2[i];
    }
    cout << endl;

    for (auto e : s3)
        cout << e;
    cout << endl;

    auto it = s4.begin();
    while (it != s4.end())
    {
        cout << *it;
        ++it;
    }
    cout << endl;

}

//容量模块
void TestMyString02()
{
    String::string s("hello");
    s.reserve(15);

    cout << s.size() << endl;
    cout << s.length() << endl;
    cout << s.capacity() << endl;

    s.resize(10, '!');//只增加,不扩容
    cout << s << " " << s.size() << " " << s.capacity() << endl;

    s.resize(20, 'A');//需要扩容
    cout << s << " " << s.size() << " " << s.capacity() << endl;

    s.resize(12);
    cout << s << " " << s.size() << " " << s.capacity() << endl;
    s.resize(5);
    cout << s << " " << s.size() << " " << s.capacity() << endl;

}

//插入模块
void TestMyString03()
{
    String::string s;
    s.push_back('h');
    s += 'e';
    s += "llo";
    s += ' ';
    s.append("world");

    cout << s << endl;
}

//正反查找模块
void TestMyString04()
{
    String::string s("hello world 你好!!");
    size_t pos = s.find('o');
    cout << pos << endl;

    pos = s.find('0', pos + 1);//从pos+1位置开始找
    cout << pos << endl;

    pos = s.find('0', pos + 1);//再从pos+1位置开始找
    cout << pos << endl;
    
    pos = s.rfind('你');//从末尾开始找
    String::string ret = s.substr(pos + 1);
    cout << ret << endl;
}
int main()
{
//    TestMyString01();
//    TestMyString02();
//    TestMyString03();
    TestMyString04();
}

上机实践:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值