C++中的string类,(浅浅模拟实现一下)

C++中的string类是要比STL出现的早的,但是string类的接口和常规容器的接口基本相同,

其中包括 构造,拷贝构造,赋值,这些涉及到的一些深浅拷贝,迭代器、增删改查、扩容、还有比较、[ ] 、<< 、 >> 一些操作符的重载,

其实对于string类没有特别要注意的点,那我就浅浅的模拟实现一下吧,有什么问题欢迎大家指出来。

#include <iostream>
#include <stdlib.h>
using namespace std;

namespace ltx
{
    class string
    {
        friend ostream& operator<<(ostream& _cout, const ltx::string& s);
        friend istream& operator>>(istream& _cin, ltx::string& s);

    public:
        typedef char* iterator;
        
        string(const char* str = "") {
            _size = strlen(str);
            _capacity = _size;
            _str = new char[_size + 1];
            //strncpy(_str, str, _size - 1);
            strcpy(_str, str);
            _str[_size] = '\0';
        }
        string(const string& s) {
            string temp(s._str);
            swap(temp);
        }
        string& operator=(const string& s) {
            string temp(s._str);
            swap(temp);
            return *this;
        } 
        ~string() {
            delete[] _str;
            _str = nullptr;
            _size = 0;
            _capacity = 0;
        }

        //

    // iterator
        iterator begin() {
            return _str;
        }
        iterator end() {
            return _str + _size - 1;
        }

        /

        void Expansion() {
            while (_size >= _capacity) {
                _capacity *= 2;
                char* temp = new char[_capacity];
                strcpy(temp, _str);
                delete[] _str;
                _str = temp;
            }
        }

    // modify
        void push_back(char c) {
            Expansion();
            _str[_size++] = c;
            _str[_size] = '\0';
        }
        string& operator+=(char c) {
            push_back(c);
            return *this;
        }
        void append(const char* str) {
            int size = _size;
            _size += strlen(str);
            Expansion();
            strcpy(_str + size, str);
            _str[_size] = '\0';
        }
        string& operator+=(const char* str) {
            append(str);
            return *this;
        }
        void clear() {
            _size = 0;
            _str[0] = '\0';
        }
        void swap(string& temp) {
            ::swap(_size, temp._size);
            ::swap(_capacity, temp._capacity);
            ::swap(_str, temp._str);
        }
        const char* c_str()const {
            return _str;
        }

        /

        // capacity
        size_t size()const {
            return _size;
        }
        size_t capacity()const {
            return _capacity;
        }
        bool empty()const {
            return _size == 0;
        }
        void resize(size_t n, char c = '\0') {
            if (n > _size) {
                int size = _size;
                _size += n;
                Expansion();
                while (size < _size) {
                    _str[size++] = c;
                }
                _str[_size] = '\0';
            }
            else if (n >= 0){
                _size = n;
                _str[_size] = '\0';
            }
        }
        void reserve(size_t n) {
            if (_capacity < n) {
                _capacity = n;
                char* temp = new char[_capacity];
                strcpy(temp, _str);
                delete[] _str;
                _str = temp;
                _str[_size] = '\0';
            }
        }

        /

        // access
        char& operator[](size_t index) {
            return _str[index];
        }
        const char& operator[](size_t index)const {
            return _str[index];
        }

        /

        //relational operators
        bool operator<(const string& s)const {
            int size = _size < s._size ? _size : s._size;
            for (int i = 0; i < size; i++) {
                if (_str[i] <= s._str[i]) continue;
                return false;
            }
            return _size < s._size;
        }
        bool operator<=(const string& s)const {
            return !(*this > s);
        }
        bool operator>(const string& s)const {
            int size = _size < s._size ? _size : s._size;
            for (int i = 0; i < size; i++) {
                if (_str[i] >= s._str[i]) continue;
                return false;
            }
            return _size > s._size;
        }
        bool operator>=(const string& s)const {
            return !(*this < s);
        }
        bool operator==(const string& s)const {
            if (_size != s._size) return false;
            for (int i = 0; i < _size; i++) {
                if (_str[i] != s._str[i]) return false;
            }
            return true;
        }
        bool operator!=(const string& s)const {
            return !(*this == s);
        }
        // 返回c在string中第一次出现的位置
        size_t find(char c, size_t pos = 0) const {
            for (; pos < _size; pos++) {
                if (_str[pos] == c) return pos;
            }
            return -1;
        }
        // 返回子串s在string中第一次出现的位置
        size_t find(const char* s, size_t pos = 0) const {
            int i = pos;
            int j = 0;
            int jmax = strlen(s);
            while (pos < _size) {
                i = pos;
                j = 0;
                while (i < _size && j < jmax && _str[i++] == s[j++]) {
                    if (_str[i] != s[j]) break;
                }
                if (j == jmax) return pos;
                pos++;
            }
            return -1;
        }
        // 在pos位置上插入字符c/字符串str,并返回该字符的位置
        size_t insert(size_t pos, char c) {
            if (pos <= _size) {
                _size++;
                int size = _size;
                Expansion();
                while (size > pos) {
                    :: swap(_str[size], _str[size - 1]);
                    size--;
                }
                _str[pos] = c;
            }
            return pos;
        }
        size_t insert(size_t pos, const char* str) {
            if (pos <= _size) {
                int Ssize = strlen(str);
                _size += Ssize;
                int cur = _size;
                Expansion();
                while (cur > pos) {
                    ::swap(_str[cur], _str[cur - 1]);
                    cur--;
                }
                //_str[pos] = c;
                strncpy(_str + pos, str, Ssize);
            }
            return pos;
        }
        // 删除pos位置上的元素,并返回该元素的下一个位置
        size_t erase(size_t pos, size_t len) {
            if (pos < _size) {
                if (pos + len < _size) {
                    while (pos + len <= _size) {
                        _str[pos] = _str[pos + len];
                        pos++;
                    }
                    _size -= len;
                }
                else {
                    _str[pos] = '\0';
                    _size = pos;
                }
            }
            return pos;
        }

    private:
        char* _str;
        size_t _capacity;
        size_t _size;
    };
    ostream& operator<<(ostream& _cout, const ltx::string& s) {
        _cout << s.c_str();
        return _cout;
    }
    istream& operator>>(istream& _cin, ltx::string& s) {
        s.clear();
        char get = _cin.get();
        char temp[33] = { 0 };
        int i = 0;
        while (get != '\n') {
            if (i == 32) {
                s += temp;
                i = 0;
            }
            temp[i++] = get;
            get = _cin.get();
        }
        if (i != 0) {
            temp[i] = '\0';
            s += temp;
        }
        return _cin;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟婆的cappucino

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值