Some basic usage of Allocator and simple vector implementation

Source: Internet
Author: User

First, about Allocator, next to the stamp → Wikipedia-distributor (C ++)

This time, I only used some of the APIs to implement a simple vector container. This simple version of vector implements simple functions such as insertion, deletion, and search. Due to insufficient understanding of the right value, therefore, this time the right value API is not implemented.

[Msdn-class allocator_base API]

This example uses:

  • Allocate-used for allocating and allocating space
  • Construct-used to construct an object
  • Destroy-used to destroy an object (call its constructor)

Next, let's use examples to illustrate how to use allocator to compile a Vector class.

Complete source code can be downloaded at the bottom.

First, give some typedef

typedef T           value_type;typedef T*          pointer;typedef const T*    const_pointer;typedef T*          iterator;typedef const T*    const_iterator;typedef T&          reference;typedef const T&    const_reference;typedef size_t      size_type;typedef ptrdiff_t   different_type;

T is typename, and a pointer to T is used to implement the usage of the iterator. After typedef is used, you can easily understand the code and enhance readability.

With regard to Allocator in this vector class, I chose the inclusion rather than private inheritance method. I personally feel that such code is more readable and easier to modify and maintain.

The private part of the class has the following four members:

// Store STD: Allocator <value_type> _ alloc_vec; // pointer to the first vertex _ first; // pointer to the last vertex _ last; // record the current capacity size_type _ capacity;

Allocator is used to allocate memory and store elements. Two T * type pointers point to the beginning and end of the vector, and the size_type _ capacity records the current capacity of the vector, expand when the capacity is insufficient.

Then there is the member function. I wrote the member function based on msdn, so it is all public member functions, but it is enough to complete this simple class.

First, constructor, copy constructor, destructor, And the '=' symbol that is overloaded.

// Default constructor vector (size_type _ size = 256): _ First (_ alloc_vec.allocate (_ SIZE), _ last (_ first ),
_ Capacity (_ SIZE) {}// copy const vector (const vector & right): _ First (_ alloc_vec.allocate (right. _ capacity), _ last (_ first ),
_ Capacity (right. _ capacity)
{For (Auto P: Right) _ alloc_vec.construct (_ last ++, P);} // default destructor ~ Vector () = default; // reload '= 'vector & operator = (const vector <t> & right) {return vector (right );}

The default constructor uses a _ size parameter of 256 by default to initialize the object. The default constructor pre-allocates a memory space that is not too small and will facilitate future expansion.

_first(_alloc_vec.allocate(_size))

The role of this sentence is equivalent

_first = _alloc_vec.allocate(_size);

Assign _ alloc_vec a space of _ size and point _ first to its first place. For specific function declaration, stamp msdn.

Since no element is added to the object, _ last and _ first point to the same address, that is, the first place of _ alloc_vec, and then store the value of _ size into _ capacity.

For the copy constructor, the capacity () function is used to obtain the right capacity. Then, the calling object is allocated a space of the same size and the two pointers are directed to the first place, then, record the container size, go to the function body, use the for loop to copy all the elements in the right to the calling object, and modify the position indicated by _ last.

Note that no matter what type of constructor, the position indicated by _ last is the next position of the last element.

As for the destructor, the default destructor is used directly, and the equal sign directly calls the copy constructor. (Not because of laziness.

Then there are two member functions that are easy to implement: pop_back () and push_back.

// Delete the end element void pop_back () {_ alloc_vec.destroy (-- _ last);} // Insert the element into the last void push_back (reference Val) {If (SIZE ()> = _ capacity) {_ alloc_vec.allocate (_ capacity); _ capacity * = 2;} _ alloc_vec.construct (_ last ++, Val );}

The destroy function is called in pop_back (). This function will call the destructor of the object pointed to by the _ last pointer after the auto-minus 1 and destroy the object, after this action is completed, _ last still points to the next position of the last element.

Push_back () before adding an element to a vector, it checks whether the capacity is sufficient to allocate space. If not, it calls allocate to allocate more space. When an element is added, construct is called, this function will call a constructor of the type T, and after adding it, it will move the _ last one bit.

Then it seems that the three functions have been introduced ...... The insert and erase functions in the remaining vector use methods similar to push_back and pop_back ...... So I will not write it. (Too many rows)

As for other operations, some operations on pointers ...... Well, that's it ...... (Escape

[Complete source code download]

Some basic usage of Allocator and simple vector implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.