How to create the massif

How to create the massif

Massifs are one of the simplest and, perhaps, the most used form of the structured data storage at information processing in computer programs. Their main advantage - a possibility of very fast access to an element on its serial number (index). It is possible to create the massif in language C ++ in several ways.

It is required to you

  • - text editor;
  • - compiler C ++.

Instruction

1. Create the massif of the fixed size. Its declaration has to contain type of values, the identifier of a variable and the specification of dimension with the indication of quantities of elements. For example, the one-dimensional massif of the whole values consisting of ten elements it is possible to define so: int aNumbers [10]; In this way it is possible to create also multidimensional massifs: int aNumbers[3][4]; For initialization of similar variables it is possible to use literals of massifs: int aNumbers_1[10] = {1, 2, 3}; int aNumbers_2[3][3] = {    {1, 2, 3},    {4, 5, 6},    {7, 8, 9} }; Pay attention that the quantity of elements of literal massifs initialisers can be less than at the variables initialized by it. In this case parts of elements of the massif receiver just will not be appropriated values.

2. Create the massif which volume is defined by the initializing literal. Declare the massif, without specifying its size. The massifs defined in this way have to be initialized: int aNumbers [] = {1, 2, 3}; In the same way it is possible to create multidimensional massifs. However "variables" are allowed to do only one, first measurement: int aNumbers [] [3] = {    {1, 2, 3},    {4, 5, 6},    {7, 8, 9},    {0, 1, 2} }; It is very convenient to determine by This way static constant massifs. Their volume can be calculated at a compilation stage with use of a keyword of sizeof.

3. Create the massif in dynamically distributed memory. Define the index on value of type of an element of the massif. Mark out memory under necessary amount of data. Appropriate to the index value of the address of the first byte of the allocated block. Use functions of distribution of memory of library C (calloc, malloc), new C operator ++ or platforms - specific functions (such as VirtualAlloc, VirtualAllocEx in Windows). For example: int * paNumbers_0 = (int *) malloc (sizeof(int) * 10); int * paNumbers_1 = new int(10); paNumbers_0[1] = 0xFF;//access to elementupanumbers_1 [2] = 0xFF;//access to an elementuposla of the end of use of the massifs created thus, it is necessary to release the marked-out memory: free(paNumbers_0); delete [] paNumbers_1;

4. Create an object of the class realizing functionality of the massif. Similar classes or templates of classes contain in many popular frameworks and libraries. So, in standard library of templates C ++ (STL) the container STD is available:: vector. It is possible to create and use the massif on its basis as follows: std:: vector <int> oVector;//declaration of an object of the oVector.resize (10) array;//change of the size massivaovector [0] = 1;//access to элементуoVector.push_back(0xFF);//addition of an element in a konetsobratita attention that because of automatic control of memory and existence of convenient methods of modification (change of the size, addition of elements, etc.), use of similar classes often happens more expedient, than application of massifs in style C.

Author: «MirrorInfo» Dream Team


Print