VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 791|回复: 0

[分享] c++结构体(入门篇)

[复制链接]
13_avatar_middle
最佳答案
1 
在线会员 发表于 2022-1-6 20:29:58 | 显示全部楼层 |阅读模式
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;

struct Student
{
    //成员列表
    //姓名
    string name;
    //年龄
    int age;
    //分数
    int score;
};

int main()
{
   
    struct Student s1;
    s1.name = "张三";
    s1.age = 18;
    s1.score = 100;
    cout << "姓名:" << s1.name << " 年龄:" << s1.age << " 分数:" << s1.score << endl;
    system("pause");
    return 0;
}

2:结构体数组------类似数组
struct Student
{
    //成员列表
    //姓名
    string name;
    //年龄
    int age;
    //分数
    int score;
};

int main()
{
    //创建结构体数组
    struct Student stuArray[3] =
    {
        {"张三",18,100},
        {"李四",28,99},
        {"王五",38,66}
    };
    //3.给结构体数组中的元素赋值
    stuArray[2].name = "赵六";
    stuArray[2].age = 80;
    stuArray[2].score = 60;
    for (int i = 0; i < 3; i++)
    {
        cout << "姓名:"<<stuArray[i].name<<" 年龄:"<<stuArray[i].age<<" 分数:"<<stuArray[i].score<<endl;
      
    }
3:结构体指针
//定义学生结构体
struct Student
{
    string name;//姓名
    int age;//年龄
    int score;//分数
};

int main()
{
    //创建学生结构体变量
    Student s = { "张三",18,100 };
    //通过指针指向结构体变量
    Student* p = &s;
    //通过指针访问结构体变量中的数据
    //通过结构体指针 访问结构体中的属性,需要利用->
    cout << "姓名:"<<p->name<<" 年龄:"<<p->age<<" 分数:"<<p->score<< endl;
    system("pause");
    return 0;
}




上一篇:C++中iomanip用法
下一篇:SQL 中的modify使用问题
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-5-29 23:20

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表