n-dimensional array implementation of data structure review (use of variable parameter tables)

Source: Internet
Author: User

First, let's introduce the macro that the mutable parameter table needs to use:

Header files: #include <cstdarg>

void Va_start (Va_list arg_ptr, Prev_param);
Type Va_arg (va_list arg_ptr, type);
void Va_end (Va_list arg_ptr);

Va_list: A type of information required to hold macro Va_start, Va_arg, and Va_end. In order to access parameters in a variable-length parameter list, you must declare
An object definition for the Va_list type: typedef char * VA_LIST;
Va_start: A macro used before accessing a parameter in a variable-length argument list that initializes an object declared with Va_list and initializes the result for the macro va_arg and
Va_end use;
Va_arg: Expands the macro into an expression that has the value and type of the next parameter in the variable-length argument list. Each call to Va_arg will be modified
The object declared with Va_list so that the object points to the next parameter in the argument list;
Va_end: This macro enables the program to return normally from a variable-length argument list with a function referenced by the macro va_start.
VA here is the meaning of the variable-argument (variable parameter).
These macros are defined in Stdarg.h, so a program that uses mutable parameters should contain this header file. Let's write a simple variable parameter function, change the function to have at least one integer parameter, the second argument is an integer, is optional. The function simply prints the values of both parameters.

/*There seems to be no distinction between inner and nested classes in C + + two nouns.    Both inner and nested classes refer to defining classes in a class.    A local class is defined as a class in a function. (c + + cannot define functions in a function.) A function that C + + defines in a class is also a member function. The biggest difference between C + + inner classes and Java inner classes is that C + + 's inner class object has no pointers to external class objects, cannot access non-static members of external class objects, and Java's non-static inner class objects have pointers to external class objects that can access non-static members of external class objects. )*/#include<cstdarg>//Standard header files, providing macros Va_start, Va_arg, and Va_end#include <cstdio>#include<cstdlib>#include<iostream>#include<string>#include<cstring>#defineMax_array_dim 8//assume that the maximum number of dimensions for an array is 8using namespacestd;template<typename elemtype>classmyarray{Private:     Public:        classarray{ Public: Elemtype*Base;//base address for array elements                intDim//number of dimensions of an array                int* bounds;//array dimension boundary address                int* Constants;//array image function constant base address        }; Static Const intERROR =-1; Static Const intOK =1; intInitarray (Array &a,intDim, ...);//yanet The length of the dim and subsequent dimensions is legal, the corresponding array A is constructed        intDestoryarray (Array &a);//Destroy Array a        intValue (Array A, Elemtype &e, ...);//A is n an array, E is an element variable, followed by n subscript value, and the element corresponding to the subscript value is assigned to e        intAssign (Array &a, Elemtype e, ...);//assigns the element e to the specified subscript value        intLocate (Array A, va_list AP,int&off);//returns the offset address of the specified subscript value, stored in the off};template<typename elemtype>intMyarray<elemtype>::initarray (Array &a,intDim, ...) {    if(dim<1|| Dim > Max_array_dim)returnMyarray::error; A.dim=Dim; A.bounds= (int*)malloc(Dim *sizeof(int)); if(! A.bounds)returnMyarray::error; intElemtotal =1;    Va_list ap; Va_start (AP, dim);//gets the array that holds the variable-length parameter information     for(intI=0; i<dim; ++i) {A.bounds[i]= Va_arg (AP,int); if(A.bounds[i] <0)returnMyarray::error; Elemtotal*=A.bounds[i];    } va_end (AP); A.Base= (Elemtype *)malloc(Elemtotal *sizeof(Elemtype)); if(! A.Base)returnMyarray::error; A.constants= (int*)malloc(Dim *sizeof(int)); if(! a.constants)returnMyarray::error; //To begin the image array//int L = sizeof (elemtype);//the size of each element    intL =1;//Note that the unit size of the element here is 1, because A.base + off is actually a.base+off*sizeof (elemtype);a.constants[dim-1] =L;  for(inti=dim-2; i>=0; --i) a.constants[i]= a.bounds[i+1] * a.constants[i+1]; returnMyarray::ok;} Template<typename elemtype>intMyarray<elemtype>::D Estoryarray (Array &A) {    if(! A.Base)returnMyarray::error;  FreeA.Base); if(! A.bounds)returnMyarray::error;  Free(A.bounds); if(! a.constants)returnMyarray::error;  Free(a.constants);} Template<typename elemtype>intMyarray<elemtype>::value (Array A, Elemtype &e, ...)    {va_list ap; intoff;    Va_start (AP, E); if(Locate (A, AP, off) ==myarray::error)returnMyarray::error; E= * (A.Base+off);    Va_end (AP); returnMyarray::ok;} Template<typename elemtype>intMyarray<elemtype>::assign (Array &A, Elemtype e, ...)    {va_list ap; intoff;    Va_start (AP, E); if(Locate (A, AP, off) ==myarray::error)returnMyarray::error; * (A.Base+off) =e;    Va_end (AP); returnMyarray::ok;} Template<typename elemtype>intMyarray<elemtype>::locate (Array A, va_list AP,int&off) {Off=0;  for(intI=0; i<a.dim; ++i) {        intIND = Va_arg (AP,int); if(ind<0|| IND >= A.bounds[i])returnMyarray::error; Off+ = a.constants[i]*IND; }    returnMyarray::ok;}classstudent{ Public:        Char*name; intAge ; Student () {} Student (Char*name,intAge ) {         This->name =name;  This->age =Age ; }        voidoutmsg () {cout<<"Name:"<< name <<", Age:"<<Age ; }};intMain () {MyArray<int>Testarray; MyArray<int>:: Array A; Testarray.initarray (A,4,1,2,3,4);  for(intI=0; i<1; ++i) for(intj=0; j<2; ++j) for(intk=0; k<3; ++k) for(intn=0; n<4; ++N) {                    intE = i+j+k+N;                Testarray.assign (A, E, I, J, K, N); }                     for(intI=0; i<1; ++i) for(intj=0; j<2; ++j) for(intk=0; k<3; ++k) for(intn=0; n<4; ++N) {                    inte;                    Testarray.value (A, E, I, J, K, N); printf ("%d\n", E); } MyArray<Student>Testarrayx; MyArray<Student>:: Array Ax; Testarrayx.initarray (Ax,3,4,Ten, -);//4, 10, 20 are six levels, seat line number, seat row numberStudent S ("Hjzgg", at); Testarrayx.assign (Ax, S,1,2,3); S.name="LXKDD"; Testarrayx.value (Ax, S,1,2,3);    S.outmsg (); return 0;}

n-dimensional array implementation of data structure review (use of variable parameter tables)

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.