forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrVec.h
50 lines (39 loc) · 1.38 KB
/
StrVec.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef STRVEC_H
#define STRVEC_H
#include <string>
#include <memory> // allocator, uninitialized_copy
#include <initializer_list>
class StrVec {
public:
typedef size_t size_type;
StrVec() : first_elem(nullptr), first_free(nullptr), cap(nullptr) { }
StrVec(std::initializer_list<std::string>);
StrVec(const StrVec &);
~StrVec();
StrVec &operator=(const StrVec &);
void push_back(const std::string &s);
void pop_back();
void reserve(size_type);
void resize(size_type, const std::string & = std::string());
// default value for parameter put in declaration
bool empty() const { return cbegin() == cend(); }
size_type size() const { return first_free - first_elem; }
size_type capacity() const { return cap - first_elem; }
std::string *begin() { return first_elem; }
std::string *end() { return first_free; }
const std::string *begin() const { return first_elem; }
const std::string *end() const { return first_free; }
const std::string *cbegin() const { return begin(); }
const std::string *cend() const { return end(); }
private:
static std::allocator<std::string> alloc;
std::string *first_elem;
std::string *first_free;
std::string *cap;
void chk_n_alloc() { if (size() == capacity()) reallocate(); }
std::pair<std::string *, std::string *>
alloc_n_copy(const std::string *, const std::string *);
void reallocate();
void free();
};
#endif