C + + Dynamic request two-dimensional array and copy constructors

Source: Internet
Author: User

One, C + + dynamic application of two-dimensional array

In C + +, it is not possible to apply the two-dimensional array directly, and after searching, we find a better method to apply the two-dimensional array dynamically.

The code is as follows (Matrix_type for a certain type, lines and columns):

matrix_type** Elem; // C + + two-dimensional matrix dynamic application space New matrix_type*[lines];elem[0New Matrix_type[lines * Columns];  for (int1; i < Lines; i++)     1] + Columns;

You can read and assign values directly:

0;

The advantage of applying this method to a two-dimensional array is that memory is contiguous and used directly.

Second, C + + copy constructors

When you use the following code, C + + copies the original object and assigns the value to the next object. However, there is a problem that C + + cannot copy dynamic member data when the object contains dynamic members.

classmatrix{ Public:    intLines; intColumns; Matrix_type**Elem;//InitializeMatrix (); Matrix (intLinesintcolumns); ~Matrix ();}; Matrix::matrix (intLinesintcolumns) {Lines=lines; Columns=columns; //C + + two-dimensional matrix dynamic application spaceElem =Newmatrix_type*[Lines]; elem[0] =NewMatrix_type[lines *Columns];  for(inti =1; i < Lines; i++) Elem[i]= Elem[i-1] +Columns; memset (elem[0],0, Lines * Columns *sizeof(Matrix_type));} Matrix::~Matrix () {//C + + two-dimensional matrix destructionDelete elem[0]; Delete Elem;}
Matrix m2 == m2;

One way to solve this problem is to rewrite the copy constructor. The form is as follows:

//copy ConstructorMatrix::matrix (Constmatrix&m) {    //because new is present in the constructor, the copy constructor must be rewrittenLines=M.lines; Columns=M.columns; //C + + two-dimensional matrix dynamic application spaceElem =Newmatrix_type*[Lines]; elem[0] =NewMatrix_type[lines *Columns];  for(inti =1; i < Lines; i++) Elem[i]= Elem[i-1] +Columns; memcpy (elem[0], m.elem[0], Lines * Columns *sizeof(Matrix_type));}

C + + Dynamic request two-dimensional array and copy constructors

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.