#include<iostream>
#include<memory>
#include<initializer_list>
#include<string>
#include<utility>
using namespace std;
class StrVec{
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;
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;}
void reserve(size_t ); //并不改变容器中元素的数量,它仅仅影响StrVec预先分配
string *begin() const{return elements;}
string *end() const{return first_free;}
void resize(size_t);
void resize(size_t,const string&);
};
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 p=first_free;p!=elements;)
alloc.destroy(--p);
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 &s){
auto data=alloc_n_copy(s.begin(),s.end());
free();
elements=data.first;
first_free=cap=data.second;
return *this;
}
void StrVec::reallocate(){
auto newcapacity=size()?2*size():1;
auto newdata=alloc.allocate(newcapacity);
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+newcapacity;
}
void StrVec::reserve(size_t sz){
if(sz>capacity()){
auto newdata=alloc.allocate(sz);
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+sz;
}
}
void StrVec::resize(size_t n){
if(n>size()){
while(size()<n) push_back("");
}
else if(n<size()){
while(size()>n)
alloc.destroy(--first_free);
}
}
void StrVec::resize(size_t n,const string &s){
if(n>size()){
while(n<size())
push_back(s);
}
else if(n<size()){
while(n>size())
alloc.destroy(--first_free);
}
}
int main(int argc, char *argv[])
{
return 0;
}
编译产生的错误:120 4 d:\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\ext\new_allocator.h [Error] no matching function for call to 'std::basic_string::basic_string(std::basic_string*)',应该如何解决