如果要在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. jsp四种属性范围

    在JSP提供了四种属性的保存范围.所谓的属性保存范围,指的就是一个设置的对象,可以在多个页面中保存并可以继续使用.它们分别是:page.request.session.appliction. 1.pa ...

  2. Linux驱动开发9——kfifo缓冲队列

    Kfifo是Linux内核缓冲队列 #include <linux/kfifo.h> 分配kfifo队列,返回值为0表示成功,其他值表示失败 int kfifo_alloc(struct ...

  3. Python 字符串与列表去重

    最近面试中出现频率比较高的字符串和列表的去重pstr = 'abcadcf'# 字符串去重# 1.使用集合 --没有保持原来的顺序 print(set(pstr)) # 2.使用字典 -- 没有保持原 ...

  4. 506C Mr. Kitayuta vs. Bamboos

    分析 代码 #include<bits/stdc++.h> using namespace std; #define int long long ],h[],now[],cnt[]; in ...

  5. 子系统安装nginx

    Win10中启用WSL WSL是微软和Canonical合作为开发人员提供的一个运行在win10环境下的一个Linux子系统,由微软编写核心代码,并由Canonical提供软件包的支持.要想使用WSL ...

  6. c# 跨应用程序域通讯

    public class MyTask { public class MyEventArgs : EventArgs { public object EventData { get; private ...

  7. 应用安全 - 工具 - 知道创宇 - CEYE检测平台

    DNS Query

  8. vue分别打包测试环境和正式环境

    vue打包时使用不同的环境变量 需求 同一个项目通过打包使用不同的环境变量,目前的环境有三个: 一.本地------开发环境 二.线上------测试环境 三.线上------正式环境 我们都知道vu ...

  9. redis配置主从出现DENIED Redis is running in protected mode

    修改redis配置文件,将绑定的ip给注释掉 #127.0.0.1 在配置文件中将protected-mode 改为no protected-mode no 另一种方式是在配置文件中设置密码 requ ...

  10. 第五周课程总结&实验报告(三)

    实验三 String类的应用 实验目的 掌握类String类的使用: 学会使用JDK帮助文档: 实验内容 1.已知字符串:"this is a test of java".按要求执 ...