(Original region) how to create a two-dimensional partition column (multiple partition columns )? (C/C ++)

Source: Internet
Author: User

In the (original region), how does one create a two-member queue column (multiple region queue columns )? In (high availability) (C), we can see that it is not easy to use the C statement to create a two-dimensional dynamic column, but is there any better solution in C ++?

In (original region), how does one create a dynamic region column? In the (initial) (C/C ++), we can see that C ++ creates a sequence of dynamic columns

Int   * IA =   New   Int [Sizex];

Based on previous experiences, the second phase of the second phase is as follows:

Int   ** IA =   New   Int [Sizey] [sizex];

Or

Int   * IA [sizex] =   New   Int [Sizey] [sizex];

However, neither of these two compression methods can pass compiler. The initial response of the first compression method is as follows:

Cannot convert from ' INT (*) [3] ' To ' Int ** '

In the second method

Cannot convert from ' INT (*) [3] ' To ' Int * [3] '

Therefore, the method of using new cannot be used to create the dynamic Dynamic Binary regression column.

Modern C ++ has always wanted to replace array with vector. Can a vector be used to automatically create a two-dimensional regression column?

1 /**/ /*  
2 (C) oomusou 2007 Http://oomusou.cnblogs.com
3
4 Filename: arraydynamictwodim_vector.cpp
5 Compiler: Visual C ++ 8.0/BCB 6.0/GCC 3.4.2/iso c ++
6 Description: Demo how to dynamic allocate 2 dim array on heap by Vector
7 Release: 02/25/2007 1.0
8 02/28/2007 2.0
9 */
10 # Include < Iostream >
11 # Include < Vector >
12 # Include " Conio. h "
13
14 Using   Namespace STD;
15
16 Void Printtwodimdynamicarray (vector < Vector < Int >   > Ivec) {
17 For ( Int Y =   0 ; Y ! = Ivec. Size (); ++ Y) {
18 For ( Int X =   0 ; X ! = Ivec [ 0 ]. Size (); ++ X) {
19Cout<Ivec [y] [x]< " ";
20}
21 Cout < Endl;
22 }
23 }
24
25 Int Main () {
26 Const   Int Sizex =   3 ;
27 Const   Int Sizey =   2 ;
28 Vector < Vector < Int >   > Ivec (sizey, Vector < Int > (Sizex ));
29
30 For ( Int Y =   0 ; Y ! = Sizey; ++ Y) {
31 For ( Int X =   0 ; X ! = Sizex; ++ X) {
32Ivec [y] [x]=Y+X;
33}
34 }
35
36 Printtwodimdynamicarray (ivec );
37
38 Getch ();
39 }

Results

0   1   2
1   2   3

27 rows

Vector < Vector < Int >   > Ivec (sizey, Vector < Int > (Sizex ));

We have already declared a binary regression column, and the initial value has been set to 0. Why can this problem be solved?

First, we use the vector of vector Modulo for the binary operator column. Therefore, the vector type is not vector <int>, while the vector has two constructor definitions as follows:

Vector < ELEM > C (n, ELEM)
Vector < ELEM > C (n)

The purpose of the first constructor is to create a vector of N and each element is ELEM.
The second constructor is intended to create a vector of N and the initial value of each element is 0.

The first constructor in the root segment. We define a vector with the size of sizey, and each element is a vector <int> (sizex), and the second constructor in the root segment, the second vector is sizex in size and the initial value is 0. In this case, the combination is better than the vector of vector, and the two rows column is modeled.

31 rows

Ivec [y] [x] = Y + X;

The method of using subscripting in the two-dimensional Sequence column is successfully used !!

16 rows

Void Printtwodimdynamicarray (vector < Vector < Int >   > Ivec )

When loading a vector into the function, pointer is no longer used. The vector <int> type is used, and the sizey and sizex parameters are not used.

Using another vertex of the vector, you don't have to worry about how to place the primary column, and there will be no more memory leak.

Conclusion
We have successfully modeled the binary regression column through the vector, but we do not understand it. If your environment allows c ++, he strongly suggested switching to the C ++ coding method.

See also
(Original region) how to create a two-dimensional partition column (multiple partition columns )? (High latency) (c)
(Original) How can I create a dynamic sequence column? (Initial Release) (C/C ++)
(Original partition) is modeled by the (multiple partition columns) (medium partition) (C/C ++)
(Original region) how to create a two-dimensional partition column (multiple partition columns )? (Initial Release) (C #)

Reference
C ++ program named character 6.2.2 vector operation function p.150
Dynamic score column in "memory configuration"

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.