C/C++:vector中存放结构体类型变量

2025-10-02 16:21:03

1、设结构体类型变量为:

typedef struct student

   char school_name[100]; 

   char gender; 

   int age; 

   bool is_absent; 

} StudentInfo;

C/C++:vector中存放结构体类型变量

2、vector存放结构体类型变量的副本:

#include <iostream> 

#include <string> 

#include <vector> 

//struct

typedef struct student

       char school_name[100]; 

       char gender;//xing bie 

       int age; 

       bool is_absent; 

} StudentInfo; 

typedef std::vector<StudentInfo> StudentInfoVec;//sheng ming 

void print(StudentInfoVec *stduentinfovec)

       for (int j=0;j<(*stduentinfovec).size();j++)//bian li vector 

       { 

              std::cout<<(*stduentinfovec)[j].school_name<<"\t"

                          <<(*stduentinfovec)[j].gender<<"\t"

                             <<(*stduentinfovec)[j].age<<"\t"

                             <<(*stduentinfovec)[j].is_absent<<"\t"

                             <<std::endl; 

       } 

int main()

       StudentInfo micheal={"Micheal",'m',18,false}; 

      

       StudentInfo cherry={"Cherry",'f',16,true}; 

      

       StudentInfoVec studentinfovec;//dui xiang 

      

       studentinfovec.push_back(micheal); 

       studentinfovec.push_back(cherry); 

       print(&studentinfovec); 

      

       system("pause");

       return 0; 

}  

C/C++:vector中存放结构体类型变量

3、vector存放指向结构体类型变量的指针:

#include <iostream> 

#include <string> 

#include <vector> 

//struct

typedef struct student

       char* school_name; 

       char gender; 

       int age; 

       bool is_absent; 

} StudentInfo; 

typedef std::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; 

       } 

int main()

       StudentInfoPtrVec studentinfoptrvec; //dui xiang

       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; 

       system("pause");

       return 0; 

}  

C/C++:vector中存放结构体类型变量

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢