Replace the array of C-style with Vector

Source: Internet
Author: User
When it comes to arrays, you must be familiar with them. You can use indexes and pointers to operate arrays, which brings great flexibility to program design. But do you know that it has many inherent defects?
First, the array out-of-bounds may cause the program to crash (if it crashes, you are lucky enough ). Secondly, the dynamic nature is not good, including dynamic change and dynamic application. This kind of thing will definitely make you have a headache. Is there any way to solve these problems?
You don't have to worry about it. Next I will introduce you to a method: Replace the array of C-style with vector.
I don't want to talk much about vector. I suppose everyone knows temlplate and STL. You can find the content in any c ++ book (if not, Please discard it as soon as possible ). So why is it vector? We know that vector provides the operator [] function, which can operate like an array, and also has the boundary check to dynamically change the size. (Deque is also possible from this point ). Vector can be used to replace a one-dimensional array. Here we only use it to replace a two-dimensional array. More than two-dimensional data can be pushed accordingly.
We know that the template parameter of C ++ can be defined by nesting. You can define the instance of a template in this way.
Space between vector <int> array2 (3); // note> and>.
This is our key. array2 can store three vectors, and the length of the vector can be changed. Array2 [I] returns the I-th vector. Similarly, array2 [I] [J] returns the J element in the I vector.
Here, you may proudly say, "I understand, is it very simple! ". Don't worry, there are some details:
Vector <vector <int> array2 (3 );
Array2 [1] [2] = 9;

I promise that your program will segement failed because you have not specified the vector size. The push_back function can solve the problem: array2 [1]. push_back (9. Cannot I use operator? The answer is yes. But there are several more steps:
For (INT I = 0; I <3; I ++)
Array2 [I]. Resize (3 );

In this way, you define a 3x3 array (the other 3 is defined at the time of declaration ). And you can change its size at any time.
In addition, you can use the exception mechanism of c ++ to capture illegal behaviors such as the following mark crossing the border. Perform necessary processing. Make your program more robust. I will not go into details about the specific methods. Leave it to your own research. The following is an example for your reference.

# Include <iostream>
# Include <vector>
 
Using namespace STD;

Void main ()
{
Vector <vector <int> array (3 );

For (INT I = 0; I <3; I ++)
Array [I]. Resize (3); // you can specify the size of an array as 3x3.
// Now you can use this vector like 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 <array [I] [J] <"";
Cout <Endl;
}

Array. Resize (5 );
Arry [3]. Resize (3 );
Arry [4]. Resize (3 );

// Now the array is 5x3
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 <array [I] [J] <"";
Cout <Endl;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.