Method of assigning a one-dimensional dynamic array or two-dimensional dynamic array and learning new method or vector

Source: Internet
Author: User

Let's start with an appetizer.

1 //dynamically allocating storage space using new2 3#include <iostream>4 usingstd::cout;5 6 intMain ()7 {8 //1th Way9 int*a=New int;Ten*a=1; Onecout<<"the result of dynamically allocating storage space using the first method is: \ n" A<<"*a="<<*a<<Std::endl; - DeleteA//releasing dynamic storage space - //2nd Way the int*b=New int(2); -cout<<"the result of dynamically allocating storage space using the first method is: \ n" -<<"*b="<<*b<<Std::endl; - Deleteb//releasing dynamic storage space + //3rd Way - int*C; +C=New int(3); Acout<<"the result of dynamically allocating storage space using the first method is: \ n" at<<"*c="<<*c<<Std::endl; - DeleteC//releasing dynamic storage space -  - //Creating arrays Dynamically - float*d=New float[3];//difference 1 new Type () and 2 new type [quantity]
One is to create an object from a constructor function
One is to create a dynamic array but not initialize if the built-in type has a default constructor
Most need to construct the initialization separately again -d[0]=3; ind[1]=6; -d[2]=8; tocout<<"d[0]="<<d[0]<<Std::endl; +D=d+1;//the fundamental difference between an array name and a pointer -cout<<"d[0]="<<d[0]<<Std::endl; thed=d-1; *cout<<"d[0]="<<d[0]<<Std::endl; $ Delete[] D;//releasing dynamic storage spacePanax Notoginseng return 0; -}

The result of dynamically allocating storage space using the first method is:
*a= 1
The result of dynamically allocating storage space using the first method is:
*b= 2
The result of dynamically allocating storage space using the first method is:
*c= 3
D[0]= 3
D[0]= 6
D[0]= 3

Above for the results of the implementation of their own want Ah!!!

new A pointer array
For a pointer to Class A, you can write this, not just new, but also the constructor of a.

A *P = new A();

But for a new pointer array, we can write this:

A **P = new A *[10];//这里是创建了10个A类型的指针 返回的是指针的地址

But we haven't made any construction yet? Here can not forget the construction AH!!!!

for(int i=0; i<10; i++){ p[i] = new A();}

Another problem is releasing:

for(int i=0; i<10; i++){ delete p[i];//为什么不是delete[]p[i]}delete [] p;

—————————————————————————————————————————

Method of assigning a one-dimensional dynamic array or two-dimensional dynamic array and learning new method or vector

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.