请问这个指针该怎样使用 Max函数中数组的值怎样传递进去呢 这个程序该怎么修改啊😭
这个问题蛮简单的
首先先看要求:
#include <iostream>
class Student
{
public:
int id;
int grade;
friend int max(const Student* stu) noexcept;
};
inline int max(const Student* stu) noexcept
{
// we know the length of the student array is 5
Student max = * stu;
for(int i = 1; i < 5; i++)
{
if(max.grade < (stu + i)->grade)
max = *(stu + i);
}
return max.id;
}
int main()
{
// create the student array
Student arr[5] = {
{1, 90}, {2, 80}, {3, 70}, {4, 95}, {5, 100}
};
std::cout << max(arr) << '\n';
}
max函数返回值类型改为int
max函数改为
int max(Student *p,int n)
{
int grade = 0;
for(int i=0;i<n;i++)
if(p[i].grade > grade)
grade = p[i].grade;
return grade;
}
main函数调用为max(std);
cout<<max(std)<<endl;