forked from jzplp/Cpp-Primer-Answer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrVec.h
72 lines (58 loc) · 1.97 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef STRVEC_H
#define STRVEC_H
#include<string>
#include<memory>
#include<utility>
#include<initializer_list>
class StrVec
{
friend bool operator==(const StrVec &lhs, const StrVec &rhs);
friend bool operator<(const StrVec &lhs, const StrVec &rhs);
friend bool operator>(const StrVec &lhs, const StrVec &rhs);
public:
StrVec() : elements(nullptr), first_free(nullptr), cap(nullptr) { }
StrVec(const StrVec &);
StrVec & operator=(const StrVec &);
~StrVec();
StrVec(std::initializer_list<std::string> li);
StrVec(StrVec &&s) noexcept;
StrVec & operator=(StrVec &&rhs) noexcept;
StrVec & operator=(std::initializer_list<std::string> li);
std::string & operator[](std::size_t n);
const std::string & operator[](std::size_t n) const;
void push_back(const std::string &);
void push_back(std::string &&);
template <typename ... Args> void emplace_back(Args && ... args);
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);
void resize(size_t n, const std::string &t = 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;
};
bool operator==(const StrVec &lhs, const StrVec &rhs);
bool operator!=(const StrVec &lhs, const StrVec &rhs);
bool operator<(const StrVec &lhs, const StrVec &rhs);
bool operator<=(const StrVec &lhs, const StrVec &rhs);
bool operator>(const StrVec &lhs, const StrVec &rhs);
bool operator>=(const StrVec &lhs, const StrVec &rhs);
template <typename ... Args>
void StrVec::emplace_back(Args && ... args)
{
chk_n_alloc();
alloc.construct(first_free++, std::forward<Args>(args)...);
}
#endif