//10 – 1 the structure’s definition and usage
//user’s defining himself’s data structure
#include <iostream>
#include <string>
//use string must have defined the string head file at first
using namespace std;
//1. structure definition
//struct 结构体名 { 结构体成员变量列表 };
struct Book {
string name;
double price;
int value; //值
};
//结构体关键字 struct在定义时不能省略 ,但是在c++中创建结构体时可以省略
struct Book2 {
string name;
double price;
int value; //值
}cpp;//2.3
//2.create a structure
int main() {
//2.1 creation method1
Book c;
//结构体关键字 struct 在定义时不能省略 ,但是在c++中创建结构体时可以省略
//struct Book c;
c.name = “C语言程序设计”;
c.price = 39.99;
c.value = 10;//价值
cout << c.name << ‘ ‘ << c.price << ‘ ‘ << c.value << endl;
cout << “———————-” << endl;
//2.2 creation method2 (initialized a list)
Book py = { “Python编程”,1999,10 };
cout << py.name << ‘ ‘ << py.price << ‘ ‘ << py.value << endl;
cout << “———————-” << endl;
//2.3 creation method3 (add a symbol to the last before ; on the structure(after the definition in the structure)
cpp.name = “C++零基础编程”;
cpp.price = 9999999;
cpp.value = 10000000;
cout << cpp.name << ‘ ‘ << (int)cpp.price << ‘ ‘ << cpp.value << endl;
return 0;
}
//10 – 2 structure array
#include <iostream>
using namespace std;
//1. structure definition
//struct 结构体名 { 结构体成员变量列表 };
//结构体关键字 struct在定义时不能省略 ,但是在c++中创建结构体时可以省略
struct Book {
string name;
double price;
int value;
}cpp;
int main() {
//2.create a structure array
// Book 数组名[元素个数] ={ {},{},{}, … };
Book books[3] = {
{“C语言程序设计”,199.99,7},
{“Python零基础”,399.99,9},
{“C++零基础教程”,39.99,1000000}
};
//Modified the element of the structure array
books[2].name = “C++算法联盟”;
//CRUD copy read update delete 增删改查
// 这里是查找
for (int i = 0; i < 3; ++i) {
cout << books[i].name << ‘ ‘ << books[i].price << ‘ ‘ << books[i].value << endl;
}
return 0;
}
//10 – 3 structure pointer
//结构体指针
#include <iostream>
using namespace std;
// 1. structure definition
//struct 结构体名 { 结构体成员变量列表 };
//结构体关键字 struct在定义时不能省略 ,但是在c++中创建结构体时可以省略
struct Book {
string name;
double price;
int value;
};
int main() {
Book b = { “C语言”,99.99,7 };
Book c = b;
//拷贝
//因为b和c的地址不是同一个,这种方法改不掉b的
c.name = “B语言入门”;
//需要结构体指针,使用b的地址
Book* pb = &b;
//结构体指针必须用箭头访问、
//修改(地址)
pb->name = “C++”;
cout << b.name << ‘ ‘ << b.price << ‘ ‘ << b.value << endl;
return 0;
}
//10 – 4 nested structure
//嵌套结构体
#include <iostream>
using namespace std;
struct Point {
double x, y;
};
//在结构体里定义一个新的成员变量
struct Circle {
//double x, y;
//在结构体里定义一个新的成员变量 ,代替double x, y;
Point pt;//嵌套结构体
double radius;
};
struct Circles {
int size;
Circle c[100];
};
int main() {
Circle c;
c.pt.x = 9;
//通过多一个点得到下一级的对象
c.pt.y = 8;
c.radius = 5;
//定义两个圆 一个x是9 y是8 半径是5,第二个x是2 y是1 半径是1
Circles cs = {
2,{
{{9,8},5},
{{2,1},1}
}
};
//初始化两个圆完成
for (int i = 0; i < cs.size; ++i) {
Circle tmp = cs.c[i];
//赋值给临时变量,为了拿到Circle中的pt和radius
cout << “(” << tmp.pt.x << “,” << tmp.pt.y << “) ” << tmp.radius << endl;
}
//通过不断地去点 获得结构体的成员变量
return 0;
}
//10 – 5 structure parameter passing
//结构体传参
#include <iostream>
using namespace std;
struct Point {
double x, y;
};
struct Circle {
Point pt;
double radius;
};
//void printCircle(Circle c) {
// //这个c 传参时在内部不需要改,是只读的,所以用结构体本身
// cout << “(” << c.pt.x << “,” << c.pt.y << “) ” << c.radius << endl;
//}
void printCircle2(const Circle *c) { //好处1:只读时,加个const防止它在内部修改
//这个c 传参时在内部不需要改,是只读的,所以用结构体本身(不用指针用对象)
// 好处2:传对象 不传指针 的话 每次都会进行一次拷贝 有消耗 ,但是传地址是没有消耗的
//c->pt.x = 1; //不加const 这种就可以改了
//c->pt.x +=1 ;//常量不可修改 const value can’t be modified
cout << “(” << c->pt.x << “,” << c->pt.y << “) ” << c->radius << endl;
}
//
//void moveCircle(Circle c, int x, int y) {
// //cout << &c << endl;//地址不一样说明了函数传参传进来后进行了一次拷贝 并不是原来的结构体
// //在函数内部结构体的值并不会改变函数外部结构体的值
// c.pt.x += x;
// c.pt.y += y;
//}
void moveCircle2(Circle *c, int x, int y) {//这里使用结构体指针
//cout << &c << endl;//地址不一样说明了函数传参传进来后进行了一次拷贝 并不是原来的结构体
//在函数内部结构体的值并不会改变函数外部结构体的值
c->pt.x += x; //结构体指针使用需要 ->
c->pt.y += y;
}
int main() {
Circle c = { {9,8},5 };
//cout << &c << endl;
//moveCircle(c, 1, -2);//x正方向移动一个位置,y负方向移动两个位置
moveCircle2(&c, 1, -2);//用结构体指针需要传地址
//printCircle(c);
printCircle2(&c);
return 0;
}