• 【STL】自定义string类


    string的文档介绍

    1. string是表示字符序列的类
    2. 标准的字符串类提供了对此类对象的支持,其接口类似于标准字符容器的接口,但添加了专门用于操作 单字节字符字符串的设计特性。
    3. string类是使用char(即作为它的字符类型,使用它的默认char_traits和分配器类型(关于模板的更多信 息,请参阅basic_string)。
    4. string类是basic_string模板类的一个实例,它使用char来实例化basic_string模板类,并用char_traits和allocator作为basic_string的默认参数(根于更多的模板信息请参考basic_string)。
    5. 注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个 类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。
    1. string是表示字符串的字符串类
    2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
    3. string在底层实际是:basic_string模板类的别名,typedef basic_stringstring;
    4. 不能操作多字节或者变长字符的序列。

    深浅拷贝

    浅拷贝:也称位拷贝,编译器只是将对象中的值拷贝过来。如果对象中管理资源,最后就会导致多个对象共享同一份资源,当一个对象销毁时就会将该资源释放掉,而此时另一些对象不知道该资源已经被释放,以为还有效,所以 当继续对资源进项操作时,就会发生发生了访问违规。
    深拷贝:如果一个类中涉及到资源的管理,其拷贝构造函数、赋值运算符重载以及析构函数必须要显式给出。一般情况都是按照深拷贝方式提供。给每个对象独立分配资源,保证多个对象之间不会因为共享资源而造成多次释放导致程序崩溃问题。

    实现的接口

    默认成员函数部分

    string(const char* str = “”)string(const string& s);
    string& operator=(const string &s)~string()
    
    • 1
    • 2
    • 3
    • 4

    修改及访问部分

    void push_back(char c);
    string& operator+=(char c);
    void append(const char* str);
    string& operator+=(const char* str);
    void clear();
    void swap(string& s);
    const char* c_str()const;
    // 返回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, size_t len);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    迭代器部分

    iterator begin();
    iterator end();
    const_iterator begin() const
    const_iterator end() const
    
    • 1
    • 2
    • 3
    • 4

    容量部分

    size_t size()const
    size_t capacity()const
    bool empty()const
    void resize(size_t n, char c = ‘\0);
    void reserve(size_t n);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    方括号访问部分

    char& operator[](size_t index);
    const char& operator[](size_t index)const;
    
    • 1
    • 2

    关系运算符部分

    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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    具体的代码实现

    #define _CRT_SECURE_NO_WARNINGS
    #include 
    #include 
    #include 
    using namespace std;
    namespace lc
    {
        class string
        {
        public:
            typedef char* iterator;
            typedef const char* const_iterator;
            friend std::ostream& operator<<(std::ostream& _cout, const string& s);
            friend std::istream& operator>>(std::istream& _cin, string& s);
            string(const char* str = "")  //构造空的string类对象,即空字符串
            {                     
                _size = strlen(str);
                _capacity = _size;
                _str = new char[_capacity + 1];
                strcpy(_str, str);
            }
     
            //传统方法
            //string(const string& s)  
            //    :_str(new char[s._capacity + 1])
            //    , _capacity(s._capacity)
            //    , _size(s._size)
            //{
            //    strcpy(_str, s._str);
            //}
            string(const string& s)   //拷贝构造、现代写法
                :_str(nullptr)
                , _capacity(0)
                , _size(0)
            {
                string tmp(s._str);
                swap(tmp);
            }
            //s3 = s1
            //string& operator=(const string& s)
            //{
            //    if (this != &s)
            //    {
            //        
            //        char* tmp = new char[s._capacity + 1];
            //        strcpy(tmp, _str);
            //        _capacity = s._capacity;
            //        _size = s._size;
            //        delete[] _str;
            //        _str = tmp;
            //    }
            //    return *this;
            //}
            string& operator=(const string& s)   //拷贝赋值(现代写法)
            {
                if (this != &s)
                {
                    string tmp(s._str);
                    swap(tmp);
                }
                return *this;
            }
    
            ~string()   //析构函数
            {
                delete[] _str;
                _str = nullptr;
                _capacity = _size = 0;
            }
           // begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
            iterator begin()
            {
                return _str;
            }
            iterator end()
            {          
                return _str + _size;
            }
            const_iterator begin() const
            {
                return _str;
            }
            const_iterator end() const
            {
                return _str + _size;
            }
            void reserve(size_t n)   //为字符串预留空间
            {
                if (n > _capacity)
                {
                    char* tmp = new char[n + 1];
                    _capacity = n;
                    strcpy(tmp, _str);
                    delete[] _str;
                    _str = tmp;
                }
    
            }
            void push_back(char c)  //在字符串后尾插字符c
            {
                //if (_size == _capacity)
                //{
                //    reserve(_capacity == 0 ? 8 : 2 * _capacity);
                //}
                //_str[_size] = c;
                //_size++;
                //_str[_size] = '\0';
                insert(_size, c);
            }
            size_t size()const  //返回字符串有效字符长度
            {
                return _size;
            }
            size_t capacity()const  //返回空间总大小
            {
                return _capacity;
            }
    
            void append(const char* str)  //在字符串后追加一个字符串
            {
                //size_t len = strlen(str);
                //if (_size + len > _capacity)
                //{
                //    reserve(_size + len);
                //}
                //strcpy(_str + _size, str);
                //_size += len;
                insert(_size, str);
            }
            string& insert(size_t pos, char ch)  //在某个位置后插入字符
            {
                assert(pos <= _size);
                if (_size == _capacity)
                {
                    reserve(_capacity == 0 ? 4 : 2 * _capacity);
                }
                size_t end;
                for (end = _size + 1; end > pos; end--)
                {
                    _str[end] = _str[end - 1];
                }
                _str[pos] = ch;
                _size++;
                return *this;
            }
            string& insert(size_t pos, const char* str)  //在某个位置后插入字符串
            {
                assert(pos <= _size);
                size_t len = strlen(str);
                reserve(_size + len);
                size_t end = _size + 1;
                while (end > pos)
                {
                    _str[end + len - 1] = _str[end - 1];
                    end--;
                }
                strncpy(_str + pos, str, len);
                _size += len;
                return *this;
            }
            void erase(size_t pos, size_t len = npos)  // 删除pos位置上的元素
            {
                assert(pos < _size);
                if (len == npos || len + pos >= _size)
                {
                    _str[pos] = '\0';
                    _size = pos;
                }
                else
                {
                    strcpy(_str + pos, _str + pos + len);
                    _size -= len;
                }
            }
            size_t find(char ch, size_t pos = 0) const //从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
            {
                assert(pos < _size);
                for (size_t i = pos; i < _size; i++)
                {
                    if (_str[i] == ch) return i;
                }
                return npos;
            }
            size_t find(const char* sub, size_t pos = 0) const  // 返回子串s在string中第一次出现的位置
            {
                assert(pos < _size);
                assert(sub);
                char* p = strstr(_str, sub);
                if (p != NULL)
                {
                    return p - _str;/*return find(*p, 0);*/
                }
                else
                    return npos;
    
            }
            string substr(size_t pos, size_t len = npos) const  //在str中从pos位置开始,截取len个字符,然后将其返回
            {
                assert(len < _size);
                size_t ch = len;
                string newstring;
                if (len == npos || len + pos > _size)
                {                             
                    ch = _size - pos;
                }
                for (size_t i = 0; i < ch; ch++)
                {
                    newstring += _str[i + pos];
                }
                return newstring;
            }
            //字符串大小比较
            bool operator>(const string& s) const
            {
                return strcmp(_str, s._str) > 0;
            }
            bool operator==(const string& s) const
            {
                return strcmp(_str, s._str) == 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 !(*this == s);
            }
            void append(size_t n, char ch)  //尾部追加一个字符
            {
                reserve(_size + n);
                for (size_t i = 0; i < n; i++)
                {
                    _str[_size + i] = ch;
                }           
            }
            string& operator+=(const char* str)  //在字符串后追加字符串str
            {
                append(str);
                return *this;
            }
            string& operator+=(char ch)  //追加字符
            {
                push_back(ch);
                return *this;
            }
            void clear()  //清空字符串
            {
                _size = 0;
                _str[_size] = '\0';
            }
            void swap(string& s)
            {
                ::swap(_str, s._str);
                ::swap(_capacity, s._capacity);
                ::swap(_size, s._size);
            }
            const char* c_str()const   //返回C格式字符串
            {
                return _str;
            }
    
            bool empty()const    //判空
            {
                return _size == 0;
            }
            void resize(size_t n, char c = '\0')  //将有效字符的个数该成n个,多出的空间用字符c填充
            {
                if (n > _size)
                {
                    reserve(n);
                    for (size_t i = _size; i <n ; i++)
                    {
                        _str[i] = c;
                    }
                    _size = n;
                    _str[n] = '\0';
                }
                else
                {
                    _str[n] = '\0';
                    _size = n;
                }
            }
            char& operator[](size_t index)  //返回index位置的字符,(const)string类对象调用
            {
                assert(index < _size);
                return _str[index];
            }
            const char& operator[](size_t index)const
            {
                assert(index < _size);
                return _str[index];
            }
        //private:
            size_t _capacity;
            char* _str;
            size_t _size;
            static size_t npos;
        };
        size_t string::npos = -1;
        //输出运算符重载
        std::ostream& operator<<(std::ostream& out, const string& s)
        {
            out << s._str;
            return out;
        }
    	//输入运算符重载
        std::istream& operator>>(std::istream& in, string& s)
        {
            while (1)
            {
                char ch;
                ch = in.get();
                if (ch == '\n' || ch == ' ')
                {
                    break;
                }
                else
                {
                    s.push_back(ch);
                }
            }
    
            return in;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
  • 相关阅读:
    mysql主从同步
    Application启动流程
    括号有效配对题型问题解法
    [教你做小游戏] 只用几行原生JS,写一个函数,播放音效、播放BGM、切换BGM
    RPA是什么意思?RPA机器人很厉害吗?
    基于springboot的医院挂号管理系统
    pytorch 笔记:KLDivLoss
    【Python爬虫】selenium4新版本使用指南
    mysql之SQL练习
    java毕业设计——基于java+MyBatis+jsp的网上招聘系统设计与实现(毕业论文+程序源码)——网上招聘系统
  • 原文地址:https://blog.csdn.net/ll1175555/article/details/127889914