In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use dynamic arrays in C++. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
C++ 's new operator is a very good grammatical feature of the language. However, in practice, it is found that the new operator has many limitations. A prominent point is that when allocating multi-dimensional array space with the new operator, each dimension of the array can not be dynamically changed. This paper will propose a simple and intuitive solution, which will be explained in a simplified model of a practical problem, so as to explain many beginners' misunderstandings about the new operator and multi-dimensional array in C++.
1. The raising of the question-- the practical use of multidimensional variable arrays
The following is a simplified model of problems encountered in actual programming. ChessBoard is a chessboard class, in which m_board is a two-dimensional array used to store information about pieces on the chessboard. DIMENSION is the size or dimension of the chessboard, and because it is used for array declaration, it must be a constant whose value can be determined during compilation. Here we use anonymous enumerations. For different types of chess, the board size is different. For black and white, DIMENSION is defined as 8, for Gobang, DIMENSION should be 15, and go, it has to be 19. Conditional compilation is used to determine the value of DIMENSION constant to ensure the reusability of this code.
Because m_board must be a compile-time constant, the size of the m_board array is immutable at run time of the program. You can't do this if you want to implement black and white, Gobang and go at the same time in the program-it's a bit of an exaggeration, of course, but even go has 9x9, 13x13 and 19x19 boards, and it should give users the freedom to choose when the program is running.
Class ChessBoard {private: enum {# ifdef OTHELLO DIMENSION=8 file:// if it is black and white, the board size is 8x8 # endif # ifdef PENTE DIMENSION=15 file://. If Gobang is Gobang, the board size is 15x15 # endif}; int mboard [DIMENSION] [DIMENSION] Public: / * other member functions. * /}
To do this, we must use the new operator or the malloc function to dynamically allocate space for m_board when the program is running. As new supports more C++ features, our program uses the new operator.
2. Explanation of applying multidimensional array with new in MSDN-- further understanding of new operator
The following code is extracted from "new operator" in MSDN, where the second line will get an error message when compiled in VC6.0. The description in MSDN is that the type returned by the new operator is float (*) [25] [10], that is, a pointer to float [25] [10] (remove the leftmost dimension). The correct code should look like lines 3 or 4.
1. Float * fp;2. Fp = new float [10] [25] [10]; / / error message: cannot convert from 'float (*) [25] [10]' to 'float *' 3. Float (* cp) [25] [10]; 4. Cp = new float [10] [25] [10]
With reference to this code, let's consider our chessboard problem. According to the gourd drawing, we can get the following code:
Int (* m_board) [DIMENSION]; / / declare m_board = new int [Changeable] [DIMENSION] in the member variables of the class; / / determine the corresponding Changeable value according to the user's choice
It is not difficult to see that since arrays must still be declared with the compile-time constant DIMENSION, m_board arrays can only be one-dimensional variable, which is of no use to our problem.
3. Solution
Two solutions are given here, and the specific code for the second one is given.
1)。 We can apply for an one-dimensional array with the size of XSIZE*YSIZE, and then locate the corresponding storage unit by converting the xy subscript. The code is as follows:
Int * p=new int [YSIZE*XSIZE]; file://XSIZE and YSIZE should be defined as constant file://, but the reference to p [y] [x] is a syntax error, which should be p [y * XSIZE + x] = yroom1000 + x
The biggest advantage of this method is that the array dimension can be determined freely, even dynamically, because it is converted to an one-dimensional array. But its biggest inconvenience is the complexity of subscript conversion, which is more obvious in the case of multi-dimensional arrays. For example, the following code is a program to verify that the subscript conversion is correct, and the output should be that the address of each array unit is different and falls between the "start address" and the "end address".
Const int YSIZE=6; const int XSIZE=7; const int ZSIZE=9; int * p=new int [YSIZE*XSIZE*ZSIZE]; file:// but the reference to p [y] [x] is a syntax error and should be cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.