-
Notifications
You must be signed in to change notification settings - Fork 14
/
String.h
41 lines (30 loc) · 814 Bytes
/
String.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
#ifndef DEMO_STRING_H
#define DEMO_STRING_H
#include<memory>
#include<utility>
class String
{
public:
String() : elements(nullptr), first_free(nullptr), cap(nullptr) { }
String(const char *);
~String() { free(); }
String(const String &);
String & operator=(const String & rhs);
void push_back(char c);
size_t size() const { return first_free - elements; }
size_t capacity() const { return cap - elements; }
char * begin() const { return elements; }
char * end() const { return first_free; }
void reserve(size_t n);
void resize(size_t n, char c = '\0');
private:
void chk_n_alloc();
std::pair<char *, char *> alloc_n_copy(const char *, const char *);
void free();
void reallocate();
static std::allocator<char> alloc;
char * elements;
char * first_free;
char * cap;
};
#endif