基础之基础之最后一节-结构体
1.结构体的定义
结构体相对于自定义的一种新的变量类型。
四种定义方式,推荐第一种;第四种适合大量定义,也适合查找;
#include <iostream>
using namespace std;
#include <string.h>struct Student
{string name;int age;float score;
}s3;void prinf(Student s)
{cout << s.name << " | " << s.age << " | " << s.score << endl;
}int main()
{Student s1;s1.name = "niuniu";s1.age = 18;s1.score = 96;prinf(s1);Student s2 = { "zhangsan", 92, 30 };prinf(s2);s3.name = "lisi";s3.age = 13;s3.score = 31;prinf(s3);//shuzuStudent arr[3] = {{"zhangsan", 31,55},{"lisi", 41, 42},{"wangwu", 32,44}};for (int i = 0; i < 3; ++i){prinf(arr[i]);};return 0;}
2.结构体指针
和其他类型的指针一致;需要注意的是,如果s是结构体的指针,不仅可以使用(*s).name还可以使用s->name访问结构体的属性;
#include <iostream>
using namespace std;
#include <string.h>struct Student
{string name;int age;float score;
}s3;void prinf(Student* s)
{cout << s->name << " | " << s->age << " | " << s->score << endl;
}int main()
{Student s1;s1.name = "niuniu";s1.age = 18;s1.score = 96;prinf(&s1);Student s2 = { "zhangsan", 92, 30 };prinf(&s2);s3.name = "lisi";s3.age = 13;s3.score = 31;prinf(&s3);//shuzuStudent arr[3] = {{"zhangsan", 31,55},{"lisi", 41, 42},{"wangwu", 32,44}};for (int i = 0; i < 3; ++i){prinf(&arr[i]);};return 0;}
3.结构体嵌套结构体
类似于函数中包含函数;
#include <iostream>
using namespace std;
#include <string.h>struct teacher
{int id;string name;int age;student std;
};struct student
{string name;int age;int score;
};
结果报错,因为需要把student放在前面!!!
4.案例
4.1给三国英雄排序;使用结构体,指针
#include <iostream>
#include <string.h>
using namespace std;struct hero
{string name;int age;string gender;
};void prf(hero arr[5])
{for (int i = 0; i < 5; i++){cout << arr[i].name << " | " << arr[i].age << " | " << arr[i].gender << endl;}
}void sort(hero arr[5], const int nums)
{for (int i = 0; i < nums; i++){int counts = nums;for (int j = 0; j < nums - i - 1; j++){if (arr[j].age > arr[j + 1].age){hero tmp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = tmp;counts--;}}}
}int main()
{hero arr[5] = {{"liubei",22,"nan"},{"guanyu", 22, "nan"},{"zhangfei", 20, "nan"},{"zhaoyun", 21, "nan"},{"diaochan", 19, "nv"}};sort(arr, 5);prf(arr);return 0;}
4.2通讯录案例
#include <iostream>
#include <string.h>
#define MAX 1000
using namespace std;struct People
{string name;int gender;int age;long phone;string address;
};struct AddressBook
{People addressbook[MAX];int nums;
};void init()
{cout << "*****************" << endl;cout << "please choice one" << endl;cout << "1. add contact" << endl;cout << "2. display contact" << endl;cout << "3. delete contact" << endl;cout << "4. search contact" << endl;cout << "5. revise contact" << endl;cout << "6. delete all contact" << endl;cout << "7. out system" << endl;cout << "*****************" << endl;
}void add(AddressBook* abs)
{string name;int gender;int age;long phone;string address;cout << "please input contact name" << endl;cin >> name;cout << "please input contact gender (1: nan; 2: nv" << endl;cin >> gender;cout << "please input contact age" << endl;cin >> age;cout << "please input contact phone" << endl;cin >> phone;cout << "please input contact address" << endl;cin >> address;abs->addressbook[abs->nums].name = name;abs->addressbook[abs->nums].age = age;abs->addressbook[abs->nums].gender = gender;abs->addressbook[abs->nums].phone = phone;abs->addressbook[abs->nums].address = address;abs->nums++;
}void display(AddressBook* abs)
{for (int i = 0; i < abs->nums; i++){cout << "name : " << abs->addressbook[i].name << "\t";cout << "gender : " << abs->addressbook[i].gender << "\t";cout << "age : " << abs->addressbook[i].age << "\t";cout << "phone : " << abs->addressbook[i].phone << "\t";cout << "address : " << abs->addressbook[i].address << "\t";cout << endl;};
}int Search(AddressBook* abs, int* p_c)
{string name;int idx = 0;cout << "please input contact name" << endl;cin >> name;for (int i = 0; i < abs->nums; i++){if (abs->addressbook[i].name == name){if (*p_c != 4){return i;}cout << "name : " << abs->addressbook[i].name << "\t";cout << "gender : " << abs->addressbook[i].gender << "\t";cout << "age : " << abs->addressbook[i].age << "\t";cout << "phone : " << abs->addressbook[i].phone << "\t";cout << "address : " << abs->addressbook[i].address << "\t";cout << endl;}}
}void Delete(AddressBook* abs, int* p_c)
{int idx = Search(abs, p_c);for (int i = idx; i < abs->nums; i++){abs->addressbook[i] = abs->addressbook[i + 1];}abs->nums--;
}void Revise(AddressBook* abs, int* p_c)
{Delete(abs, p_c);add(abs);
}void DelAll(AddressBook* abs)
{abs->nums = 0;
}int main()
{int choice = 0;AddressBook book;book.nums = 0;// init book.addressbook[book.nums].name = "aaa";book.addressbook[book.nums].age = 13;book.addressbook[book.nums].gender = 1;book.addressbook[book.nums].phone = 10010;book.addressbook[book.nums].address = "dalian";book.nums++;book.addressbook[book.nums].name = "ooo";book.addressbook[book.nums].age = 52;book.addressbook[book.nums].gender = 2;book.addressbook[book.nums].phone = 10086;book.addressbook[book.nums].address = "suzhou";book.nums++;// endwhile (1){init();cin >> choice;// add, display, delete, search, revise, delete all, outswitch (choice){case(1):{add(&book);break;};case(2):{display(&book);break;};case(3):{Delete(&book, &choice);break;};case(4):{Search(&book, &choice);break;};case(5):{Revise(&book, &choice);break;};case(6):{DelAll(&book);break;};default:break;}if (choice == 7){break;};system("pause");system("cls");}
}
总结一下:
1.对于python来说,数组传入函数时,相当于传入c++指针,因为函数中改变数组中的值,外部的数组也会改变;所以不想改变的时候,需要使用deep.copy。
2.对于c++来说,不仅数组传入相对于指针,对于整型、实型及结构体,只要使用&a,把a的指针传入函数,在函数中修改后,外部的值也会改变。
3.对于c++的数组来说,就算不使用*p,arr本身也是地址。所以在函数中对于传入的arr来说,不需要使用指针,也能干指针的活;