• c++ string的简单实现


    string的简单实现

    #include 
    #include 
    
    using namespace std;
    
    /*
     *  todo
     *  1. copy on write
     * */
    
    class String {
    public:
        using size_type = size_t;
        using value_type = char;
        using pointer = char *;
    
        //有参构造
         String(const char *src = nullptr) {
            init();
            if (nullptr == src) {
                return;
            }
            size_type sz = strlen(src);
            rep.Long = false;
            add(src, sz);
        }
    
        //拷贝
        String(const String &s) {
            copy(s);
        }
    
        //移动
        explicit String(String &&s) {
            rep = s.rep;
            if (s.rep.Long) {
                s.init();
            }
        }
    
        ~String() {
            destory();
        }
    
        //赋值
        String &operator=(const String &s) {
            destory();
            init();
            copy(s);
            return *this;
        }
    
        //赋值
        String &operator=(String &&s)  noexcept {
            destory();
            init();
            rep = s.rep;
            if (s.rep.Long) {
                s.init();
            }
            return *this;
        }
    
        //左移运算符重载
        friend ostream &operator<<(ostream &os, const String &s) {
            if (s.rep.Long) {
                os << s.rep.m_l.beg_;
            } else {
                os << s.rep.m_s.m_data_;
            }
            return os;
        }
    
        String &operator+=(const String &right) {
            add(right);
            return *this;
        }
    
        friend String operator+(const String &left, const String &right);
    
        //输出大小
        [[nodiscard]] size_t size() const {
            if (rep.Long) {
                return rep.m_l.size_;
            }
            return rep.m_s.size_;
        }
    
        [[nodiscard]] pointer c_str() const {
            if (rep.Long) {
                return rep.m_l.beg_;
            } else {
                return (pointer) rep.m_s.m_data_;
            }
        }
    
    private:
    
        void init() {
            rep.Long = false;
            rep.m_s.size_ = 0;
            rep.m_s.m_data_[0] = '\0';
        }
    
        void add(const String &s) {
            if (s.rep.Long) {
                add(s.rep.m_l.beg_, s.rep.m_l.size_);
            } else {
                add((pointer) s.rep.m_s.m_data_, s.rep.m_s.size_);
            }
        }
    
        void add(const char *src, int n) {
            if (!rep.Long) {
                if (!rep.m_s.add(src, n)) {
                    // 复制原值
                    m_rep other = this->rep;
                    rep.Long = true;
                    int sz = other.m_s.size_;
                    rep.m_l.init(sz + n + 1);
                    rep.m_l.add((pointer) other.m_s.m_data_, other.m_s.size_);
                }
            }
            if (rep.Long) {
                rep.m_l.add(src, n);
            }
        }
    
        void destory() {
            if (this->rep.Long) {
                if (rep.Long) {
                    rep.m_l.destroy_long();
                    init();
                }
            }
        }
    
        void copy(const String &str) {
            this->rep.Long = str.rep.Long;
            if (!str.rep.Long) {
                this->rep.m_s.copy(str.rep.m_s);
            } else {
                add(str.rep.m_l.beg_, str.rep.m_l.size_);
            }
        }
    
        // 长字符串
        class m_long {
        public:
            pointer beg_;
            pointer cap_;
            size_type size_;
    
            void destroy_long() {
                if (nullptr != beg_) {
                    delete[] beg_;
                    beg_ = nullptr;
                }
            }
    
            void init(size_type n) {
                beg_ = new value_type[n];
                cap_ = beg_ + n;
                size_ = 0;
            }
    
            void move(m_long *other) {
                beg_ = other->beg_;
                cap_ = other->cap_;
                size_ = other->size_;
            }
    
            void copy(m_long *other) {
                for (int i = 0; i < other->size_; i++) {
                    beg_[i] = other->beg_[i];
                }
                size_ = other->size_;
            }
    
            void enlarge(size_type n) {
                size_type room = cap_ - beg_;
                if (room < 1024) {
                    room = room * 2 + n;
                } else {
                    room += n;
                }
                m_long other;
                other.move(this);
    
                this->init(room);
                this->copy(&other);
    
                other.destroy_long();
            }
    
            void enLargeIfNeed(size_type n) {
                if (cap_ - beg_ < n) {
                    enlarge(n);
                }
            }
    
            void add(const char *str, size_t n) {
                if (nullptr == str || n == 0) {
                    return;
                }
                enLargeIfNeed(n);
                strncpy(beg_ + size_, str, n);
                size_ += n;
                beg_[size_] = '\0';
            }
        };
    
        enum {m_min_cap = (sizeof(m_long) - 1)/sizeof(value_type) > 2 ?
                          (sizeof(m_long) - 1)/sizeof(value_type) : 2};
    
        // 短字符串
        struct m_short {
            value_type m_data_[m_min_cap];
            unsigned char size_;
    
            void init(size_type n) {
                size_ = 0;
            }
    
            bool add(const char *str, size_t n) {
                if (nullptr == str || n == 0) {
                    return true;
                }
                if (size_ + n + 1 > m_min_cap) {
                    return false;
                }
                strncpy(m_data_ + size_, str, n);
                size_ += n;
                m_data_[size_] = '\0';
                return true;
            }
    
            void copy(const m_short &other) {
                size_ = other.size_;
                strncpy(this->m_data_, other.m_data_, size_);
                this->m_data_[size_] = '\0';
            }
        };
    
        struct m_rep {
            bool Long{};
            union {
                m_long m_l;
                m_short m_s;
            };
        };
        m_rep rep;
    };
    
    String operator+(const String &left, const String &right) {
        String s;
        s.add(left);
        s.add(right);
        return s;
    }
    
    
    int main() {
        String s1("hello world");
        cout << s1.size() << " " << s1 << endl;
    
        String s2 = s1;
        cout << s2.size() << " "<< s2 << endl;
        String s3;
        cout << s3.size() << " " << s3 << endl;
    
        String s;
        s += "abcasdsdsadsdfsadfa fsdfsadas";
        cout << s << endl;
    }
    
    
    
    • 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

    输出

    在这里插入图片描述

    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds
    c++ string的简单实现x x s x x x x x x x xcxxcadds

  • 相关阅读:
    @Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法
    JVM系列 运行时数据区
    Java八股文 字节码
    修改时间服务器-域控环境
    Spring 框架的 MethodInterceptor 简介
    【1day】复现H3C多系列路由器敏感信息泄露漏洞
    C#迭代器的详细用法
    国内40多家金融机构数据被窃,多家知名基金上榜
    以太坊“共识层”客户端prysm和teku对比选型
    网络安全(黑客)工具篇
  • 原文地址:https://blog.csdn.net/qq_34179431/article/details/126733063