Using vector to implement two-dimensional arrays

Source: Internet
Author: User

From:XHeartBlue.home.chinaren.com

Substituting vectors for c-style arrays
To bring up the array, you must be familiar with the index and pointers to manipulate the array, to the design of a great flexibility. But do you know that there are many innate flaws in it?
First, the bounds of the array may cause the program to crash (and, if it crashes, you're lucky ^_^). The second is bad dynamic, including dynamic change of size, dynamic application. That sort of thing is going to hurt your brain. Is there any way to solve these problems?
You don't have to worry, I'm going to introduce you to a method: vector instead of c-style array.
I don't want to say much about vector, I suppose we all know temlplate and STL. You can find this in any C + + book (if not, throw it away quickly). Then why is it a vector? We know that Vector provides a operator[] function that can act like an array, and there are boundary checks to change the size dynamically. (Deque is also possible from this point). Vectors can be used instead of one-dimensional arrays, which are only used to replace two-dimensional arrays. Two-dimensional above can and so on.
We know that C + + template parameters can be nested defined, so you can define a template of the instance
Vector <vector <int> > Array2 (3);//Note the space between > and >.
This is our key, Array2 can save 3 vectors, the length of the vector can be changed. Array2[i] returns the first vector. Similarly, Array2[i][j] returns the J element of the first vector.
Here, you may be proud to say: "I understand, very simple." "。 Don't worry, there are some details:
Vector <vector <int> > Array2 (3);
array2[1][2]=9;
I guarantee that your program will segement failed because you do not specify the size of the vector. The problem can be solved by using the Push_back function: Array2[1].push_back (9); but it doesn't seem so good. Can not use operator[]? The answer is yes. However, there are a few more steps to follow:
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 the Declaration). And you can change the size of it at any time.
Other, you can use C + + exception mechanism to capture the following illegal behavior such as Cross-border. Carry out the necessary processing. Make your program more robust. I will not go into the details of the specific method. I leave it to you to delve into. A sample is provided below for reference.
Use vectors instead of arrays.
Pan Li Liang 2002-1-13
Passed under the GNU C + + Mandrake Linux7.0,
Under VC will have variable definition problem, we solve
#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);//Set the size of the array 3x3
Now you can use this vector just like you do with arrays.
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 it's an array of 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;
}
}

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.