string

简介

  • string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits 和allocator作为basic_string的默认参数
  • string在底层实际是:basic_string模板类的别名 -> typedef basic_string<char,char_traits, allocator> string
  • 不能操作多字节或者变长字符的序列

模拟实现

注意点:

cin/cout的重载(以字符为单位)

拷贝构造和=重载的 传统/现代写法

resize / reserve / insert / erase的写法(注意传参是否合规 / 超出)

代码 

#include <iostream>
#include <assert.h>
#include <string.h>

using namespace std;

namespace mystring
{
    class string
    {
    public:
        typedef char *iterator;
        // iterator

        iterator begin() const
        {
            return _str;
        }
        iterator end() const
        {
            return _str + _size;
        }

        friend ostream &operator<<(ostream &_cout, const string &s)
        {
            for (auto c : s) // 由于是按照字符为单位打印,那么中间即使有\0也不影响它的打印,所以需要修改其他地方的字符处理函数,否则会遇到\0就结束
            {
                _cout << c;
            }
            return _cout;
        }
        friend istream &operator>>(istream &_cin, string &s)
        {
            char c;
            s.clear();
            //_cin>>c;  无法使用普通的cin来读入空格/回车,所以无法停止,对于cin来说,他俩都是用于结束的符号,不读入
            c = _cin.get();
            while (c != ' ' && c != '\n')
            {
                s += c;
                c = _cin.get();
            }
            return _cin;
        }

    public:
        string(const char *str = "") : _size(strlen(str))
        {
            _capacity = _size;
            _str = new char[_size + 1];
            // strcpy(_str, str);
            memcpy(_str, str, _size);
        }
        string(const string &s) // 拷贝构造
        {
            //传统写法
            _str = new char[s._size];
            // strcpy(_str, s._str);
            memcpy(_str, s._str, s._size);
            _size = s._size;
            _capacity = s._capacity;

            // //现代写法
            // string tmp(s._str); //使用_str直接构造,当中间有\0时就只能拷贝一部分 -> 不建议使用
            // swap(tmp);  //这里由于没有处理自己(也就是this),成员数据可能会是随机值,换给tmp后,tmp调用析构函数时,就会释放一段不存在的空间->错误
            // //所以需要初始化this的成员

        }

        //传统写法
        // string &operator=(const string &s)
        // {
        //     _str = new char[s._size];
        //     // strcpy(_str, s._str);
        //     memcpy(_str, s._str, s._size);
        //     _size = s._size;
        //     _capacity = s._capacity;
        //     return *this;
        // }
        //现代写法
        string &operator=(string s)
        {
            swap(s);
            return *this;
        }

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

        // modify

        void push_back(char c)
        {
            if (_size == _capacity)
            {
                reserve(_capacity == 0 ? 4 : _capacity * 2);
            }
            _str[_size++] = c;
            _str[_size] = '\0';
        }
        string &operator+=(char c)
        {
            push_back(c);
            return *this;
        }
        void append(const char *str)
        {
            int len = strlen(str);
            if (_size + len > _capacity)
            {
                reserve(_size + len);
            }
            // strcpy(_str + _size, str);
            memcpy(_str + _size, str, len);
            _size += len;
        }
        string &operator+=(const char *str)
        {
            append(str);
            return *this;
        }
        void clear()
        {
            _size = 0;
            _str[0] = '\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 (_size == 0)
            {
                return true;
            }
            return false;
        }
        void resize(size_t n, char c = '\0')
        {
            if (n > _size)
            {
                reserve(n);
                for (int i = _size; i < n; i++)
                {
                    _str[i] = c;
                }
            }
            _size = n;
            _str[n] = '\0';
        }
        void reserve(size_t n)
        {
            if (n > _capacity)
            {
                char *tmp = new char[n + 1];
                _capacity = n;
                // strcpy(tmp, _str);
                memcpy(tmp, _str, _size);
                delete[] _str;
                _str = tmp;
            }
        }

        // 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) const
        {
            int ret = memcmp(_str, s._str, _size < s._size ? _size : s._size);
            return ret == 0 ? _size < s._size : ret < 0;
        }
        bool operator<=(const string &s) const{
            return *this<s||*this==s;
        }
        bool operator>(const string &s) const{
            return !(*this<=s);
        }
        bool operator>=(const string &s) const{
            return !(*this<s);
        }
        bool operator==(const string &s) const{
            return _size==s._size&&memcmp(_str, s._str, _size );
        }
        bool operator!=(const string &s) const{
            return !(*this==s);
        }

        // 返回c在string中第一次出现的位置

        size_t find(char c, size_t pos = 0) const
        {
            assert(pos < _size);
            for (int i = 0; i < _size; i++)
            {
                if (_str[i] == c)
                {
                    return i;
                }
            }
            return npos;
        }

        // 返回子串s在string中第一次出现的位置

        size_t find(const char *s, size_t pos = 0) const
        {
            assert(pos < _size);
            const char *ppos = strstr(_str, s);
            return ppos - _str;
        }

        // 在pos位置上插入字符c/字符串str,并返回该字符的位置

        string &insert(size_t pos, int n, char c)
        {
            assert(pos >= 0);
            assert(pos <= _size);
            if (n + _size > _capacity)
            {
                reserve(n + _size);
            }
            size_t end = _size;
            for (int i = 0; i < (_size - pos + 1); i++) // 移动的次数
            {
                _str[end + n] = _str[end];
                end--;
            }
            // size_t end = _size + n + 1; //+1是为了让pos位置的字符可以被移动(需要被移动的次数是_size+1-pos)
            // while (end != n)  //因为这里判断的是是否相等,如果不+1,pos位置的字符不会被处理
            // {
            //     end--;  //放前面减是为了让 第一次循环的开始位置 正确
            //     _str[end] = _str[end - n];
            // }
            for (int i = 0; i < n; i++)
            {
                _str[pos + i] = c;
            }
            _size += n;
            return *this;
        }
        string &insert(size_t pos, const char *str)
        {
            assert(pos >= 0);
            assert(pos <= _size);
            int len = strlen(str);
            if (len + _size > _capacity)
            {
                reserve(len + _size);
            }
            size_t end = _size;
            for (int i = 0; i < (_size - pos + 1); i++) // 移动的次数
            {
                _str[end + len] = _str[end];
                end--;
            }
            for (int i = 0; i < len; i++)
            {
                _str[pos + i] = str[i];
            }
            _size += len;
            return *this;
        }

        // 删除pos位置上的元素的n个元素

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

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

结构

vs2019 32位

string占28个字节

 它是有一个联合体,该联合体用来定义string中字符串的存储空间,以及一个size_t字段保存字符串长度,一个size_t字段保存从堆上开辟空间总的容量,一个指针做一些其他事情 

底层 

介绍 

bx是一个联合体

  • buf是一个16个字节的char数组
  • ptr是一个指针,指向一块堆上的空间
  • alias是一个监视窗口,不用管
  • 所以bx大小就是最大buf的大小 -> 16

mysize记录字符串有效长度

myres记录开辟的空间大小

myproxy是一个指针类型(不知道干嘛的)

所以16+4+4+4=28 

字符串存储位置

g++ 32位

string大小为4字节

g++下,string是通过写时拷贝实现的,内部只包含了一个指针,该指针将来指向一块堆空间,用来存储字符串,且内部包含了一个结构体

底层

写时拷贝现象 

通过上面的代码,我们可以发现只有当修改的时候,才会另开辟空间,这就是写时拷贝

写时拷贝
  • 有时候,我们并不需要修改这个拷贝出来的对象,那么为什么不采用共享资源这种方式呢?既然能共享,就没必要再复制一份了,太浪费空间
  • 所以,为了保证效率,采用写时拷贝是一个很好的方法
  • 当有多个对象共享该资源,其中一个对象试图修改共享资源时,才进行实际的拷贝操作
  • 写时拷贝这个方法非常多地方都有用到,比如linux下子进程继承的父进程的数据等等
  • 根据不同的编译器或标准库实现,可能会有不同的优化策略或内部实现,所以不是所有的string都会写时拷贝
  • 例如vs2019下,是直接进行拷贝

说是要拷贝,但拷贝其实有两种方式,深拷贝和浅拷贝 

浅拷贝问题

问题
  • 当我们没有自定义的拷贝构造时,系统会自动调用系统合成的默认拷贝构造,但该函数只进行了浅拷贝
  • 浅拷贝是简单的赋值,对于其他一般类型来说完全够用,但对于指针来说,一旦浅拷贝就会面临两个问题
引用计数
  • 对于析构两次的问题,可以引入引用计数来解决
  • 它是一种内存管理技术,用于跟踪共享资源的引用数目
  • 记录下与这块空间绑定的对象个数,对象析构后该计数就-1,直到为0,才释放空间,这样可以保证释放该资源的内存的安全性
深拷贝
  • 对象之间的修改会被影响只能通过深拷贝来解决
  • 它用于创建一个新的数据副本,并将原始数据及其内容完全复制到新的内存空间中,这意味着新的副本与原始数据之间是相互独立的,对其中一个进行修改不会影响另一个
  • 但是一拷贝就深拷贝,万一用不上呢?是不是就可以联想到前面说到的写时拷贝,当被修改时,再深拷贝,来避免不必要的复制操作
  • 当然如果引用计数为1,表示没有其他对象,可以直接修改原始资源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值