1、设结构体类型变量为:typedef struct student{ char school_name[100]; char gender; int age; bool is_absent;} StudentInfo;
2、vector存放结构体类型变量的副本:#inc盟敢势袂lude <iostream>#include <stri荏鱿胫协ng>#include <vector>//structtypedef struct student{ char school_name[100]; char gender;//xing bie int age; bool is_absent;} StudentInfo;typedef std::vector<StudentInfo> StudentInfoVec;//sheng mingvoid 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;}
3、vector存放指向结构体类型变量的指针:#i艘绒庳焰nclude <iostream>#include <stri荏鱿胫协ng>#include <vector>//structtypedef 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;}