vector存放结构体数据的2种方法
如果要在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种方法的更多相关文章
- C语言结构体定义的几种方法
什么是结构体? 在C语言中,结构体(struct)指的是一种数据结构,是C语言中聚合数据类型(aggregate data type)的一类.结构体可以被声明为变量.指针或数组等,用以实现较复杂的数据 ...
- C语言结构体初始化的四种方法(转载)
原文:https://blog.csdn.net/ericbar/article/details/79567108 定义 struct InitMember { int first: double s ...
- C语言结构体初始化的四种方法
定义 struct InitMember{ int first: double second: char* third: float four;}; 方法一:定义时赋值 str ...
- 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 ...
- 剔除list中相同的结构体数据
剔除list中相同的结构体数据,有三个思路:1.两层循环,逐个比较 2.使用set容器来剔除 3.使用unique方法去重 // deduplication.cpp : 定义控制台应用程序的入口点. ...
- 计算机二级-C语言-对结构体数据进行求平均值。对结构体数据进行比较处理。
//函数fun的功能是:计算形参x所指数组中N个数的平均值(规定所有数均为正数),作为函数返回,并将大于平均值的数放在形参y所指数组中,在主函数中输出. //重难点:对结构体数据进行求平均值. #in ...
- PHP获取POST数据的几种方法汇总
一.PHP获取POST数据的几种方法 方法1.最常见的方法是:$_POST['fieldname']; 说明:只能接收Content-Type: application/x-www-form-urle ...
- SQLServer 批量插入数据的两种方法
SQLServer 批量插入数据的两种方法-发布:dxy 字体:[增加 减小] 类型:转载 在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Ins ...
- 关于iOS去除数组中重复数据的几种方法
关于iOS去除数组中重复数据的几种方法 在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方 ...
随机推荐
- 解决CsvWriter:中文乱码、末尾行多一行空格(/r)、非第一列空字符串""显示null问题
一:主要内容 解决CsvWriter存csv,csv文件打开后中文乱码问题 解决CsvWriter存csv,csv文件最后一行总是多一行空行的问题 解决CsvWriter存csv,csv文件不是第一列 ...
- 卡死浏览器使IPhone的自带safari打开重启的JS循环代码
<html> <body> <script> var total=""; for (var i=0; i < 1000000; i++) ...
- 四十、python中的生成器和迭代器
A.生成器(包含yield的就是生成器) def func(): print(11) yield 1 print(22) yield 2 print(33) yield 3 print(44) yie ...
- log() exp()函数
1 对数函数表示法 import numpy as np import math print('输出自然底数e:',math.e) # np表示法 # np.log()是以e为底的自然对数 print ...
- 使用Postman对HTTP接口进行功能测试
一.工具说明 Postman是一种网页调试与发送网页http请求的工具.我们可以用来很方便的模拟get或者post或者其他方式的请求来调试接口. 二.应用场景 1.Get请求 get请求通过接口参数拼 ...
- gitlab在centos7.3上搭建
gitlab在centos7.3上搭建 最近接到gitlab+jenkins的任务,由于以前只接触过GitHub,并只是简单的使用,这里简单记录gitlab与jenkins搭建的 环境: centos ...
- 使用cesium中的scene.open中遇到的几个问题
有些服务是发在场景(scene)下的,超图提供了一个很方便的方法:scene.open,这个方法会将场景中所有的图层(无论是OSGB还是影像和地形)加载进来.同时这个方法会自带一个自动地位功能,具体实 ...
- 应用安全 - 软件漏洞 - sudo漏洞汇总
sudo Potential bypass of Runas user restrictions(CVE-2019-14287 ) Date:2019.10.14 类型: sudo提权漏洞 影响版本: ...
- .net core 学习小结之 配置介绍(config)以及热更新
命令行的配置 var settings = new Dictionary<string, string>{ { "name","cyao"}, {& ...
- 关于MySQL的安装使用心得
MySQL浅浅地学习了几天,当然还是转到正轨Java上来了,昨天打了一串代码,测试注解来着,结果MySQL挂了~~~ 如何干净卸载MySQL帖子有很多,不再赘述,注册表是个好东西~~ 卸载了Mysql ...