目录
定义结构体后用substr函数提取出生日期后依次比较后输出。
一:认识结构体(struct)
在C++除了基本的int、long long 、string 、bool、double、char、short等基本数据类型之外,我们也可以根据自己的需要,用struct自定义结构体数据类型,非常方便。例如可以定义一个结构体student(学生)再有三个参数h(身高),age(年龄),name(名字)就可以用student数据类型保存一个学生的基本数据而不是定义三个变量储存。
二:结构体声明
使用结构体必须要声明一个结构体类型,再定义和使用结构体变量。结构体类型的声明格式如下:
struct 类型名
{
数据类型1 成员名1;
数据类型2 成员名2;
......
};
例如,声明一个学生类型的结构体:
struct student
{
int age;
string name;
int height;
};
上面定义了一种新的结构体数据类型:student类型。
student类型含有三个成员:age、name、height。
三:结构体变量的总结
(一)结构体变量的定义
格式是: 结构体类型名 变量名列表;
例如:在声明了student类型之后,就可以用下面的格式来定义变量:
student a,b,c; //变量a,b,c是student类型的变量。
(二)结构体变量的成员的引用
结构体变量的成员的引用,格式是:结构体变量名.成员名。
例如: student a; //a是student类型变量。
如果需要引用a的name成员,那么格式是:a.name, 因为a.name是string类型,因此它的使用方法普通的string类型变量相同。同理,如果需要引用a的age成员,那么格式是a.age,a.age的使用方法和int类型变量的使用方法相同。
(三)结构体变量的读入输出
可以通过cin和cout对结构体变量的每个成员进行读入和输出操作。
studnet a; //a是student类型变量。
cin>>a.name>>a.age>>a.height; //读入a的姓名、年龄、体重
cout<<a.age<<" "<<a.height<<" "<<a.name<<endl; //输出a的姓名、年龄、体重
(四)结构体变量的初始化
回顾一下在定义一个整型变量时可以进行初始化工作,例如:
int age=15; //定义了age变量,它是int类型,而且age的初始值为15。
那么结构体变量在定义时是否可以进行初始化呢?可以!
struct student //声明一种student类型
{
int age;
string name;
int height;
};
student a={15,"zhangsan",160}; //a是student类型变量,年龄是15,姓名是"zhangsan",身高是160。
(五)结构体变量的赋值
定义了结构体变量之后,可以给结构体变量的各个成员赋值。
student a;
a.age = 18;
a.name = "zhangsan";
a.height=165;
(六)交换结构体变量
我们可以使用swap函数来交换两个整数变量、两个字符串变量等等。我们也可以使用swap函数来交换两个结构体变量。例如:
student a,b;
cin>>a.age>>a.name>>a.height;
cin>>b.age>>b.name>>b.heigth;
swap(a,b); //交换a和b变量。
四:练习
题目一 最高的学生
读入三个学生的姓名和身高,输出身高最高的学生的姓名。
输入格式
3行,每行描述一个学生的信息:姓名和身高。
数据保证身高不同。
输出格式
一个字符串
输入/输出例子1
输入:
zhangsan 170
lisi 175
wangwu 169
输出:
lisi
分析:
定义一个结构体变量,比较后输出。
代码:
#include<bits/stdc++.h>
using namespace std;
struct student
{
int height; //身高
string name; //姓名
};
student read()
{
student x;
cin>>x.name>>x.height;
return x;
}
int main(){
student a, b, c;
a = read(); //调用自定义函数read,读入一个学生信息。
b = read();
c = read();
student ans = a; //先默认a是最高的。
if(b.height > ans.height) ans = b; //如果b比ans高,则替换ans
if(c.height > ans.height) ans = c; //如果c比ans高,则替换ans
cout<< ans.name <<endl;
return 0;
}
题目二 三人排序
读入三个学生的姓名和身高,按照身高从大到小的次序输出三个学生的姓名。
输入格式
三行,每行描述一个学生的姓名和身高,数据保证身高不同。
输出格式
三行,每行一个姓名。
输入/输出例子1
输入:
zhangsan 170
lisi 175
wangwu 169
输出:
lisi
zhangshan
wangwu
分析:
定义一个结构体变量,比较后依次输出。
代码:
#include<bits/stdc++.h>
using namespace std;
struct student
{
int height; //身高
string name; //姓名
};
student read() //自定义函数,功能是读入并返回一个student变量
{
student x;
cin>>x.name>>x.height;
return x;
}
int main(){
student a, b, c ;
a = read(); //调用自定义函数read,读入一个学生信息。
b = read();
c = read();
//下面通过必要的交换操作,使得最终身高从大到小的次序是a,b,c。
if(a.height<b.height) swap(a,b); //如果a比b矮,则交换a和b,以保证a高于b
if(c.height>b.height) swap(b,c); //保证b高于c
if(b.height>a.height) swap(a,b) ;
cout<<a.name<<endl;
cout<<b.name<<endl;
cout<<c.name<<endl;
return 0;
}
题目三 三人排序II
读入三个人的姓名、身份证号码、身高,按照身高从大到小的次序输出三个人的姓名,如果身高相同的,出生早的人排在前面。出生年月可以从身份证号码提取(不会的可自行百度),保证出生年月不同。
输入格式
共3行,每行是一个人信息:姓名、身份证号码、身高。
输出格式
共3行,每行是一个人信息:姓名。
输入/输出例子1
输入:
zhangsan 440682200810013286 150
lisi 440682200808317696 175
wangwu 500682200809301293 150
输出:
lisi
wangwu
zhangsan
分析:
定义结构体后用substr函数提取出生日期后依次比较后输出。
代码:
#include<bits/stdc++.h>
using namespace std;
struct student
{
string name; //姓名
string ID; //身份证
int height; //身高
int year, month, day; //出生的年份、月份、日
};
student a, b, c;
student read() //自定义函数,功能是读入并返回一个student变量
{
student x;
cin>>x.name>>x.ID>>x.height;
return x;
}
int change(string t) //字符串转整数
{
stringstream ss; //定义字符串流的对象ss。
ss<<t; //把字符串t输入到流。
int d;
ss>>d ; //流会以整数形式输出给整数变量d。
return d;
}
void set_year_month_day() //根据身份证信息,获取并设置出生年份、月份、日
{
a.year = change(a.ID.substr(6,4)); //根据身份证获取年份信息,并转为整数。
a.month = change( a.ID.substr(10,2) ) ; //根据身份证获取月份信息,并转为整数。
a.day = change( a.ID.substr(12,2) ); //根据身份证获取日信息,并转为整数。
b.year = change(b.ID.substr(6,4)); //根据身份证获取年份信息,并转为整数。
b.month = change( b.ID.substr(10,2) ) ; //根据身份证获取月份信息,并转为整数。
b.day = change( b.ID.substr(12,2) ); //根据身份证获取日信息,并转为整数。
c.year = change(c.ID.substr(6,4)); //根据身份证获取年份信息,并转为整数。
c.month = change( c.ID.substr(10,2) ) ; //根据身份证获取月份信息,并转为整数。
c.day = change( c.ID.substr(12,2) ); //根据身份证获取日信息,并转为整数。
}
bool young(student x, student y)
{//如果x比y年轻返回true,否则返回false
if(x.year>y.year) return true;
if(x.year==y.year && x.month>y.month) return true;
if( x.year==y.year && x.month==y.month && x.day>y.day ) return true;
return false;
}
bool needSwap(student x, student y) //判断x和y是否需要交换
{
if(x.height < y.height) return true; //如果x身高比y矮,需要交换
if( x.height==y.height && young(x,y)==true ) return true; //身高相同但x比y年轻,也需要交换
return false; //其他情况不需要交换
}
int main(){
a = read(); //调用自定义函数read,读入一个学生信息。
b = read(); //读入b的信息
c = read(); //读入c的信息
set_year_month_day(); //计算并设置a,b,c的出生年份、月份、日
//下面通过必要的交换操作,使得最终身高从大到小的次序是a,b,c。
if( needSwap(a,b)==true) swap(a,b); //如果a和b不满足最终的次序,则交换a和b,保证a排在b前面
if( needSwap(b,c)==true) swap(b,c); //保证b要排在c前面
if( needSwap(a,b)==true) swap(a,b) ;
cout<<a.name<<endl;
cout<<b.name<<endl;
cout<<c.name<<endl;
return 0;
}
五:结构体数组
1、结构体数组的定义。
struct student
{
int age;
string name;
int height;
};
上面声明了student这个结构体类型。如果我们要保存100个学生的信息,怎么办?
可以定义一个student类型的数组,格式如下:
student a[100]; //a是stduent类型的数组,这个数组可以保存100个student类型的变量,分别是a[0]、a[1]、a[2]、...... a[99]。
2、结构体数组的读入输出
可以用循环语句读入结构体数组的信息。
下面是读入100个学生的。
student a[100]; //定义一个数组,这个数组的类型是student类型,保存100个学生信息。
for(int i=0; i<100; i++) //读入100个学生的信息。
{
cin>>a[i].age>>a[i].name>>a[i].height; //读入第i个学生的信息。
}
for(int i=99;i>=0; i--) //反向输出每个学生的信息
{
cout<<a[i].name<<" "<<a[i].age<<" "<<a[i].height<<endl; //输出第i个学生信息
}
六:练习二
下面再来分享一下五道题目。
题目一 排序总分
输入N个人的学生姓名和总分成绩,然后按总分由高到低进行排序,输出他们的姓名和总分。
输入格式
第一行为整数n,(1<=n<=100)
接着有n行,每行是学生的姓名和总分。
输出格式
共n行,每行是一个学生的姓名和总分
输入/输出例子1
输入:
3
huang 263
wang 213
luo 301
输出:
huang 263
wang 213
luo 301
分析:可以用循环读入后再冒泡排序,输出。
代码实现:
#include<bits/stdc++.h>
using namespace std;
struct student
{
string name; //姓名
int score;//总分
};
student a[105]; //结构体数组,最多可以保存105个学生的信息
int n;
int main()
{
cin>>n;
for(int i=1; i<=n; i++){
cin>> a[i].name ; //读入第i个学生的姓名
cin>> a[i].score ; //读入第i个学生的总分
}
//下面对a数组进行冒泡排序,按照总分从高到低排序
for(int i=1; i<n; i++) //共进行n-1轮冒泡排序
{
for(int j=1; j<=n-i; j++) //第i轮冒泡,要比较n-i次
if(a[j].score < a[j+1].score) //如果第j个学生的总分比第j+1个学生的总分低,则交换
swap(a[j],a[j+1]) ;
}
for(int i=1; i<=n; i++)
cout<<a[i].name<<" "<<a[i].score<<endl;
return 0;
}
这就是关于结构体介绍,希望大家可以点个赞鼓励一下。