Two methods vector storage structure of the data

If the type of structure to be stored in a Vector variable vessel, often seen two kinds of storage methods.

Method 1: Add a copy of this structure type variables.

Second way: put pointer to the structure type of the variable.

Suppose the structure is such that the variable type,

typedef struct student{
   char school_name[100];
   char gender;
   int age;
   bool is_absent;
} StudentInfo;

Then, a mode two mode of implementation and are as follows:

/ * [A way] to put the stack structure, vector copies in the discharge --------------------- * / 
#include <the iostream> 
#include < String > 
# the include <Vector> 
typedef struct Student {
    char school_name [ 100 ];
    char Gender;
    int Age;
    BOOL is_absent; 
} StudentInfo; 
 
typedefstd :: Vector <StudentInfo> StudentInfoVec; 
 
void Print (StudentInfoVec * stduentinfovec) {
    for ( int J = 0 ; J <(* stduentinfovec) .size (); J ++  )
    {
       std::cout<<
           (*stduentinfovec)[j].school_name<<"\t"<<
           (*stduentinfovec)[j].gender<<"\t"<<
           (*stduentinfovec)[j].age<<"\t"<<
           (*stduentinfovec)[j].is_absent<<"\t"<<std::endl;
    }
   return;
}
 
int main(){
   StudentInfo micheal={"Micheal",'m',18,false};
   StudentInfo cherry={"Cherry",'f',16,true};
   StudentInfoVec studentinfovec;
   studentinfovec.push_back(micheal);
   studentinfovec.push_back(cherry);
   print(&studentinfovec);
   return 0;
}
 

An output result of embodiment

/ * [Second way] into the stack structure, vector pointer in the discharge --------------------- * / 
typedef struct Student {
    char * school_name;
    char Gender ;
    int Age;
    BOOL is_absent; 
} StudentInfo; 
 
typedefstd :: Vector <StudentInfo *> StudentInfoPtrVec; 
 
void Print (StudentInfoPtrVec * stduentinfoptrvec) {
    for ( int J = 0 ; J <(* stduentinfoptrvec) .size (); J ++ ) 
    { 
       STD cout :: << 
           ( * stduentinfoptrvec) [J] -> school_name << " \ t"<<
           (*stduentinfoptrvec)[j]->gender<<"\t"<<
           (*stduentinfoptrvec)[j]->age<<"\t"<<
           (*stduentinfoptrvec)[j]->is_absent<<"\t"<<std::endl;
    }
   return;
}
 
int main(){
 
   StudentInfoPtrVec studentinfoptrvec;
 
   char* p_char_1=NULL;
   p_char_1=new char[100];
   strcpy(p_char_1,"Micheal");
   StudentInfo* p_student_1=new StudentInfo;
   p_student_1->school_name=p_char_1;
   p_student_1->gender='m';
   p_student_1->age=18;
   p_student_1->is_absent=false;
   studentinfoptrvec.push_back(p_student_1);
 
   char* p_char_2=NULL;
   p_char_2=new char[100];
   strcpy(p_char_2,"Cherry");
   StudentInfo* p_student_2=new StudentInfo;
    p_student_2->school_name=p_char_2;
   p_student_2->gender='f';
   p_student_2->age=16;
   p_student_2->is_absent=false;
   studentinfoptrvec.push_back(p_student_2);
      
   print(&studentinfoptrvec);
   delete p_char_1;
   delete p_student_1;
   delete p_char_2;
   delete p_student_2;
   return 0;
 
}

 

The output mode II, above, is still

【转】https://blog.csdn.net/feliciafay/article/details/9128385

 

Summary Note: The type defines the type of typedef also need to define a variable of type

Guess you like

Origin www.cnblogs.com/hshy/p/10962524.html