C++ Primer 13.5节练习
2016-04-13 20:32
721 查看
--------------------------------------------------StrVec.h------------------------------------------------------- #ifndef STRVEC_H #define STRVEC_H #define _SCL_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <memory> #include <initializer_list> using namespace std; class StrVec{ public: StrVec() :elements(nullptr), first_free(nullptr), cap(nullptr) {} StrVec(const initializer_list<string>&); 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; } string *begin() const { return elements; } string *end() const { return first_free; } void reserve(size_t); void resize(size_t n, const string &str); void printf(); private: static allocator<string> alloc; string *elements; string *first_free; string *cap; void check_n_alloc() { if (size() == capacity()) reallocate(); } void reallocate(); void free(); pair<string*, string*> alloc_n_copy(const string*, const string*); }; #endif
--------------------------------------------------StrVec.cpp------------------------------------------------------- #include "StrVec.h" allocator<string> StrVec::alloc; void StrVec::push_back(const string &str){ check_n_alloc(); alloc.construct(first_free++, str); } pair<string*, string*> StrVec::alloc_n_copy(const string *b, const string *e){ auto data = alloc.allocate(e - b); auto next_ptr = uninitialized_copy(b,e,data); return{ data, next_ptr }; } void StrVec::free(){ if (elements){ for (auto be = elements; be != first_free; ++be) alloc.destroy(be); alloc.deallocate(elements,capacity()); } } StrVec::StrVec(const StrVec &s){ auto newdata = alloc_n_copy(s.elements, s.first_free); elements = newdata.first; first_free = cap = newdata.second; } StrVec::~StrVec(){ free(); } StrVec& StrVec::operator=(const StrVec &s){ auto newdata = alloc_n_copy(s.elements, s.first_free); free(); elements = newdata.first; first_free = cap = newdata.second; return *this; } StrVec::StrVec(const initializer_list<string> &lis1){ elements = alloc.allocate(lis1.end() - lis1.begin()); cap = first_free = uninitialized_copy(lis1.begin(), lis1.end(), elements); } void StrVec::reallocate(){ auto newcapacity = size() ? 2 * size() : 1; auto const newdata = alloc.allocate(newcapacity); auto p = newdata; auto p_ele = elements; for (size_t i = 0; i != size(); ++i) alloc.construct(p++, move(*p_ele++)); free(); elements = newdata; first_free = p; cap = elements + newcapacity; } void StrVec::reserve(size_t n){ if (n>capacity()){ auto newdata = alloc.allocate(n); auto p = newdata; auto p_ele = elements; for (size_t i = 0; i != size(); ++i) alloc.construct(p++, move(*p_ele++)); free(); elements = newdata; first_free = p; cap = elements + n; } } void StrVec::resize(size_t n, const string &str = string()){ if (n <= capacity()){ if (n < size()){ for (auto i = elements + n; i != first_free; ++i) alloc.destroy(i); first_free = elements + n; } else{ for (auto i = first_free; i != first_free + n - size(); ++i) alloc.construct(i, str); first_free = elements + n; } } } void StrVec::printf(){ for (auto i = elements; i != first_free; ++i) cout << *i << " "; }
--------------------------------------------------main.cpp------------------------------------------------------- #include "StrVec.h" int main(){ initializer_list<string> lis1{ "how", "are", "you"}; StrVec s1, s2(lis1),s3(s2),s4=s3; s1 = s2; s1.printf(); cout << endl; s2.printf(); cout << endl; s3.printf(); cout << endl; s4.printf(); cout << endl; cout << "capacity: " << s2.capacity() << endl; cout << "size: " << s2.size() << endl; cout << endl; s2.push_back("push_insert"); s2.printf(); cout << endl; cout << "capacity: " << s2.capacity() << endl; cout << "size: " << s2.size() << endl; cout << endl; s2.reserve(10); s2.resize(3, "not_insert"); s2.printf(); cout << endl; cout << "capacity: " << s2.capacity() << endl; cout << "size: " << s2.size() << endl; cout << endl; s2.resize(6, "must_insert"); s2.printf(); cout << endl; cout << "capacity: " << s2.capacity() << endl; cout << "size: " << s2.size() << endl; return 0; }
相关文章推荐
- 据说是雅虎的一份PHP面试题附答案
- JS之小练习代码
- 5个实用的shell脚本面试题和答案
- PHP中提问频率最高的11个面试题和答案
- 《算法竞赛入门经典》解题索引
- 一些简单的Python测试题(个人参考答案)
- 【python】编程语言入门经典100例--34
- 【Android】两个layout之间的滑动切换
- 服务器练习
- 2008年计算机四级网络参考答案
- 鸟哥私房菜 习题答案(服务篇)4
- 用户登录脚本故障排查
- 名企面试题+答案
- 阿里笔试题+答案
- 谷歌2013校招全套笔试题
- 腾讯2014笔试题+答案
- 过时的广东省教育技术中级培训题答大全
- 2009年下半年网络规划设计师上午试卷讨论版答案
- 静态网页联系
- 1.1 常量与变量-基础练习