StrVec
Class DefinitionHaving sketched the implementation, we can now define our StrVec
class:
// simplified implementation of the memory allocation strategy for a vector-like class
class StrVec {
public:
StrVec(): // the allocator member is default initialized
elements(nullptr), first_free(nullptr), cap(nullptr) { }
StrVec(const StrVec&); // copy constructor
StrVec &operator=(const StrVec&); // copy assignment
~StrVec(); // destructor
void push_back(const std::string&); // copy the element
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; }
// ...
private:
static std::allocator<std::string> alloc; // allocates the elements
void chk_n_alloc() // used by functions that add elements to a StrVec
{ if (size() == capacity()) reallocate(); }
// utilities used by the copy constructor, assignment operator, and destructor
std::pair<std::string*, std::string*> alloc_n_copy
(const std::string*, const std::string*);
void free(); // destroy the elements and free the space
void reallocate(); // get more space and copy the existing elements
std::string *elements; // pointer to the first element in the array
std::string *first_free; // pointer to the first free element in the array
std::string *cap; // pointer to one past the end of the array
};
// alloc must be defined in the StrVec implmentation file
allocator<string> StrVec::alloc;
The class body defines several of its members:
• The default constructor (implicitly) default initializes alloc
and (explicitly) initializes the pointers to nullptr
, indicating that there are no elements.
• The size
member returns the number of elements actually in use, which is equal to first_free - elements
.
• The capacity
member returns the number of elements that the StrVec
can hold, which is equal to cap - elements
.
• The chk_n_alloc
causes the StrVec
to be reallocated when there is no room to add another element, which happens when cap == first_free
.
• The begin
and end
members return pointers to the first (i.e., elements
) and one past the last constructed element (i.e., first_free)
, respectively.