#include
#include
#include
#include
#include
using namespace std; class StrVec { public: StrVec () : elements (nullptr), first_free (nullptr), cap (nullptr) {}; //add a construct function receive initializer_list
StrVec(initializer_list
); StrVec(const StrVec&); StrVec(StrVec &&)_NOEXCEPT; StrVec &operator=(initializer_list
); StrVec &operator=(const StrVec&); StrVec &operator=(StrVec&&); string &operator[](size_t); const string &operator[](size_t)const; ~StrVec (); void push_back (const string&); void push_back (string&&); size_t size ()const { return first_free - elements; } size_t capacity ()const { return cap - elements; } string *begin ()const { return elements; } string *end ()const { return first_free; } private: allocator
alloc; void cnk_n_alloc () { if (size () == capacity ())reallocate (); } pair
alloc_n_copy (const string*, const string*); void free (); void reallocate (); string *elements; string *first_free; string *cap; }; StrVec::StrVec (StrVec&& sv)_NOEXCEPT { elements = sv.elements; first_free = sv.first_free; cap = sv.cap; sv.elements = sv.cap = sv.first_free = nullptr; } StrVec &StrVec::operator=(StrVec &&rhs) { if (this != &rhs) { free (); elements = rhs.elements; first_free = rhs.first_free; cap = rhs.cap; rhs.elements = rhs.cap = rhs.first_free = nullptr; } return *this; } void StrVec::push_back (const string &s) { cnk_n_alloc (); alloc.construct (first_free++, s); } void StrVec::push_back (string &&s) { cnk_n_alloc (); alloc.construct (first_free++, std::move (s)); } StrVec::StrVec (initializer_list
lst):elements (nullptr), first_free (nullptr), cap (nullptr) { for (const auto elem : lst) push_back (elem); } pair
StrVec::alloc_n_copy(const string *b, const string *e) { auto data = alloc.allocate (e - b); return{ data, uninitialized_copy (b, e, data) }; } void StrVec::free () { if (elements) for (auto q = first_free; q != elements; /*empty*/) alloc.destroy (--q); /* 另一种方法为: for_each (elements, first_free, [&] (string& elem){alloc.destroy (&elem);}); */ alloc.deallocate (elements, cap - elements); } StrVec::StrVec (const StrVec &rhs) { auto data = alloc_n_copy (rhs.begin (), rhs.end ()); elements = data.first; first_free = cap = data.second; } StrVec::~StrVec () { free (); } StrVec &StrVec::operator=(initializer_list
lst) { auto data = alloc_n_copy (lst.begin (), lst.end ()); free (); elements = data.first; first_free = cap = data.second; return *this; } string &StrVec::operator[](size_t n) { return elements[n]; } const string &StrVec::operator[](size_t n)const { return elements[n]; } 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::reallocate () { auto newcapacity = size () ? 2 * size () : 1; auto data = alloc.allocate (newcapacity); auto dest = data; auto elem = elements; for (size_t i = 0; i != size (); ++i) alloc.construct (dest++, move (*elem++)); /*另一种方法为: auto first = alloc.allocate(newcapacity); auto last = uninitialized_copy (make_move_iterator (begin ()), make_move_iterator (end ()), first); */ free (); elements = data; first_free = dest; cap = elements + newcapacity; } void print (string name, const StrVec &sv) { cout << name << ": "; for (auto sv_it = sv.begin (); sv_it != sv.end (); ++sv_it) cout << *sv_it << " "; cout << "\n----------------" << endl; } void StrVec_move_test () { //use initializer_list initialize it StrVec sv{ "fuck" }; string line; while (cin >> line) { sv.push_back (line); } print ("sv", sv); StrVec new_sv (std::move (sv)); print ("moved sv", sv); print ("The new sv", new_sv); sv = std::move (new_sv); print ("moved new sv", new_sv); print ("final sv", sv); } int main () { ios::sync_with_stdio (false); StrVec svec{ "fuck" }; const StrVec cvec = svec; //if svec have element, empty the first element if (svec.size () && svec[0].empty ()) { svec[0] = "Zero"; } return 0; }
一个类似vector的类
最新推荐文章于 2020-03-30 10:21:31 发布