数据结构C++

定义结构

struct Peoples
{
   string name;
   string sex;
   int age;
} people;

访问成员

#include <iostream>
using namespace std;
 
//定义一个books的数据结构
struct Peoples{
    string name;
    string sex;
    int age;
};
int main( )
{
    Peoples people1;

    people1.name="张三";
    people1.sex="男";
    people1.age=23;
    cout<<"姓名为:"<<people1.subject<<endl;
    cout<<"性别为:"<<people1.title<<endl;
    cout<<"年龄为:"<<people.book_id<<endl;
   return 0;
}

数据结构作为函数参数

#include <cstdio>
#include <iostream>
using namespace std;
 
//声明一个函数
void printPeople(struct Peoples people);
//定义一个books的数据结构
struct Peoples{
    string name;
    string sex;
    int age;
};
int main( )
{
    Peoples people1;

    people1.name="张三";
    people1.sex="男";
    people1.age=23;
    printPeople(people1);
   return 0;
}
void printPeople(struct Peoples people){
    cout<<"姓名为:"<<people.name<<endl;
    cout<<"性别为:"<<people.sex<<endl;
    cout<<"年龄为:"<<people.age<<endl;
}

指向结构的指针

形式:struct Peoples *struct_pointer;
使用&运算符查找结构变量的地址。
使用->运算符访问结构的成员。
实例:

#include <iostream>
#include <cstring>
 
using namespace std;
void printPeople( struct Peoples *people);
 
struct Peoples
{
   string name;
   string sex;
   int age;
};
 
int main( )
{
    Peoples people;
    people.name="张三";
    people.sex="男";
    people.age=23;
    printPeople( &people );
    return 0;
}
// 该函数以结构指针作为参数
void printPeople( struct Peoples *people )
{
   cout << "姓名  : " << people->name<<endl;
   cout << "性别 : " << people->sex<<endl;
   cout << "年龄 : " << people->age<<endl;
}

typedef关键字

 typedef struct Peoples
{
    string name;
    string sex;
    int age;
}Peoples;

typedef关键字可以用来定义非结构类型:typedef long int *pint32; pint32 x,y,z;x,y,z都是指向长整型long int 的指针。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值