vector存放结构体数据的2种方法

如果要在Vector容器中存放结构体类型的变量,经常见到两种存放方式.

方式一:放入这个结构体类型变量的副本。

方式二:放入指向这个结构体类型变量的指针。

假设结构体类型变量是这样的,

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

那么,方式一和方式二的实现分别如下所示:

/*[方式一] 结构体放栈中,vector中放副本---------------------*/
#include <iostream>
#include <string>
#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;
}
 

方式一的输出结果

/*[方式二]  结构体放入堆中,vector中放指针---------------------*/
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;

}

方式二的输出结果,同上,依然是

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

总结注意:类型的typedef 定义了类型  还需要定义类型的变量

原文地址:https://www.cnblogs.com/hshy/p/10962524.html

时间: 2024-08-04 15:41:35

vector存放结构体数据的2种方法的相关文章

结构体数组初始化三种方法,转载

C语言结构体初始化的三种方法:原文链接http://www.2cto.com/kf/201503/386575.html 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 #include <stdio.h> struct student_st {     char c;     int score;     const c

C语言结构体定义的几种方法

什么是结构体? 在C语言中,结构体(struct)指的是一种数据结构,是C语言中聚合数据类型(aggregate data type)的一类.结构体可以被声明为变量.指针或数组等,用以实现较复杂的数据结构.结构体同时也是一些元素的集合,这些元素称为结构体的成员(member),且这些成员可以为不同的类型,成员一般用名字访问. 结构体的定义: C语言结构体类型的定义模板大概为: struct 类型名{ 成员表列 } 变量; 在成员表列中可以是几种基本数据类型,也可以是结构体类型. struct 类

C语言结构体初始化的四种方法

定义 struct InitMember{    int first:    double second:    char* third:    float four;}; 方法一:定义时赋值 struct InitMember test = {-10,3.141590,"method one",0.25}: 需要注意对应的顺序,不能错位.方法二:定义后逐个赋值 struct InitMember test: test.first = -10;test.second = 3.14159

C语言结构体初始化的三种方法

直接上示例了 #include <stdio.h> struct student_st { char c; int score; const char *name; }; static void show_student(struct student_st *stu) { printf("c = %c, score = %d, name = %s\n", stu->c, stu->score, stu->name); } int main(void) {

剔除list中相同的结构体数据

剔除list中相同的结构体数据,有三个思路: 1.两层循环,逐个比较 2.使用set容器来剔除 3.使用unique方法去重 // deduplication.cpp : 定义控制台应用程序的入口点. // #include <list> #include <set> #include <iostream> #include <algorithm> #include <vector> #include <tchar.h> using

Socket传输结构体数据注意事项

[1 背景] 在Socket通信中,要传输结构化的数据或者要进行协议数据传输的时候,发送端必须要构造结构体进行数据传输. 接收端也必须通过同样的结构体进行解析. 但Socket传输结构体数据时候,稍有不慎就会出现:1)解析数据出错:2)接收数据不完整:3)解析为乱码等的Bug. [2 举例] 如下是接收端解析数据为乱码甚至崩溃的一类常见错误. 结构体也就是一段连续的内存. 但是类似如下的结构体: typedef struct _PER_SPIDER_INFO { UINT nTimeDelay;

C语言-结构体定义的几种方式

若struct后面接的是名字,则其为该结构体的名称.第一种是最基本的结构体定义,其定义了一个结构体A. struct A //第一种 { int a; }; 第二种则是在定义了一个结构体B的同时定义了一个结构体B的变量m. struct B //第二种 { int b; }m; 第三种结构体定义没有给出该结构体的名称,但是定义了一个该结构体的变量n,也就是说,若是想要在别处定义该结构体的变量是不行的,只有变量n这种在定义结构体的同时定义变量才行. struct //第三种 { int c; }n

Node.JS的表单提交及OnceIO中接受GET/POST数据的三种方法

OnceIO 是 OnceDoc 企业私有内容(文档)管理系统的底层Web框架,它可以实现模板文件.静态文件的全缓存,运行起来完全不需要I/O操作,并且支持客户端缓存优化,GZIP压缩等(只压缩一次),拥有非常好的性能,为您节约服务器成本.它的模块化功能,可以让你的Web进行分布式存储,在一个扩展包里即可包含前端.后端和数据库定义,只需通过添加/删除目录的方式就可实现功能删减,实现真正的模块化扩展.目前 OnceIO 已经开源,本文主要介绍node.js语言中的表单提交及OnceIO中接受GET

PHP获取POST数据的几种方法

一.PHP获取POST数据的几种方法 方法1.最常见的方法是:$_POST['fieldname'];说明:只能接收Content-Type: application/x-www-form-urlencoded提交的数据解释:也就是表单POST过来的数据 方法2.file_get_contents("php://input");说明:允许读取 POST 的原始数据.和 $HTTP_RAW_POST_DATA 比起来,它给内存带来的压力较小,并且不需要任何特殊的 php.ini 设置.p