El ingenio de los programadores dark horse = estructura

1. Definición de estructura

2. Matriz de estructuras

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

};
int main()
{
	stu stu1[2] = {
		{"张三",18, 90},
		{"李四",18, 90}
	};
	for (int i = 0; i < 2; i++)
	{
		cout << "姓名: " << stu1[i].name << "年龄: " << stu1[i].age << "分数:" << stu1[i].score << endl;
	}
	
		system("pause");
		return 0;
}

3. Puntero de estructura

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

};
int main()
{
	stu stu1 = {"张三",18, 90};
	stu* p = &stu1;
	cout<< "姓名:" << p->name << endl;
	system("pause");
	return 0;
}

Al definir un puntero a una estructura, tenga en cuenta que el tipo de puntero debe ser el mismo que el tipo de estructura. Cuando use punteros para acceder a elementos en la estructura, use "->"

4. Estructura estructura anidada

Preste atención al uso de estructura estructura anidada.

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

};
struct teacher
{
	string name;
	int age;
	struct stu stu1;
};
int main()
{
	teacher t;
	t.name = "a";
	t.age = 50;
	t.stu1.age = 20;
	t.stu1.name = "b";
	t.stu1.score = 90;
	cout << "老师姓名:" << t.name << " 老师学生姓名:" << t.stu1.name << endl;
	system("pause");
	return 0;
}

5. Estructura como parámetro de función

Hay dos formas de pasar datos de tipo de estructura a parámetros: paso de valor y paso de dirección;

Transferencia de valor: no cambiará el valor de la estructura en la función principal;

Transferencia de dirección: cambiará el valor de la estructura en la función principal.

#include <iostream>
using namespace std;
#include<string>
struct student
{
    string name;
    int age;
    int score;
};
void printstu(struct student stu)
{
    stu.age = 18;
    cout << "子函数1姓名:" << stu.name << " 子函数1年龄:" << stu.age << " 子函数1分数:" << stu.score << endl;
}
void printstu2(student *p)
{
    p->age = 18;
    cout << "子函数2姓名:" << p->name << " 子函数2年龄:" << p->age << " 子函数2分数:" << p->score << endl;
}
int main()
{
    student stu;
    stu.name = "hhhh";
    stu.age = 16;
    stu.score = 90;
    printstu(stu);
    cout << "主函数1姓名:" << stu.name << " 主函数1年龄:" << stu.age << " 主函数1分数:" << stu.score << endl;
    printstu2(&stu);
    cout << "主函数2姓名:" << stu.name << " 主函数2年龄:" << stu.age << " 主函数2分数:" << stu.score << endl;
    system("pause");
    return 0;
}

6, el uso de const en la estructura.

Al pasar una estructura como un parámetro de función, si utiliza la transferencia de valor, la memoria necesita copiar todos los valores en la estructura y asignarlo a la función al pasarlo;

Si se usa el puntero para pasar, el puntero solo ocupa cuatro bytes (32 bits) y la memoria requerida se reduce considerablemente. Modifique el valor original por error en la función y cambie el parámetro de tipo de puntero a tipo constante.

#include <iostream>
using namespace std;
#include<string>
struct student
{
    string name;
    int age;
    int score;
};

void printstu2(const student *p)
{
    //p->age = 18;//错误操作,const修饰*,此时不能改变指针指向的值;
    cout << "子函数2姓名:" << p->name << " 子函数2年龄:" << p->age << " 子函数2分数:" << p->score << endl;
}
int main()
{
    student stu;
    stu.name = "hhhh";
    stu.age = 16;
    stu.score = 90;
   
    printstu2(&stu);
    cout << "主函数2姓名:" << stu.name << " 主函数2年龄:" << stu.age << " 主函数2分数:" << stu.score << endl;
    system("pause");
    return 0;
}

 

Supongo que te gusta

Origin blog.csdn.net/yyyllla/article/details/109299343
Recomendado
Clasificación