C + + dynamic arrays

Source: Internet
Author: User

Arrays are often used in C + + threads, and in the C + + programming version 2nd-rectification, it is also clear that the length of an array must be defined by a constant expression.

However, these two days because in the development of a C + + tool, suddenly found that C + + defines a one-dimensional array, you can also use variables to define the length.

    ints=0;//number of rooms representedcout<<"Please input the number of rooms:"; CIN>>s; introbotnum=0;//represents the number of robotscout<<"Please input the number of robots:"; CIN>>Robotnum; intH=0;//number representing the conflict IDcout<<"Please input the number of conflict markings:"; CIN>>h; intm=robotnum*R; Ctree Tr[robotnum];

Part of the development code, the last line runs normally.

However, the use of more or dynamic arrays, because there are many structures in the project, there are arrays inside the structure, the length of the array is not certain, the array in the case of the dynamic open-up method.

struct ctnode{    int *M;     int // The number    of changes entering the node Ctnode *firstchild;     *nextSibling;     *parent;} Ctnode,*ctree;

The purpose of defining the pointer m is to create the array dynamically.

Ctree c=New  ctnode;c->m=newint[s];

s can be any variable.

For a two-dimensional array, for example, create matrices out and in.

       int** out,**inch;  out=New int*[T]; inch=New int*[T];  for(intf=0; f<t;f++){             out[f]=New int[S]; inch[f]=New int[S]; }

This makes it possible to dynamically open up space without worrying about the length uncertainty.

C + + dynamic arrays

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.