string类的部分实现

3 篇文章 0 订阅
1 篇文章 0 订阅

可能会有讲解,但肯定不在本篇。

今天没有学习的欲望。

太多了,我甚至都没有测试完。 

#pragma once 
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cassert>

using std::ostream;
using std::istream;
using std::cin;
using std::cout;
using std::endl;

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

    public:
        typedef char* iterator;
        typedef char* reverse_iterator;

    public:

        string(const char* str = "");
        string(const string& s);
        string(size_t n, char ch);
        string& operator=(const string& s);
        ~string();

        // iterator
        iterator begin();
        iterator end();
        reverse_iterator rbegin();
        reverse_iterator rend();
        // modify
        void push_back(char c);
        string& operator+=(char c);
        string& append(const char* str);
        string& append(size_t n, char ch);
        string& operator+=(const char* str);
        void clear();
        void swap(string& s);
        const char* c_str()const;

        // capacity
        size_t size()const;
        size_t capacity()const;
        bool empty()const;
        void resize(size_t n, char c = '\0');
        void reserve(size_t n);


        char& operator[](size_t index);
        const char& operator[](size_t index)const;

        //relational operators
        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);

        // 返回c在string中第一次出现的位置
        size_t find(char c, size_t pos = 0) const;

        // 返回子串s在string中第一次出现的位置
        size_t find(const char* s, size_t pos = 0) const;

        // 在pos位置上插入字符c/字符串str,并返回该字符的位置
        string& insert(size_t pos, char c);
        string& insert(size_t pos, const char* str);

        // 删除pos位置上的元素,并返回该元素的下一个位置
        string& erase(size_t pos = 0, size_t len = npos);

    private:
        char* _str;
        size_t _capacity;
        size_t _size;
        static size_t npos;
    };

    size_t string::npos = -1;

    ostream& operator<<(ostream& _cout, const bite::string& s) {
        _cout << s._str;
        return _cout;
    }

    istream& operator>>(istream& _cin, bite::string& s) {
        _cin >> s._str;
        return _cin;
    }

    string::string(const char* str)
        :_str(new char[strlen(str) + 1])
        , _capacity(strlen(str))
        , _size(_capacity)
    {
        if (str == NULL) {
            str = "";
        }
        strcpy(_str, str);
    }

    //string::string(const string& s)
    //    :_str(new char[s._capacity + 1])
    //    ,_capacity(s._capacity)
    //    ,_size(s._size)
    //{
    //    strcpy(_str, s._str);
    //}

    string::string(const string& s)
        :_str(nullptr)
    {
        string tmp(s._str);
        this->swap(tmp);
    }

    string::string(size_t n, char ch) {
        _str = new char[n + 1];
        memset(_str, ch, n);
        _str[n] = '\0';
        _size = n;
        _capacity = n;
    }

    string& string::operator=(const string& s) {
        if (this != &s) {
            _size = s._size;
            _capacity = s._capacity;
            _str = new char[_size];
        }
        return *this;
    }

    string::~string() {
        if (nullptr != _str) {
            delete[] _str;
            _size = 0;
            _capacity = 0;
        }
    }

    //iterator
    string::iterator string::begin() {
        return _str;
    }

    string::iterator string::end() {
        return _str + _size;
    }

    string::reverse_iterator string::rbegin() {
        return _str + _size;
    }

    string::reverse_iterator string::rend() {
        return _str;
    }

    //

    // capacity
    size_t string::size() const {
        return _size;
    }

    size_t string::capacity() const {
        return _capacity;
    }

    bool string::empty() const {
        return 0 == _size;
    }

    void string::resize(size_t n, char c) {
        if (n <= _size) {
            _str[n] = '\0';
        }
        else {
            if (n > _capacity) {
                reserve(n + 16);
            }
            append(n - _size, c);
        }
        _size = n;
    }

    void string::reserve(size_t n) {
        if (n > _capacity) {
            char* tmp = new char[n + 1];
            strcpy(tmp, _str);
            delete[] _str;
            _str = tmp;
            _capacity = n;
        }
    }

    void string::push_back(char c) {
        append(1, c);
    }

    string& string::operator+=(char c) {
        append(1, c);
        return *this;
    }

    string& string::append(size_t n, char ch) {
        size_t newsize = n + _size;
        if (_capacity < newsize) {
            reserve(n + _size + 1);
        }
        for (size_t i = _size; i < newsize; ++i) {
            _str[i] = ch;
        }
        _str[newsize] = '\0';
        _size = newsize;
        return *this;
    }

    string& string::append(const char* str) {
        int len = strlen(str);
        if (_capacity < len + _size) {
            reserve(len + _size + 1);
        }
        strcpy(_str + _size, str);
        _size = len;
        return *this;
    }

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

    void string::clear() {
        _str[0] = '\0';
        _size = 0;
    }

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

    const char* string::c_str()const {
        return _str;
    }
    // access
    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];
    }

    //relational operators
    bool string::operator<(const string& s) {
        for (size_t i = 0; i < _size && i < s.size(); ++i) {
            if (_str[i] != s[i]) {
                return _str[i] < s[i];
            }
        }
        if (_size < s.size()) {
            return true;
        }
        return false;
    }
    bool string::operator<=(const string& s) {
        return (*this < s) || (*this == s);
    }

    bool string::operator>(const string& s) {
        for (size_t i = 0; i < _size && i < s.size(); ++i) {
            if (_str[i] != s[i]) {
                return _str[i] > s[i];
            }
        }
        if (_size > s.size()) {
            return true;
        }
        return false;
    }

    bool string::operator>=(const string& s) {
        return (*this > s) || (*this == s);
    }

    bool string::operator==(const string& s) {
        if (_size != s.size()) {
            return false;
        }
        for (size_t i = 0; i < _size && i < s.size(); ++i) {
            if (_str[i] != s[i]) {
                return false;
            }
        }
        return true;
    }

    bool string::operator!=(const string& s) {
        return !(*this == s);
    }

    // 返回c在string中第一次出现的位置
    size_t string::find(char c, size_t pos) const {
        for (size_t i = pos; i < _size; ++i) {
            if (_str[i] == c) {
                return i;
            }
        }
        return npos;
    }

    // 返回子串s在string中第一次出现的位置
    size_t string::find(const char* s, size_t pos) const {
        for (size_t i = pos; i < _size; ++i) {
            if (_str[i] == s[0]) {
                int len = strlen(s);
                int sign = 0;
                for (int k = 0; k < len && k + i < _size; ++k) {
                    if (s[k] != _str[i + k]) {
                        sign = 1;
                        break;
                    }
                }
                if (sign == 0) {
                    return i;
                }
            }
        }
        return npos;
    }

    // 在pos位置上插入字符c/字符串str,并返回该字符的位置
    string& string::insert(size_t pos, char c) {
        assert(pos <= _size);
        if (_capacity < _size + 1) {
            reserve(2 * _capacity);
        }
        _size = _size + 1;
        _str[_size] = '\0';
        for (size_t i = _size - 1; i > pos; --i) {
            _str[i] = _str[i - 1];
        }
        _str[pos] = c;
        return *this;
    }

    string& string::insert(size_t pos, const char* str) {
        int len = size() + strlen(str);
        char* tmp = new char[len + 1];
        strncpy(tmp, _str, pos);
        strcpy(tmp + pos, str);
        strcpy(tmp + pos * 2, _str + pos);
        delete[] _str;
        _str = tmp;
        _size = len;
        _capacity = len;
        return *this;
    }

    // 删除pos位置上的元素,并返回该元素的下一个位置
    string& string::erase(size_t pos, size_t len) {
        if (pos + len > _size) {
            _str[pos] = '\0';
            _size = pos;
        }
        else {
            size_t i;
            for (i = pos; i < pos + len && i + len < _size; ++i) {
                _str[i] = _str[i + len];
            }
            _str[i] = '\0';
            _size = i;
        }
        return *this;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

云雷屯176

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

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

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

打赏作者

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

抵扣说明:

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

余额充值