C++ Primer(第5版) 练习 16.16
练习 16.16 将StrVec类(参见13.5节,第465页)重写为模板,命名为Vec。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
#include<iostream>
#include<memory>
#include<utility>
using namespace std;
template <typename T> class Vec{
public:
Vec(): elements(nullptr), first_free(nullptr), cap(nullptr) {}
Vec(const Vec &);
Vec &operator= (const Vec &);
~Vec();
void push_back(const T &);
size_t size() const { return first_free - elements; }
size_t capacity() const { return cap - elements; }
T *begin() const { return elements; }
T *end() const { return first_free; }
void reserve(std::size_t s);
void resize(std::size_t s);
private:
Static std::allocator<T> alloc;
void chk_n_alloc(){ if (size() == capacity()) reallocate(); }
std::pair<T*, T*>alloc_n_copy(const T*, const T*);
void free();
void reallocate();
T *elements;
T *first_free;
T *cap;
};
template <typename T>
void Vec<T>::push_back(const T &s){
chk_n_alloc();
alloc.construct(first_free++, s);
}
template <typename T>
std::pair<T*, T*> Vec<T>::alloc_n_copy(const T *b, const T *e){
auto data = alloc.allocate(e - b);
return {data, uninitialized_copy(b, e, data)};
}
template <typename T>
void Vec<T>:free(){
if(elements){
for(auto p = first_free; p != elements; ){
alloc.destroy(--p);
}
alloc.deallocate(elements, cap - elements);
}
}
template <typename T>
Vec<T>::Vec(const Vec &s){
auto newdata = alloc_n_copy(s.begin(), s.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
template <typename T>
Vec<T>::~Vec() { free(); }
template <typename T>
Vec<T> &Vec<T>::operator= (const Vec &rhs){
auto data = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = data.first;
first_free = cap = data.second;
return *this;
}
template <typename T>
void Vec<T>::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++, move(*elem++));
}
free();
elements = newdata;
first_free = dest;
cap = elements + newcapacity;
}
template <typename T>
void Vec<T>::reserve(std::size_t s){
if(s <= size()){
return;
}
auto newElem = alloc.allocate(s);
auto dest = newElem;
auto elem = elements;
for(size_t i = 0; i != size(); ++i){
alloc.construct(dest++, move(*elem++));
}
free();
elements = newElem;
cap = newElem + s;
first_free = dest;
}
template <typename T>
void Vec<T>::resize(std::size_t s){
if(s > capacity()){
return ;
}
if(s < size()){
auto newFisrt = first_free;
for(size_t i = 0; i != size() - s; ++i){
alloc.destroy(--newFirst);
}
fisrt_free = newFirst;
return ;
}
else if(s == size()){
return ;
}
else{
auto newFirst = first_free;
for(size_t i = 0; i != s - size(); ++i){
alloc.construct(newFirst++, "");
}
first_free = newFirst;
return ;
}
}
标签:alloc,elements,465,first,free,Vec,size,C++
From: https://blog.csdn.net/navicheung/article/details/140837380