c++程序来比较年龄,如果5个人,并打印最年长的人的名字
C++ program to compare ages if 5 persons and print the name of the eldest person
我想写一个c++程序,我需要接受5个人的名字,年龄和地址,并显示最年长的人的名字。我能找到最年长的人的年龄,但不知道如何打印与年龄相对应的名字。请看看我的代码,并帮助我如何做到这一点。
我的c++代码是:#include <iostream>
using namespace std;
struct student{
char name[20];
int age;
char add[40];
};
int main()
{
student stu[5];
int i, max;
for (i = 0; i <=4; i++)
{
cout << "n Enter name";
cin >>stu[i].name;
cout << "n Enter age";
cin >>stu[i].age;
cout << "n Enter address";
cin >>stu[i].add;
}
max = stu[0].age;
for ( i = 0; i <=4; i++)
{
if (stu[i].age > max)
max = stu[i].age;
}
cout << "Max age is: " <<max;
return 0;
}
我能找到最大年龄。请帮我如何显示年龄最大的人的名字
不存储遇到的最大年龄,而是尝试存储一个指向最大年龄记录的指针。像这样:
student* max = &stu[0];
for ( i = 0; i <=4; i++)
{
if (stu[i].age > max->age)
max = &stu[i];
}
std::cout << "Student with max age is " << max->name << std::endl;
您可以使用<algorithm>
中的std::max_element
来检索指向max元素的迭代器:
auto it = std::max_element(std::begin(stu), std::end(stu),
[](const student& lhs, const student& rhs) {
return lhs.age < rhs.age;
});
std::cout << "Oldest is " << it->name << " with " << it->age << std::endl;
生活例子
您可以保留达到最大值的索引,这样您就可以访问该条目中的所有信息。
int maxi = 0;
for ( i = 1; i <=4; i++)
{
if (stu[i].age > stu[maxi].age)
{
maxi = i;
}
}
cout << "max age is " << stu[maxi].age << " and name is " << stu[maxi].name;
你就快成功了。当你找到年龄的时候,就保留最大的:
student eldest;
for ( i = 0; i <=4; i++)
{
if (stu[i].age > max)
{
max = stu[i].age;
eldest = stu[i];
}
}
cout << "Max age is: " <<max << " named " << eldest.name;
return 0;
}
添加另一个类型为student的变量maxStudent,当if语句为真时设置该变量。最后,您应该有正确的学生,您可以打印出每个字段。
将max的类型从int改为Student-
student max;
max.age = stu[0].age;
strcpy( stu[0].name, max.name );
strcpy( stu[0].add, max.add );
for ( i = 0; i <=4; i++)
{
if (stu[i].age > max)
max.age = stu[i].age;
strcpy( stu[i].name, max.name );
strcpy( stu[i].add, max.add );
}
您的代码通常缺乏std::vector
和std::string
。除非有充分的理由,否则不应该使用数组。
结构体student
应该是:
struct student{
std::string name;
int age;
std::string add;
};
同样,main
中的数组应该变成:
std::vector<student> stu;
请注意,您不再需要考虑大小。
接下来的事情是找到最年长的人。c++已经具备了在中找到最伟大的东西的功能。在<algorithm>
头文件中,有一个名为std::max_element
的函数正是这样做的。它适用于任何具有"小于"特定含义的东西。例如,两个int
变量具有"小于"关系,例如在"4小于5"中。
您的student
结构体没有这样的自然关系,但是您可以通过提供operator<
:
struct student{
std::string name;
int age;
std::string add;
bool operator<(student const &other) const
{
return age < other.age;
}
};
这将工作:
std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end());
int max_age = eldest_student_iter->age;
现在有人可能会说,这给student
结构体本身增加了太多的功能,并且使age
比其他两个成员更重要,这在逻辑上可能是不合理的。
幸运的是,std::max_element
有第二个版本,它接受第三个形参;所谓的函子,它告诉函数如何比较两个元素。下面是一个例子:
struct StudentComparison
{
bool operator()(student const &lhs, student const &rhs) const
{
return lhs.age < rhs.age;
}
};
std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end(), StudentComparison());
int max_age = eldest_student_iter->age;
这应该很好地包装在自己的函数中:
int GetHighestAge(std::vector<student> const &stu)
{
std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end(), StudentComparison());
int max_age = eldest_student_iter->age;
return max_age;
}
在实际代码中,由于student的向量为空,所以没有最大元素时,您还必须确定应该发生什么。
int GetHighestAge(std::vector<student> const &stu)
{
if (stu.empty())
{
// error...
}
std::vector<student>::const_iterator eldest_student_iter = std::max_element(stu.begin(), stu.end(), StudentComparison());
int max_age = eldest_student_iter->age;
return max_age;
}
如果使用c++ 11,代码可以写得更简洁。您不再需要在其他地方定义StudentComparison
函子,而是可以使用所谓的lambda直接在调用位置定义比较。您可以使用auto
来避免过于冗长的std::vector<student>::const_iterator
。
auto eldest_student_iter = std::max_element(stu.begin(), stu.end(), [](student const &lhs, student const &rhs) { return lhs.age < rhs.age; });
最后,这里是一个完整的玩具示例:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
struct student{
std::string name;
int age;
std::string add;
};
int GetHighestAge(std::vector<student> const &stu)
{
if (stu.empty())
{
return -1;
}
auto eldest_student_iter = std::max_element(stu.begin(), stu.end(), [](student const &lhs, student const &rhs) { return lhs.age < rhs.age; });
int max_age = eldest_student_iter->age;
return max_age;
}
int main()
{
std::vector<student> stu;
// some test data:
student s1;
s1.name = "One";
s1.age = 18;
s1.add = "Somewhere";
student s2;
s2.name = "Two";
s2.age = 21;
s2.add = "Somewhere Else";
student s3;
s3.name = "Three";
s3.age = 20;
s3.add = "Yet Somewhere Else";
stu.push_back(s1);
stu.push_back(s2);
stu.push_back(s3);
std::cout << "Max age: " << GetHighestAge(stu) << "n";
}
最后一个提示:存储出生日期而不是年龄。出生日期永远不变,年龄过时。
这是您的程序编辑后的版本,希望您喜欢。它显示了最年长的人的所有细节
//从学生的所有详细信息中找到最高年龄
#include < iostream >
using namespace std;
struct student
{
char name[20];
int age;
char add[40];
};
int main()
{
student stu[5];
int i, n, max;
cout << "nenter the no. of record you want to enter : ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "n Enter name : ";
cin >> stu[i].name;
cout << "n Enter age : ";
cin >> stu[i].age;
cout << "n Enter roll : ";
cin >> stu[i].add;
}
max = stu[0].age;
for (i = 0; i < n; i++)
{
if (stu[i].age > max)
max = stu[i].age;
}
cout << "nHighest age report detail -----";
for (i = 0; i < n; i++)
{
if (stu[i].age == max)
{
cout << "n your name : " << stu[i].name;
cout << "n your age : " << stu[i].age;
cout << "n your roll : " << stu[i].add;
}
}
return 0;
}
这个程序是完全编译的
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 将时间戳打印到流的最简单方法
- BFS 打印最短路径
- 从用户那里获得无限量的输入,而不是打印最大的3个,而不使用数组
- 二叉搜索树使用最常用的节点来打印出某些语句
- 如何使用递归打印最长公共子序列中涉及的字符串?
- 按升序打印矢量的所有元素直到它为空而没有重复项的最有效方法是什么?
- 打印所有最长匹配的子字符串
- 使用一个功能打印最大值和平均值
- 为什么C++打印出奇怪的结果的最小值?
- 链表不会打印最大值
- 打印可以重复以获取原始字符串的最小字符串
- 打印出矩阵中最长的增加路径
- 使用BFS存储和打印最短路径
- 如何在 c++ 中有效使用容器的情况下打印 5 个值的最大索引
- 正确的代码以打印交换的最大值和最小值
- 将解决方案打印为数组中非连续元素的最大总和
- 如何从我的结构中打印人口最多的状态
- c++程序来比较年龄,如果5个人,并打印最年长的人的名字
- 如何将此程序中的员工数据从最年长的人排序为最年轻的人?C++