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

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

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

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

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

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

/*[方式一] 结构体放栈中,vector中放副本---------------------*/
#include <iostream>
#include <string>
#include <vector>
typedef struct student{
char school_name[];
char gender;
int age;
bool is_absent;
} StudentInfo; typedefstd::vector<StudentInfo> StudentInfoVec; void print(StudentInfoVec* stduentinfovec){
for (int j=;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',,false};
StudentInfo cherry={"Cherry",'f',,true};
StudentInfoVec studentinfovec;
studentinfovec.push_back(micheal);
studentinfovec.push_back(cherry);
print(&studentinfovec);
return ;
}

方式一的输出结果

/*[方式二]  结构体放入堆中,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=;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[];
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=;
p_student_1->is_absent=false;
studentinfoptrvec.push_back(p_student_1); char* p_char_2=NULL;
p_char_2=new char[];
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=;
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 ; }

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

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

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

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

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

    什么是结构体? 在C语言中,结构体(struct)指的是一种数据结构,是C语言中聚合数据类型(aggregate data type)的一类.结构体可以被声明为变量.指针或数组等,用以实现较复杂的数据 ...

  2. C语言结构体初始化的四种方法(转载)

    原文:https://blog.csdn.net/ericbar/article/details/79567108 定义 struct InitMember { int first: double s ...

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

    定义 struct InitMember{    int first:    double second:    char* third:    float four;}; 方法一:定义时赋值 str ...

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

    直接上示例了 ? 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 3 ...

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

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

  6. 计算机二级-C语言-对结构体数据进行求平均值。对结构体数据进行比较处理。

    //函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),作为函数返回,并将大于平均值的数放在形参y所指数组中,在主函数中输出. //重难点:对结构体数据进行求平均值. #in ...

  7. PHP获取POST数据的几种方法汇总

    一.PHP获取POST数据的几种方法 方法1.最常见的方法是:$_POST['fieldname']; 说明:只能接收Content-Type: application/x-www-form-urle ...

  8. SQLServer 批量插入数据的两种方法

    SQLServer 批量插入数据的两种方法-发布:dxy 字体:[增加 减小] 类型:转载 在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Ins ...

  9. 关于iOS去除数组中重复数据的几种方法

    关于iOS去除数组中重复数据的几种方法   在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方 ...

随机推荐

  1. C语言的AES加密

    C语言的AES加密 稍微封装了几个函数 方便使用 #if 1 #include <stdio.h> #include <stdlib.h> #include <strin ...

  2. rtmp协议分析

    最近需要做一个rtmp服务器,着手分析一下rtmp协议,开干. rtmp握手 这个推荐一篇文章讲解得比较透彻http://blog.sina.com.cn/s/blog_676e11660102v8b ...

  3. Django学习之Form表单

    一.Form介绍 普通方式手写注册功能 使用form组件实现注册功能 二.Form那些事儿 1.常用字段与插件 initial error_messages password radioSelect ...

  4. C# 无焦点获取扫码枪扫码信息

    代码网上有的是,多是需要窗体焦点直接show出扫码信息(usb,模拟键盘,hook) 怎样才能真的无焦点获取? 用串口方式 usb转串口 以接收串口通讯消息的方式获取扫码信息

  5. 【MM系列】SAP 财务帐与后勤不一致情况

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 财务帐与后勤不一致情况   ...

  6. Redis为什么不能使用一主一从哨兵

    哨兵机制 识别挂掉的主节点 quorum(法定人数) 是判定主节点不能访问所需要的最少哨兵数量 执行失效备援perform a failover 其中一个哨兵需要被选为救援的领导,并被授权执行救援,而 ...

  7. 判断RecyclerView是否滚动到底部

    转:http://www.jianshu.com/p/c138055af5d2 一.首先,我们来介绍和分析一下第一种方法,也是网上最多人用的方法: public static boolean isVi ...

  8. Scrapy输出文件格式问题汇总

    Q:Scrapy抓取的内容(包含中文)输出到JSON Lines文件时如何确保输出的是字符本身而不是其unicode编码? A:默认的JsonLinesItemExporter其ensure_asci ...

  9. [Git] 018 冲突在所难免,需要巧妙化解

    0. 回顾 [Git] 005 初识 Git 与 GitHub 之分支 中"4.2 情形二"的 9 提及了"解决冲突" 当时没有展开,这回详谈 我这回反其道而行 ...

  10. JS跨域--window.name

    JS跨域--window.name:https://www.jianshu.com/p/43ff69d076e3