C ++ uses variant for two-dimensional array operations

Source: Internet
Author: User

Variant is one of the important parameter variables for communication between COM components. It can accommodate a variety of different types, such as short, long, and double, including various pointers and arrays. Mutual calling between components is time-consuming, especially when components are in different processes. Therefore, reducing the number of passes is an effective way to improve efficiency. An Excel table operation may involve a large amount of data. Transferring a two-dimensional array at a time improves the efficiency of operations on the Excel table. The following two methods are used to operate the variant two-dimensional array.

1. Use safearray to implement a two-dimensional array

The safearray security array can implement multi-dimensional arrays. The safearray implementation steps can be roughly divided into three steps.

(1) create a safearray security array, including setting the type, dimension, and size of the array elements.

(2) assign a value to the safearray. You can use the safearrayputelement function to take responsibility for each element, or use a pointer to obtain the data address of the safearray, and then assign a value to the value pointed to by the pointer. If an array in safearray is a multi-dimensional array, you can convert the multi-dimensional array to a one-dimensional array, or you can obtain a pointer to the array to operate on the elements in the array.

(3) Use the variant variable to wrap the safearray.

The source code for implementing a two-dimensional array using safearrar is as follows:

Vartype VT = vt_i4;/* array element type, long */
Safearraybound SAB [2];/* defines the dimension of the array and the starting value of the lower mark */
Sab [0]. celements = 2;
Sab [0]. llbound = 0;
Sab [1]. celements = 2;
Sab [1]. llbound = 0;
/* Create A 2*2 two-dimensional array of the long type */
Safearray * PSA = safearraycreate (Vt, sizeof (SAB)/sizeof (safearraybound), SAB );
If (null = psa)
{
Throw;
}

/* Indirectly assign values to elements of a two-dimensional array by pointing to an array pointer */
Long (* parray) [2] = NULL;
Hresult HRET = safearrayaccessdata (PSA, (void **) & parray );
If (failed (HRET ))
{
Throw;
}
Memset (parray, 0, 2*2 * sizeof (long ));
/* Release the pointer to the array */
Safearrayunaccessdata (PSA );
Parray = NULL;

/* Assign values to elements of a two-dimensional array one by one */
Long index [2] = {0, 0 };
Long lfirstlbound = 0;
Long lfirstubound = 0;
Long lsecondlbound = 0;
Long lsecondubound = 0;
Safearraygetlbound (PSA, 1, & lfirstlbound );
Safearraygetubound (PSA, 1, & lfirstubound );
Safearraygetlbound (PSA, 2, & lsecondlbound );
Safearraygetubound (PSA, 2, & lsecondubound );
For (long I = lfirstlbound; I <= lfirstubound; I ++)
{
Index [0] = I;
For (long J = lsecondlbound; j <= lsecondubound; j ++)
{
Index [1] = J;
Long lelement = I * SAB [1]. celements + J;
Hresult HRET = safearrayputelement (PSA, index, & lelement );
If (failed (HRET ))
{
Throw;
}
}
}

 

/* Convert safearray to variant */
Variant var;
Var. Vt = vt_array | VT;/* The VT must be consistent with the PSA data type */
Var. parray = psa;
Safearraydestroy (PSA );
PSA = NULL;

 

2. Use colesafearray to implement a two-dimensional array

Colesafearray inherits from variant and is an automation class of MFC. Therefore, this class can be used only when the MFC class library is used. Colesafearray encapsulate operation-related functions. You can use msdn to query the member functions of this class to learn about functions related to security arrays. Colesafearray can also be directly converted to variant. Therefore, compared with safearray, colesafearray is easier to use. The relationship between colesafearray and safearray is the relationship between the MFC class library and the Win32 SDK. The procedure is similar.

The source code for implementing a two-dimensional array using colesafearray is as follows:

Vartype VT = vt_i4;/* array element type, long */
Safearraybound SAB [2];/* defines the dimension of the array and the starting value of the lower mark */
Sab [0]. celements = 2;
Sab [0]. llbound = 0;
Sab [1]. celements = 2;
Sab [1]. llbound = 0;

Colesafearray olesa;
Olesa. Create (Vt, sizeof (SAB)/sizeof (safearraybound), SAB );

/* Indirectly assign values to elements of a two-dimensional array by pointing to an array pointer */
Long (* parray) [2] = NULL;
Olesa. accessdata (void **) & parray );
Memset (parray, 0, 2*2 * sizeof (long ));
/* Release the pointer to the array */
Olesa. unaccessdata ();
Parray = NULL;


/* Assign values to elements of a two-dimensional array one by one */
Long index [2] = {0, 0 };
Long lfirstlbound = 0;
Long lfirstubound = 0;
Long lsecondlbound = 0;
Long lsecondubound = 0;
Olesa. getlbound (1, & lfirstlbound );
Olesa. getubound (1, & lfirstubound );
Olesa. getlbound (2, & lsecondlbound );
Olesa. getubound (2, & lsecondubound );
For (long I = lfirstlbound; I <= lfirstubound; I ++)
{
Index [0] = I;
For (long J = lsecondlbound; j <= lsecondubound; j ++)
{
Index [1] = J;
Long lelement = I * SAB [1]. celements + J;
Olesa. putelement (index, & lelement );
}
}



/* Convert the colesafearray variable to variant */
Variant Var = (variant) olesa;

 

References

Http://blog.sina.com.cn/s/blog_74f586a50100rv6t.html
Http://hfp0601.blog.163.com/blog/static/228483522011031104718762/

Related Article

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.