Introduction to C + + to understanding phase II Basic chapter -- C + + structure

Keywords: C++

1. overview

We have learned that c + + has built-in common data types, such as int, long, double, etc., but if we want to define a student's data type, c + + doesn't have one. At this time, we need to use the structure, in other words, the structure can help us define our own data types.

2. Structure definition and use

Format struct structure name {/ / member list};

For example, define a student type structure

struct Student
{
	string name;
	int age;

};

The data type of Student is defined above. How to create a data type of Student? There are three ways, one or two are recommended

First kind

#include <iostream>
#include <string>
using namespace std;
struct Student
{
	string name;
	int age;

};
int main() {
	//First, create and assign
	Student s1;
	s1.name = "Zhang San";
	s1.age = 12;
	cout << s1.age << s1.name;
}

Second kinds

#include <iostream>
#include <string>
using namespace std;
struct Student
{
	string name;
	int age;

};
int main() {
	//Second kinds
	struct Student s1 = {"Li Si",12};

	cout << s1.age << s1.name;
}

Third kinds

#include <iostream>
#include <string>
using namespace std;
struct Student
{
	string name;
	int age;

}s1;
int main() {
	s1.age = 12;
	s1.name = "lisi";
	cout << s1.age << s1.name;
}

3. Structure array

#include <iostream>
#include <string>
using namespace std;
//1. Define a student structure
struct student
{
	string name;
	int age;

};


int main() {
	//2. Define structure array
	struct student arr[3] =
	{
		{"aaa",12},
		{"bbb",12},
		{"ccc",12}
	};
	//3. Structure variable assignment
	arr[2].age = 20;
	arr[2].name = "ddd";
	//4. Access structure array
	for (int i = 0; i < 3; i++) {
		cout << arr[i].age << arr[i].name <<endl;
	}
}

4. Structure pointer

#include <iostream>
#include <string>
using namespace std;
//1. Define a student structure
struct student
{
	string name;
	int age;

};


int main() {
	
	struct student s = { "lisi",12 };
	//2. Define a structure pointer
	struct student* p = &s;
	//4. Use the structure pointer to access the properties in the structure. You need to use - >
	cout << p->age << p->name;
	
}

5. Nested structure

#include <iostream>
#include <string>
using namespace std;
//1. Define a student structure
struct student
{
	string name;
	int id;
};
//2. Define a nested structure
struct school {
	string name;
	int id;
	struct student s;
};
int main() {
	//3. Create the school variable
	school sc = {};
	sc.id = 1;
	sc.name = "tsinghua";
	sc.s.id = 2;
	sc.s.name = "lisi";
	cout << sc.id << sc.name << sc.s.id << sc.s.name << endl;
}

6. Transfer of structure as function parameter

The first is passed as a value (the arguments are not modified)

#include <iostream>
#include <string>
using namespace std;
//1. Define a student structure
struct student
{
	string name;
	int id;
};
void p(struct student s);
int main() {
	struct student s = { "lisi",10 };
	p(s);
	cout << "id:" << s.id <<"Full name:"<< s.name<<endl;//id:10 name: lisi
	return 0;
}
//2. Define a function
void p(struct student s) {
	s.id = 100;
	cout << "id:" << s.id <<"Full name:" << s.name << endl;//id:100 Name: lisi
}

 

The second is passed as an address (the arguments will be modified)

#include <iostream>
#include <string>
using namespace std;
struct student
{
	string name;
	int id;
};
void p(struct student *s);
int main() {
	struct student s = { "lisi",10 };
	p(&s);
	cout << "id:" << s.id <<"Full name:"<< s.name<<endl;//id:100 Name: lisi
	return 0;
}
void p(struct student *s) {
	s->id = 100;
	cout << "id:" << s->id <<"Full name:" << s->name << endl;//id:100 Name: lisi
}

Be careful:

//The use of address passing can avoid the problem that a large number of variable assignments occupy space and improve efficiency. However, the actual parameters will be modified. How to solve this problem?
void p(const struct student* s) {//After using const decoration, only read and modify the data for address delivery
	//S - > id = 100; will not be modifiable
	cout << "id:" << s->id << "Full name:" << s->name << endl;//id:100 Name: lisi
}



Posted by michaelphipps on Fri, 22 Nov 2019 09:27:31 -0800