C++结构体学习

        今天主要学习的有:①结构体初始化②结构体嵌套③结构体和函数

        结构体是为了方便使用,归类的一种手段。比如我定义结构体

struct Students
{
    int Number;
    string name;
    double score;
}

        这就是一个定义了学生学号、姓名和成绩的结构体(当然也可以加入班级等等信息)

        调用的时候,只需要调用Students.Number、Students.name、Students.score,这样的就可以了,比起一个个定义数组显然清晰了许多。

初始化结构体

定义结构体变量可以通过①初始化列表②构造函数,来初始化

初始化列表

初始化列表是最简单的办法,其实就是声明结构体后,类似于变量赋值的给结构体赋值

比如我定义了结构体:

struct transcript
{
    int Num, Class, Score;
}

意思是成绩单,包含两个变量:学号和成绩

那么我初始化它的方式是:

transcript me = {2016302000, 912, 95};

那么me.Num = 2016302000,me.Class = 912,me.score=95

初始化时,可以按顺序初始化部分变量,但不可以跳过。

比如我定义

transcript william = {2016302001, 913};

此时就是对的

但是我不知道他的班级,只知道成绩和名字,我这么写:

transcript william = {2016302001, 95};

等于把他的班级就变成95了,是错误的。

构造函数

构造函数好用,但是:

①如果有一个没有被初始化,那么之后的都不会被初始化

②要是里边有字符串,很多编译器上都无法运行

还是刚才的结构体,我换一种定义方式:

struct transcript
{
    int Num, Class, Score; //定义学号、班级、成绩
    transcript(int n = 0, int c = 0, double s = 0)
    {
        Num = n;
        Class = c;
        Score = s;
    }
}

这样,后边我只需要写:

transcript william;

那么william.Num = 0, william.Class = 0, william.Score = 0,我们只需要再写入变量就可以了。

实际应用

还是分数系统,不过这一次我们做的是平时分,有一个学校要求学生平时参加会议,参加的越多综测分数越高,那么计算综测分数的系统就是:

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

using std::cout;
using std::endl;

struct Score
{
	int StuNum;
	string name;
	double times;
	double perscore;
};

int main()
{
	Score usual;//初始化结构体变量,平时分数
	double TotalScore;//总的分数
	cout << "Enter students number:";
	cin >> usual.StuNum;
	cout << "Enter students name:";
	cin.ignore();//跳过输入中的回车字符
	getline(cin, usual.name);
	cout << "How many times you enter the meeting?";
	cin >> usual.times;
	cout << "Score per times:";
	cin >> usual.perscore;

	//计算分数
	TotalScore = usual.times * usual.perscore;
	//显示结果
	cout << "\nHere is the results:\n";
	cout << "Name:" << usual.name << endl;
	cout << "Number:" << usual.StuNum << endl;
	cout << "Meeting times:" << usual.times << endl;
	cout << "Score per time:" << usual.perscore << endl;
	cout << "Total score:" << TotalScore << endl;
	return 0;
}

结构体嵌套

结构体是可以互相嵌套的,如这个例子

struct Score
{
    double Mid;
    double Final;
};

struct Item
{
    string Name;
    string Subject;
    Score AllScore;
};

就是这么个关系

 调用名字和科目时候这么写

Item me;
me.Name = 'william';
me.subject = 'C++';

 但是注意调用Mid和Final时候的情况

me.AllScore.mid = 86;
me.AllScore.Final = 95;

 中间的是第二个结构体中的命名,绝不是结构体名。

结构体和函数

将结构体传给函数

结构体给函数传递可以通过①值②引用③常量引用

值传递是默认的,但是这会生成整个原始结构,浪费时间

引用传递好一些,函数可以直接访问原始变量并且修改它

要是不想函数修改原始变量,那么使用常量引用可以把结构体当做常量进行传递

看例子,还是上边的那个例子,在这个例子里使用了:引用和常量引用

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct Score
{
	int StuNum;
	string name;
	double times;
	double perscore;
};

// 变量引用
void getData(Score &);

//常量引用
void ShowData(const Score &);

int main()
{
	Score usual;//初始化结构体变量,平时分数
	getData(usual);
	ShowData(usual);
	return 0;
}

void getData(Score& item)
{
	cout << "Enter students number:";
	cin >> item.StuNum;
	cout << "Enter students name:";
	cin.ignore();//跳过输入中的回车字符
	getline(cin, item.name);
	cout << "How many times you enter the meeting?";
	cin >> item.times;
	cout << "Score per times:";
	cin >> item.perscore;
}

void ShowData(const Score& item)
{
	double TotalScore;//总的分数
	//计算分数
	TotalScore = item.times * item.perscore;
	//显示结果
	cout << fixed << showpoint << setprecision(2) << endl;
	cout << "\nHere is the results:\n";
	cout << "Name:" << item.name << endl;
	cout << "Number:" << item.StuNum << endl;
	cout << "Meeting times:" << item.times << endl;
	cout << "Score per time:" << item.perscore << endl;
	cout << "Total score:" << TotalScore << endl;
}

函数返回一个结构体

将上边代码里的getData改一下,可以得到

Score getData()
{
    Score item;
	cout << "Enter students number:";
	cin >> item.StuNum;
	cout << "Enter students name:";
	cin.ignore();//跳过输入中的回车字符
	getline(cin, item.name);
	cout << "How many times you enter the meeting?";
	cin >> item.times;
	cout << "Score per times:";
	cin >> item.perscore;
    return item;
}

就可以在main里调用了,调用方法:

part = getData();

这时候getData函数得到的数据就存储在part中了。

今天主要学习了结构体,包含①初始化②嵌套③和函数结合,这三方面内容。

学习资料:http://c.biancheng.net/view/1407.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值