//// ex13_40.cpp// Exercise 13.40//// Created by pezy on 2/3/15.//// Add a constructor that takes an initializer_list<string> to your StrVec class.//#include "ex13_40.h"void StrVec::push_back(const std::string &s){chk_n_alloc();alloc.construct(first_free++, s);}std::pair<std::string*, std::string*>StrVec::alloc_n_copy(const std::string *b, const std::string *e){auto data = alloc.allocate(e - b);return{ data, std::uninitialized_copy(b, e, data) };}void StrVec::free(){if (elements) {for (auto p = first_free; p != elements;)alloc.destroy(--p);alloc.deallocate(elements, cap - elements);}}void StrVec::range_initialize(const std::string *first, const std::string *last){auto newdata = alloc_n_copy(first, last);elements = newdata.first;first_free = cap = newdata.second;}StrVec::StrVec(const StrVec &rhs){range_initialize(rhs.begin(), rhs.end());}StrVec::StrVec(std::initializer_list<std::string> il){range_initialize(il.begin(), il.end());}StrVec::~StrVec(){free();}StrVec& StrVec::operator = (const StrVec &rhs){auto data = alloc_n_copy(rhs.begin(), rhs.end());free();elements = data.first;first_free = cap = data.second;return *this;}void StrVec::alloc_n_move(size_t new_cap){auto newdata = alloc.allocate(new_cap);auto dest = newdata;auto elem = elements;for (size_t i = 0; i != size(); ++i)alloc.construct(dest++, std::move(*elem++));free();elements = newdata;first_free = dest;cap = elements + new_cap;}void StrVec::reallocate(){auto newcapacity = size() ? 2 * size() : 1;alloc_n_move(newcapacity);}void StrVec::reserve(size_t new_cap){if (new_cap <= capacity()) return;alloc_n_move(new_cap);}void StrVec::resize(size_t count){resize(count, std::string());}void StrVec::resize(size_t count, const std::string &s){if (count > size()) {if (count > capacity()) reserve(count * 2);for (size_t i = size(); i != count; ++i)alloc.construct(first_free++, s);}else if (count < size()) {while (first_free != elements + count)alloc.destroy(--first_free);}}int main(){return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。