#include "ex13_49_String.h"#include <algorithm>std::pair<char*, char*>String::alloc_n_copy(const char *b, const char *e){auto str = alloc.allocate(e - b);return{ str, std::uninitialized_copy(b, e, str) };}void String::range_initializer(const char *first, const char *last){auto newstr = alloc_n_copy(first, last);elements = newstr.first;end = newstr.second;}String::String(const char *s){char *sl = const_cast<char*>(s);while (*sl)++sl;range_initializer(s, ++sl);}String::String(const String& rhs){range_initializer(rhs.elements, rhs.end);}void String::free(){if (elements) {std::for_each(elements, end, [this](char &c){ alloc.destroy(&c); });alloc.deallocate(elements, end - elements);}}String::~String(){free();}String& String::operator = (const String &rhs){auto newstr = alloc_n_copy(rhs.elements, rhs.end);free();elements = newstr.first;end = newstr.second;return *this;}String::String(String &&s) NOEXCEPT : elements(s.elements), end(s.end){s.elements = s.end = nullptr;}String& String::operator = (String &&rhs) NOEXCEPT{if (this != &rhs) {free();elements = rhs.elements;end = rhs.end;rhs.elements = rhs.end = nullptr;}return *this;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。