当前位置:网站首页>String Simulation Implementation

String Simulation Implementation

2022-06-11 07:41:00 m0_ fifty-two million twelve thousand six hundred and fifty-six

Source code :

#pragma once
#include<iostream>
#include<cassert>
#include<string>
namespace ztl
{
    using namespace std;
    class string
    {
        friend ostream& operator<<(ostream& _cout, const ztl::string& s);
        friend istream& operator>>(istream& _cin, ztl::string& s);
    public:
        typedef char* iterator;
    public:

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

        // Copy structure ( Modern writing )
        string(const string& s)
            :_size(strlen(s._str))
            , _capacity(_size)
        {
            _str = nullptr;
            string tem(s._str);
            swap(tem);
        }
        // Copy... Copy 
        string& operator=(const string& s)
        {
            if (this != &s)
            {
                string tmp(s);
                swap(tmp);
            }
            return *this;
        }
        // Destructor 
        ~string()
        {
            delete[] _str;
            _str = nullptr;
            _size = _capacity = 0;
        }

        // iterator Forward iterator 

        iterator begin()
        {
            return _str;
        }

        iterator end()
        {
            return _str + _size;
        }
        // Capacity expansion 
        void reserve(size_t n)
        {
            if (n > _capacity)
            {
                char* tmp = new char[n + 1];// to \0 Leave a space 
                strcpy(tmp, _str);
                delete[] _str;
                _str = tmp;
                _capacity = n;
            }
        }

        // modify
        void push_back(char c)
        {
            if (_size == _capacity)
            {
                reserve(_capacity == 0 ? 15 : _capacity * 2);
            }
            _str[_size] = c;
            ++_size;
            _str[_size] = '\0';
            //insert(_size, c);
        }

        string& operator+=(char c)
        {
            push_back(c);
            return *this;
        }

        void append(const char* str)
        {
            size_t len = strlen(str);
            if (_size + len > _capacity)
            {
                reserve(_size + len);
            }
            strcpy(_str + _size, str);
            _size += len;
        }

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

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

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

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

        // capacity

        size_t size()const
        {
            return _size;
        }

        size_t capacity()const
        {
            return _capacity;
        }

        bool empty()const
        {
            if (_str[0
            ] != '\0')
            {
                return false;
            }
            return true;
        }

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

        //void reserve(size_t n)
        //{
        //    if (n > _capacity)
        //    {
        //        char* tmp = new char[n + 1];// to \0 Leave a space 
        //        strcpy(tmp, _str);
        //        delete[] _str;
        //        _str = tmp;
        //        _capacity = n;
        //    }
        //}

        // access

        char& operator[](size_t index)
        {
            assert(index < _size);
            return *(_str + index);
        }

        const char& operator[](size_t index)const
        {
            assert(index < _size);
            return *(_str + index);
        }

        //relational operators

        bool operator<(const string& s)
        {
            if (strcmp(_str, s._str) < 0)
                return true;
            return false;
        }

        bool operator<=(const string& s)
        {
            if (strcmp(_str, s._str) <= 0)
                return true;
            return false;
        }

        bool operator>(const string& s)
        {

        }

        bool operator>=(const string& s)
        {
            if (strcmp(_str, s._str) >= 0)
                return true;
            return false;
        }

        bool operator==(const string& s)
        {
            if (strcmp(_str, s._str) == 0)
                return true;
            return false;
        }

        bool operator!=(const string& s)
        {
            if (strcmp(_str, s._str) != 0)
                return true;
            return false;
        }



        //  return c stay string The first place in 
        size_t npos = -1;
        size_t find(char c, size_t pos = 0) const
        {
            for (size_t i = 0; i < _size; ++i)
            {
                if (c == _str[i])
                {
                    return i;
                }
            }

            return npos;
        }

        //  Returns the substring s stay string The first place in 

        size_t find(const char* s, size_t pos = 0) const
        {
            char* ptr = strstr(_str + pos, s);
            if (ptr == nullptr)
            {
                return npos;
            }
            else
                return ptr - _str;
        }

        //  stay pos Insert character in position c/ character string str, And return the position of the character 

        string& insert(size_t pos, char c)
        {
            assert(pos <= _size);
            if (pos == _capacity)
            {
                reserve(_capacity == 0 ? 15 : _capacity * 2);
            }
            size_t end = _size + 1;
            while (end > pos)
            {
                _str[end] = _str[end - 1];
                end--;
            }
            _str[pos] = c;
            _size++;
            return *this;
        }

        string& insert(size_t pos, const char* str)
        {
            assert(pos <= _size);
            size_t len = strlen(str);
            if ((pos + len) > _capacity)
            {
                reserve(_size + len);
            }
            size_t end = _size + len;
            while (end > pos + len)
            {
                _str[end] = _str[end - len];
                end--;
            }
            strncpy(_str + pos, str, len);
            return *this;
        }
        //  Delete pos Elements in position , And return the next position of the element 

        string& erase(size_t pos, size_t len)
        {
            assert(pos < _size);
            if (len == pos || pos + len > _capacity)
            {
                _str[pos] = '\0';
                _size = pos;
            }
            else
            {
                strcpy(_str + pos, _str + pos + len);
                _size -= len;
            }
            return *this;

        }

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

    ostream& operator<<(ostream& _cout, const ztl::string& s)
    {
        cout << s._str;
        return _cout;
    }
    // Sure cin to string assignment 
    istream& operator>>(istream& _cin, ztl::string& s)
    {
        // If the original string has content , Just kill it first 
        if (s._str != NULL)
        {
            delete[] s._str;
            s._str = NULL;
            s._size = 0;
        }
        // Set the cache 
        char temp[2048] = { 0 };
        cout << " Input string :" << endl;
        cin >> temp;
        int len = strlen(temp);
        s._str = new char[len + 1];
        // Copy string 
        for (int i = 0; i <= len; i++)
        {
            s._str[i] = temp[i];
        }
        s._size = len;
        return _cin;
    }
    void test1()
    {
        ztl::string s1("hello");
        cout << s1 << endl;
        ztl::string s2("uuuu");
        cout << s2 << endl;
        s2 = s1;
        cout << s2 << endl;
        s1.clear();
        string s3(s1);
        cout << s3 << endl;
        s2.push_back('a');
        s2.insert(0,'a');
        cout << s2 << endl;
    }

};

原网站

版权声明
本文为[m0_ fifty-two million twelve thousand six hundred and fifty-six]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020519520541.html