C++ Primer 第五版 中文版 练习 13.39 个人code
2014-10-11 17:43
465 查看
C++ Primer 第五版 中文版 练习 13.39
题目:编写你自己版本的StrVec,包括自己版本的 reserve、capacity和resize。
答:
StrVec.h
做这个题还碰着个错误: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.
解决办法:
在源代码前预定义:
或者在项目里面设置如下:
题目:编写你自己版本的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; //分配的内存末尾之后的位置 }; #endifStrVec.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
或者在项目里面设置如下:
相关文章推荐
- C++ primer 第五版 中文版 练习 10.24个人code
- C++ primer 第五版 中文版 练习 10.27个人code
- C++ primer 第五版 中文版 练习 10.37 个人code
- C++ primer 第五版 中文版 练习 8.13 个人 code
- C++ primer 第五版 中文版 练习 9.5 个人 code
- C++ primer 第五版 中文版 练习 10.34 个人code
- C++ primer 第五版 中文版 练习 10.42 个人code
- C++ primer 第五版 中文版 练习 12.19 个人code
- C++ Primer 第五版 中文版 练习 14.3 个人code
- C++ primer 第五版 中文版 练习 10.1 个人code
- C++ primer 第五版 中文版 练习 10.28 个人code
- C++ primer 第五版 中文版 练习 10.35 个人code
- C++ primer 第五版 中文版 练习 11.32 个人code
- C++ Primer 第五版 中文版 练习 13.26 个人code
- C++ primer 第五版 中文版 练习 9.45 个人code 及疑问
- C++ primer 第五版 中文版 练习 10.2 个人code
- C++ primer 第五版 中文版 练习 11.33 个人code
- C++ primer 第五版 中文版 练习 12.2 个人code
- C++ Primer 第五版 中文版 练习 13.14&13.15&14.16&14.17 个人code
- C++ Primer 第五版 中文版 练习 13.27 个人code