Initialization of structure array in C++ (from "Algorithm Notes")

Take PAT(A)1025 as an example:

struct every {
    
    
	int local; // 测试地点
	int num;   // 注册编号
	int score; // 得分
	every() {
    
    }; // 用以不经初始化定义everystudent[30005]
	every(int _local, int _num, int _score) : local(_local), num(_num), score(_score) {
    
    };
	// 用以提供num和score的初始化
}everystudent[30005];

// 在main函数中再加上这几句即可完成,对结构体数组的初始化
for (int i = 1; i <= n; i++) // 初始化结构体数组
	{
    
    
		int k; // 每个测试地点的测试人数
		cin >> k;
		for (int j = 1; j <= k; j++)
		{
    
    
			cin  >> num >> score;
			everystudent[count++] = every(i, num, score);
		}
	}

This is a method of dynamically initializing a structure array. It is easier to illustrate with this example. Of course, there are other ways to initialize the structure array. For the detailed and complete code of PAT(A)1025, please refer to other articles in the blog.

Guess you like

Origin blog.csdn.net/qq_27538633/article/details/105644558