Replace the array of C-Style with vector
Labor groups, everyone must be familiar, you can operate array with indexes and pointers, bringing great flexibility to program design. But do you know that there are many natural defects?
First, the crossing of the array may cause the crash of the program (if it crashes, you will not wait for you to transport ^ _ ^). Secondly, the dynamics are not good, including dynamic changes, dynamic applications. Things such as this will definitely let you hurt the brains. Is there any way to solve these problems? You don't have to worry, let me introduce you to you: replace the array of C-Style with a vector.
I don't want to say about Vector, I assume that everyone knows Temlplate and STL. Everyone can find these contents in any C book (if not, then throw it away). So why is a vector? We know that Vector provides the Operator [] function, you can operate like an array, and there is a boundary check, dynamically changed. (From this point DEQUE is also possible). The Vector is originally available to replace the one-dimensional array. Here only uses it to replace the two-dimensional array. The two-dimensional or more can be pushed accordingly.
We know that C Template parameters can be nested, you can define a template in instance.
Vector> Array2 (3); // Note> and> Space between.
This is our key, Array2 can save 3 vector, and the length of the vector can be changed. Array2 [i] returns the i-th year. Similarly, array2 [i] [j] returns the J20 in the i-th vector.
Here, you may be proud to say: "I understand, very simple!". Don't worry, there are some details: as follows Vector> Array2 (3); array2 [1] [2] = 9; I assure that your program will send, if you don't specify the size of the vector. Use the push_back function to solve the problem: array2 [1] .push_back (9); but seems not cool. Can you use Operator []? The answer is yes. But add a few steps, as follows:
For (int i = 0; i <3; i )
Array2 [I] .resize (3);
In this way, you define a 3x3 array (another 3 is defined at the time of declaration). And you can change its size at any time.
Others, you can also use C anomalous mechanism to capture illegal behaviors such as the following margin. Perform the necessary processing. Make your program more robust. I will not introduce the specific method. Leave you yourself to study it. A sample is provided below for reference.
// instead of array with Vector
// Pan Li Liang 2002-1-13
// In GNU C Mandrake Linux7.0,
// There will be variable definition issues under VC, and everyone solves themselves.
#include
#include
Using namespace std;
void main ()
{
Vector
For (int i = 0; i <3; i )
Array [i] .resize (3); // Set the size of the array 3x3
// Now you can use this vector as an array.
For (int i = 0; i <3; i )
For (int J = 0; j <3; j )
Array [i] [j] = (i * j);
// output
For (int i = 0; i <3; i )
{
For (int J = 0; j <3; j ) cout <<< "
Cout <
}
Array.Resize (5);
ARRY [3] .resize (3);
ARRY [4] .resize (3);
// is now 5x3 array
For (int i = 0; i <5; i )
For (int J = 0; j <3; j )
Array [i] [j] = (i * j);
For (int i = 0; i <5; i )
{
For (int J = 0; j <3; j )
COUT <<< "
Cout <
}