Developing my own container — vector
Read in other languages:English, Русский
This is a highly simplified analogue of the standard container vector, with a similar structure and functionality. For a better understanding of the device and the features of the standard vector.
To build this project on linux you need:
- If you don't have Cmake installed, install Cmake
- If the "Debug" or "Release" folders are not created:
mkdir Debug
mkdir Release
- Run the command for Debug and/or Release conf:
cmake -E chdir Debug/ cmake -G "Unix Makefiles" ../ -DCMAKE_BUILD_TYPE:STRING=Debug
cmake -E chdir Release/ cmake -G "Unix Makefiles" ../ -DCMAKE_BUILD_TYPE:STRING=Release
- Go to "Debug" or "Release" folder and build:
cmake --build .
- To Run program - in the debug or release folder run:
./simple_vector
- C++17(STL)
- GCC (MinG w64) 11.2.0
- Copy elision и NRVO ( Named Return Value Optimization )
- move - семантика
- rvalue-ссылки
- Three levels of exception security
- Installation and configuration of all required components in the development environment to run the application
- The use case and tests are shown in main.cpp .
SimpleVector
is based on an array in dynamic memory. The container itself stores only the address of the beginning of the array, as well as information about its current size and capacity:
Size - the number of elements contained in the array;
Capacity - is the maximum number of elements that can fit into the array.
SimpleVector
- supports move semantics.
You can draw an analogy with a bucket of water — capacity_
defines the maximum volume of water that is placed in the bucket, and size_
— the current amount of water in the bucket.
The vector constructed by default has zero size and capacity, and the pointer to the array is nullptr.
Like std::vector
, the SimpleVector
class can change its size in the direction of increasing and decreasing. To do this, use the Resize
method
The SimpleVector
is cleaned by the Clear
method. It does not change its capacity and is semantically equivalent to calling Resize
with an argument equal to zero. The Clear
method does not throw exceptions.
simplified analog of the vector container, with the following functionality:
- Copy constructor. A copy of the vector must have a capacity sufficient to store a copy of the elements of the original vector.
- By default. Creates an empty vector with zero capacity. Does not allocate dynamic memory and does not throw exceptions.
- Parameterized constructor that creates a vector of a given size. The elements of the vector are initialized with the default value for Type. The vector must have the same size and capacity. If the size is zero, dynamic memory for its elements should not be allocated.
- Constructor from
std::initializer_list
. The vector elements must contain a copy of theinitializer_list
elements. Has a size and capacity that matches the size and capacity of the passedinitializer_list
. - The
GetSize
method to get the number of elements in the vector. Does not throw exceptions. - The
GetCapacity
method to get the capacity of the vector. Does not throw exceptions. - The
IsEmpty
method, which tells if the vector is empty. Does not throw exceptions. - Operator
[]
to access an element of a vector by its index. It has two versions — constant and non-constant. Does not throw exceptions. For the operator to work correctly, the index of the array element must not go beyond the array. - The
At
method for accessing an element of a vector by its index, analogous to the at method of thevector
class. If the index goes outside the array, it should throw an exceptionstd::out_of_range
. - The
Clear
method for clearing an array without changing its capacity. Does not throw exceptions. - The
Resize
method for changing the number of elements in the array. The method should provide a strict guarantee of exception safety. - Methods
begin, end, cbegin and cend
that return iterators to the beginning and end of the array. Pointers are used as iterators. These methods are declared with the noexcept specifier. - When a vector is destroyed, the memory occupied by its elements is freed.
- Assignment operator. Must provide a strict guarantee of the safety of exceptions.
- The
PushBack
method, which adds an element to the end of the vector. Must provide a strict guarantee of the safety of exceptions. - The
PopBack
method that removes the last element of the vector. Does not throw exceptions. - The
Insert
method, which inserts an element into an arbitrary place in the container. Provides a basic guarantee of exception safety. - The
Erase
method, which removes an element at an arbitrary position of the vector. Provides a basic guarantee of exception safety. - The
swap
method that exchanges the contents of a vector with another vector. Throws no exceptions, has a runtime of O(1). - Operators
== and !=
. Two vectors are equal if their dimensions are equal and contain equal elements. Equality of capacity is not required. - Operators
<, >, <=, >=
, performing lexicographic comparison of the contents of two vectors. Reserve
sets the capacity of the vector. This method increases the efficiency of the code in the case when the user knows in advance at least the approximate number of elements in the vector.Reserve
immediately allocates the required amount of memory. When adding new elements to the vector, copying will occur either much less frequently or not at all.
Ifnew_capacity
is greater than the currentcapacity
, the memory must be reallocated and the vector elements copied to a new memory segment.
In addition, it is possible to call theSimpleVector
constructor, which immediately reserves the required amount of memory.