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

适合翻译做兼职的网站企业品牌推广方案

适合翻译做兼职的网站,企业品牌推广方案,网站面包屑如何做,网站开发朋友圈C实现简易String类 在C中&#xff0c;使用空间配置器&#xff08;allocator&#xff09;实现自定义string类需要管理内存分配、释放及对象构造/析构。 #include <memory> #include <algorithm> #include <cstring> #include <stdexcept> #include &l…

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.dtcms.com/wzjs/476226.html

相关文章:

  • 网站建设费属于研发费用吗站长工具大全集
  • 比较冷门的视频网站做搬运百度关键词优化培训
  • 做网站公司广州西安百度
  • 企业级网站开发需求分析大学生网络营销策划方案书
  • 做网站需不需要服务器湖北seo整站优化
  • 会计专业的简历制作迈步者seo
  • 西安 网站建设网站推广的主要方法
  • 网站域名的密码seo怎么学在哪里学
  • 营销型网站四大元素广东培训seo
  • 二手房网站开发文档qq推广
  • 邯郸网站设计费用打开百度一下
  • 杭州网站建设公司服务广告宣传语
  • 怎么选择网站建设公司广州网站优化步骤
  • 前几年做那个网站致富搜索网页内容
  • 网络营销指的是什么意思上海百度推广排名优化
  • 网站开发人员的 生活北京seo优化排名
  • 在网站用什么做页面布局查网站关键词工具
  • 正能量不良网站直接进入天天seo站长工具
  • 上海网站备案中心论坛企业推广
  • 简单网页制作工具太原百度快照优化排名
  • 海南网络电视台seo收费标准
  • 区政府网站建设搜索引擎营销与seo优化
  • 慧聪网怎样做网站友情链接网站百度收录秒收方法
  • php做网站为什么比java快系统优化大师下载
  • 微型购物网站建设模板郑州网站推广
  • 快速搭建网站框架关键字优化
  • 建设领域现场专业人员报名网站苏州seo网站公司
  • 营销网站建设平台营销推广48个方法
  • 河南省人民政府门户网站推介网
  • 北京代做网站aso优化师主要是干嘛的