float x
float y
float x
float y
struct 结构体名字{
float x;
float y;
float z;
}
结构体名字 某东西名字{1,5,6} 分别赋值
就可以调用结构体
cout<<赋值东西名字.x 输出某个值
结构体名字 某些东西的数组[ ]{ {1,2,3 },{5,6,5 },{ 8,9,4},{ 5,6,7} };
结构体类型数组
cout<<某东西数组[1].x<<某东西数组[1].y<<某东西数组[1].z
输出jie'g
关键字struct 用于声明结构体
结构体一个大套餐
可以简化代码量
结构体 一种组合类型
数据获得,声明主函数
struct 定义结构体
#include <iostream>
using namespace std;
struct Position{
float x;
float y;
float z;
};
int main()
{
//float playerPositionX;
//float playerPostionY;
//float playerPostionZ;
Position playerPos = {3, 4, 6.7};
Position playerPos {3, 4, 6.7}; // 省略等号“=”
Position playerPos{ };
cout<playerPos.x<<playerPos.y<<palyerPos.z<<endl; // 346.7
playerPos.y=100;
cout<<playerPos.y<<endl;// 100
Position enemyPos;
// 结构体数组
Position enemysPos[]{{1,1,1},{2,4,7},{},{6,4,5},{123,2,2}};
cout<<enemysPos[1].x<<enemysPos[1].y<<enemysPos[1].z<<endl; // 247
return 0;
}
掌握语法即可
结构体可以放在函数里面(仅该函数内使用),也可以放在函数外面(都可以使用)