当前位置: 首页 > ds >正文

C++通过空间配置器实现简易String类

C++实现简易String类

在C++中,使用空间配置器(allocator)实现自定义string类需要管理内存分配、释放及对象构造/析构。

#include <memory>
#include <algorithm>
#include <cstring>
#include <stdexcept>
#include <utility>template<typename CharT, typename Allocator = std::allocator<CharT>>
class MyString {
public:using allocator_type = Allocator;using traits_type = std::char_traits<CharT>;using size_type = typename std::allocator_traits<Allocator>::size_type;private:CharT* data_;size_type size_;size_type capacity_;Allocator allocator_;void destroy_elements() {if (data_) {for (size_type i = 0; i < size_; ++i) {std::allocator_traits<Allocator>::destroy(allocator_, data_ + i);}std::allocator_traits<Allocator>::deallocate(allocator_, data_, capacity_ + 1);data_ = nullptr;size_ = 0;capacity_ = 0;}}public:MyString() noexcept : data_(nullptr), size_(0), capacity_(0), allocator_() {}explicit MyString(const CharT* s, const Allocator& alloc = Allocator()): allocator_(alloc) {size_ = traits_type::length(s);capacity_ = size_;if (size_ > 0) {data_ = std::allocator_traits<Allocator>::allocate(allocator_, capacity_ + 1);std::uninitialized_copy(s, s + size_, data_);std::allocator_traits<Allocator>::construct(allocator_, data_ + size_, CharT());} else {data_ = std::allocator_traits<Allocator>::allocate(allocator_, 1);std::allocator_traits<Allocator>::construct(allocator_, data_, CharT());}}MyString(const MyString& other): allocator_(std::allocator_traits<Allocator>::select_on_container_copy_construction(other.allocator_)) {size_ = other.size_;capacity_ = other.size_;if (size_ > 0) {data_ = std::allocator_traits<Allocator>::allocate(allocator_, capacity_ + 1);std::uninitialized_copy(other.data_, other.data_ + size_, data_);std::allocator_traits<Allocator>::construct(allocator_, data_ + size_, CharT());} else {data_ = std::allocator_traits<Allocator>::allocate(allocator_, 1);std::allocator_traits<Allocator>::construct(allocator_, data_, CharT());}}MyString(MyString&& other) noexcept: data_(other.data_), size_(other.size_), capacity_(other.capacity_), allocator_(std::move(other.allocator_)) {other.data_ = nullptr;other.size_ = 0;other.capacity_ = 0;}~MyString() {destroy_elements();}MyString& operator=(const MyString& other) {if (this != &other) {MyString temp(other);swap(*this, temp);}return *this;}MyString& operator=(MyString&& other) noexcept {if (this != &other) {destroy_elements();data_ = other.data_;size_ = other.size_;capacity_ = other.capacity_;allocator_ = std::move(other.allocator_);other.data_ = nullptr;other.size_ = 0;other.capacity_ = 0;}return *this;}friend void swap(MyString& a, MyString& b) noexcept {using std::swap;swap(a.data_, b.data_);swap(a.size_, b.size_);swap(a.capacity_, b.capacity_);swap(a.allocator_, b.allocator_);}const CharT* c_str() const noexcept {return data_ ? data_ : &CharT();}size_type size() const noexcept {return size_;}size_type capacity() const noexcept {return capacity_;}void reserve(size_type new_capacity) {if (new_capacity <= capacity_) return;size_type new_size = size_;CharT* new_data = std::allocator_traits<Allocator>::allocate(allocator_, new_capacity + 1);try {std::uninitialized_copy(data_, data_ + size_, new_data);} catch (...) {std::allocator_traits<Allocator>::deallocate(allocator_, new_data, new_capacity + 1);throw;}std::allocator_traits<Allocator>::construct(allocator_, new_data + new_size, CharT());destroy_elements();data_ = new_data;capacity_ = new_capacity;}void append(const CharT* s, size_type count) {if (count == 0) return;if (s >= data_ && s < data_ + size_) {size_type offset = s - data_;CharT* tmp = new CharT[count];traits_type::copy(tmp, data_ + offset, count);append_impl(tmp, count);delete[] tmp;} else {append_impl(s, count);}}private:void append_impl(const CharT* s, size_type count) {size_type new_size = size_ + count;if (new_size > capacity_) {reserve(std::max(new_size, capacity_ * 2));}std::uninitialized_copy(s, s + count, data_ + size_);size_ = new_size;std::allocator_traits<Allocator>::construct(allocator_, data_ + size_, CharT());}
};int main() {MyString<char> str("Hello");str.append(" world!", 7);return 0;
}

代码说明:

  1. 成员变量
    • data_:动态分配的字符数组指针。
    • size_:当前字符串长度。
    • capacity_:当前分配的内存容量(不含终止符)。
    • allocator_:空间配置器对象。
  2. 内存管理
    • 使用std::allocator_traits进行内存分配、释放和对象构造/析构。
    • destroy_elements()方法负责销毁对象并释放内存。
  3. 构造函数
    • 默认构造空字符串。
    • 从C风格字符串构造,计算长度并分配内存。
    • 拷贝构造和移动构造正确处理allocator传播。
  4. 赋值运算符
    • 使用拷贝交换惯用法保证异常安全。
    • 移动赋值直接转移资源所有权。
  5. 扩容机制
    • reserve()方法实现内存扩容,使用std::uninitialized_copy迁移数据。
    • append()方法处理自引用情况,避免迭代器失效。
  6. 其他功能
    • c_str()返回C风格字符串。
    • swap()方法交换所有成员,包括allocator。

该实现遵循RAII原则,确保资源安全,且兼容标准allocator机制。实际应用中需进一步处理异常安全、优化性能及支持更多字符串操作。

http://www.xdnf.cn/news/8073.html

相关文章:

  • 学习路之uniapp--unipush2.0推送功能--使用
  • 什么是智能体?
  • 顺序表VS单链表
  • RuntimeError: Cannot find sufficient samples, consider increasing dataset size.
  • 【Tauri2】047——Image
  • gcc还会有自己的头文件呢?
  • CMake 跨平台构建系统详解
  • 友达15.6寸G156HAN02.3工业显示模组
  • 在Linux系统上备份另一个系统的做法
  • 数据库主从集群 + GTID 实现高可用
  • inlier_outlier
  • 视觉大模型学习总结
  • 通过 curl 精准定位问题
  • 从零开始的嵌入式学习day25
  • Java SSM与SpringBoot面试题全面解析:从基础到源码
  • 线性表数据结构-队列
  • 8:点云处理—常见的四种3D相机
  • 今日行情明日机会——20250521
  • 探索Puter:一个基于Web的轻量级“云操作系统”
  • Java基础 5.21
  • 重磅升级!Google Play商店改版上线
  • Web服务器
  • C++语言的跨平台挑战和应对策略
  • centos7 p8p1使用ip addr查看时有的时候有两个ip,有的时候只有一个ip,有一个ip快,有一个ip慢
  • 如何在 Windows 10 或 11 上使用命令提示符安装 Angular
  • Vue Router动态路由与导航守卫实战
  • RESTful风格
  • 从零基础到最佳实践:Vue.js 系列(6/10):《Composition API(组合式 API)》
  • 论文篇目录-研究生如何阅读编写论文
  • Linux系统编程-DAY02