How To Create A Dynamic Array

Table of contents:

How To Create A Dynamic Array
How To Create A Dynamic Array

Video: How To Create A Dynamic Array

Video: How To Create A Dynamic Array
Video: C+ POINTERS (2020) - How to create/change arrays at runtime? (Dynamic arrays) PROGRAMMING TUTORIAL 2024, May
Anonim

A named set of elements of the same type is called an array. Such an organization of data has a lot of obvious advantages and one drawback - when creating an array, it is necessary to declare its size in advance, which cannot be changed by conventional means in the future. The solution to this problem is to develop dynamic arrays that can change the number of their elements at any time. Moreover, for this, you can use both already created classes, and implement your own using standard programming language tools.

How to create a dynamic array
How to create a dynamic array

Instructions

Step 1

The main essence of a dynamic array is to allocate memory for the data stored in it exactly in the size in which it is needed at the moment. It is most convenient to implement this construction in the form of a class - a wrapper for an array. Here it is necessary to provide for all functions that perform allocation and release of memory for an array, as well as operators that provide access to its elements.

Step 2

Create an object of the dynamic array wrapper class, and the constructor will automatically allocate memory of the specified size. If, as the array is filled, the memory for the elements will be completely occupied, when adding next data, the following actions are performed: - all information from the array is stored in temporary storage (auxiliary array); - previously allocated memory is freed by a special command (free, delete); - memory is allocated under an array of the size that is required to contain all the data - all "old" values are placed in a new array from temporary storage and a new element is added.

Step 3

The best way to work with dynamic arrays is to use existing library classes. One of the most common examples is the vector class. It includes all the functions and iterators necessary for the functioning of a mutable array. Moreover, the library module containing this class is supplied with any version of the C ++ compiler.

Step 4

Include the dynamic array library using the #include command. Use the vector class to create an object. Moving through the array is the same as in the usual case, using indices. The special features here are the functions for adding and removing new elements, as well as a number of auxiliary methods. An example of code for creating and operating a dynamic array vector: #include vector; vector int Mass; // declaration of a dynamic array with elements of type intMas.push_back (10); // adding the first element - number 10 Mas.push_back (15); // adding the second element - number 15Mas [1] = 30; // the second element is written the number 30Mas.pop_back (); // deleting the last element of the array Here, when creating a dynamic array named Mass, the type of its elements (int) must be specified, the dimension is not specified in this case.

Recommended: