#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <set>
#include <memory>
#include <vector>
using namespace std;
class strVec{
public:
strVec():elements(nullptr),first_free(nullptr),cap(nullptr){};
strVec(const strVec &);
strVec & operator=(const strVec &);
~strVec();
void push_back(const 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:
static allocator<string> alloc;
void chk_n_alloc(){if(size() == capacity()) reallocate();}
pair<string *,string *> alloc_n_copy(const string *,const string *);
void free();
void reallocate();
string * elements;
string * first_free;
string * cap;
};
allocator<string> strVec::alloc;
void strVec::push_back(const string & s) {
chk_n_alloc();
alloc.construct(first_free++,s);
}
pair<string *,string *>
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 i = first_free; i != elements; ){
alloc.destroy(i--);
}
alloc.deallocate(elements,cap - elements);
}
}
strVec::strVec(const strVec &s) {
auto newdata = alloc_n_copy(s.begin(),s.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
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::reallocate() {
auto newcapacity = size() ? 2 * size() : 1;
auto p = alloc.allocate(newcapacity);
auto ele = elements;
auto dest = p;
for(int i = 0; i < size(); ++i)
alloc.construct(dest ++,std::move(*ele++));
free();
elements = p;
first_free = dest;
cap = elements + newcapacity;
}
int
main() {
return 0;
}
转载:https://blog.csdn.net/tianyingang/article/details/101029698
查看评论