如果要在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++ 中的 enum 结构在内存中是怎么存储的?

     C++ 中的 enum 结构在内存中是怎么存储的? C++ C++ 中的 enum 结构在内存中是怎么存储的?里面存储的是常量值吗?   关于占用内存的大小,enum类型本身是不占内存的,编译器直接 ...

  2. springboot+mybatis+SpringSecurity 实现用户角色数据库管理(一)

    本文使用springboot+mybatis+SpringSecurity 实现用户权限数据库管理 实现用户和角色用数据库存储,而资源(url)和权限的对应采用硬编码配置. 也就是角色可以访问的权限通 ...

  3. SecureCRT通过密钥登录

    转载  https://blog.csdn.net/langkeziju/article/details/53024031 说明:一般的密码方式登录容易被密码暴力破解.所以一般我们会将 SSH 的端口 ...

  4. HDU6025 Coprime Sequence(gcd)

    HDU6025 Coprime Sequence 处理出数列的 \(gcd\) 前缀和后缀,删除一个数后的 \(gcd\) 为其前缀和后缀的 \(gcd\) . 遍历数列取 \(max\) 即为答案. ...

  5. gvim安装中文文档

    今天下载了个gvim一直无法安装中文中文文档,以为是版本原因,又下了几个版本,后来发现都不行. 最后发现是文档安装位置不对,需要放到vim/vim47目录下才行 以下是翻墙在官网下的gvim74和vi ...

  6. HTTP学习记录:二、请求方法

    学习资源主要为:@小坦克HTTP相关博客 最基础的是4种,GET.POST.PUT.DELETE对应着对资源的查.改.增.删.最常用的是GET和POST. GET一般用于获取/查询资源信息: POST ...

  7. 【Html5】表单全选、全不选

    以下为页面效果图   用HBuilder做  谷歌浏览器 index.html代码 <!DOCTYPE html> <html> <head> <meta c ...

  8. vue路由高级用法

    五.路由设置高级用法alias 别名 {path:'/list',component:MyList,alias:'/lists'}redirect 重定向 {path:'/productList',r ...

  9. Pager

    jQuery var Pager = function (ops) { this._ops = { count: ops.count || 0, selectedIndex: ops.selected ...

  10. 决解nginx代理的django项目的admin站点无法访问,和没样式的问题。

    首先我们先解决无法访问admin站点的问题 首先我们先修改一下nginx的配置,添加红色框框的部分. 然后重新启动一下nginx 访问一下admin站点 发现没有样式了. 我们先修改/fast_foo ...