C++ Primer 第五版 中文版 练习 13.39
题目:编写你自己版本的StrVec,包括自己版本的 reserve、capacity和resize。
答:
StrVec.h
#ifndef STR_VEC_H
#define STR_VEC_H
#include <string>
#include <memory>
#include <utility>
class StrVec{
public:
StrVec() :elements(nullptr), first_free(nullptr), cap(nullptr) {}
StrVec(const StrVec&);
StrVec& operator=(const StrVec&);
~StrVec();
void push_back(const std::string &);
size_t size() const { return first_free - elements; }
size_t capacity() const { return cap - elements; }
std::string *begin() const { return elements; }
std::string *end() const { return first_free; }
void reserve(size_t count); //重分配容量
void resize(size_t); //重置元素个数
// void resize(size_t, std::string &s = std::string());
private:
static std::allocator<std::string> alloc; //分配元素
void chk_n_alloc() { if (size() == capacity()) reallocate(); }
std::pair<std::string *, std::string *> alloc_n_copy(const std::string *, const std::string *);
void free();
void reallocate();
std::string *elements; //分配的内存中的首元素
std::string *first_free; //最后一个实际元素之后的位置
std::string *cap; //分配的内存末尾之后的位置
};
#endif
StrVec.cpp
#define _SCL_SECURE_NO_WARNINGS
#include "StrVec.h"
using namespace std;
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 &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 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 count)
{
if (size() < count){
auto newdata = alloc.allocate(count);
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 + count;
}
}
void StrVec::resize(size_t count)
{
if (count < size()){
for (auto iter = elements+count; iter != first_free; ++iter)
alloc.destroy(iter);
first_free = elements + count;
}
if (count > size())
{
for (size_t i = 0; i != count-size(); ++i)
alloc.construct(first_free++, string());
}
}
做这个题还碰着个错误:error C4996: 'std::_Uninitialized_copy0': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS.
解决办法:
在源代码前预定义:
#define _SCL_SECURE_NO_WARNINGS
或者在项目里面设置如下: