/*
在构造简易版vector<string>时遇到这两个问题,代码如下:
*/
#include<memory>
#include<iostream>
#include<string>
#include<utility>
#include<vector>
class StrVec {
public:
StrVec() :elements(nullptr), first_free(nullptr), cap(nullptr) { }
StrVec(const StrVec&);
StrVec(std::initializer_list<std::string>);
StrVec &operator=(const StrVec&);
~StrVec();
void push_back(const std::string);
size_t size() const { return first_free - elements; }
size_t capicity() const { return cap - elements; }
void reserve(size_t n);
void resize(size_t n);
string show(std::vector<std::string>::size_type i);
std::string *begin() const { return elements; }
std::string *end() const { return first_free; }
private:
static std::allocator<std::string> alloc;
void chk_n_alloc() { if (size() == capicity()) 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;
};
void StrVec::push_back(const std::string s) {
chk_n_alloc();
alloc.construct(first_free++, s);
}
std::pair<std::string*, std::string*> StrVec::alloc_n_copy(const std::string *s1, const std::string *s2) {
auto data = alloc.allocate(s2 - s1);
return { data, uninitialized_copy(s1, s2, 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& st) {
auto newdata = alloc_n_copy(st.begin(), st.end());
elements = newdata.first;
first_free = newdata.second;
cap = newdata.second;
}
StrVec::StrVec(std::initializer_list<std::string> s) {
auto newdata = alloc_n_copy(s.begin(), s.end());
elements = newdata.first;
first_free = newdata.second;
cap = newdata.second;
}
StrVec::~StrVec() {
this->free();
}
StrVec &StrVec::operator=(const StrVec& st) {
auto newdata = alloc_n_copy(st.begin(), st.end());
this->free();
elements = newdata.first;
first_free = newdata.second;
cap = newdata.second;
return *this;
}
void StrVec::reallocate() {
auto newcapcity = this->size() ? 2 * this->size() : 1;
auto newdata = alloc.allocate(newcapcity);
auto dest = newdata;
auto elem = this->elements;
for (size_t i = 0; i < this->size(); ++i) {
alloc.construct(dest++, std::move(*elem++));
}
this->elements = newdata;
this->first_free = dest;
this->cap = this->elements + newcapcity;
}
void StrVec::reserve(size_t n) {
if (n > this->capicity()) {
auto newdata = alloc.allocate(n);
auto dest = newdata;
auto elem = this->elements;
for (size_t i = 0; i < this->size(); ++i) {
alloc.construct(dest++, std::move(*elem++));
}
this->elements = newdata;
this->first_free = dest;
this->cap = this->elements + n;
}
}
void StrVec::resize(rsize_t n) {
if (n <= this->capicity()) {
if (n > this->size()) {
auto beg = this->first_free;
for (size_t i = 0; i != n - (this->first_free - this->elements); ++i) {
alloc.construct(beg++);
}
this->first_free = beg;
}
else if (n < this->size()) {
auto beg = this->first_free;
for (; beg != this->first_free - n;) {
alloc.destroy(--beg);
}
this->first_free = beg;
}
}
}
std::string StrVec::show(std::vector<std::string>::size_type i) {
return *(elements + i);
首先,C4996这个问题,解决方案:
1、打开project的属性
2、打开c/c++目录
3、点击预处理器
4、在右侧表单中编辑第一条“预处理器定义”
5、将报错提示中的问题填入,我的时_SCL_SECURE_NO_WARNINGS
6、应用确认
或者在头文件前加上:#pragma warning(disable:4996)
接着LNK2001 无法解析的外部符号 "private: static class std::allocator:
原因在于第26行,类内静态变量未初始化,删去static或者在类外定义