如何在结构中记录前5个值
How to record top 5 values in struct
我在下面有这个结构
struct Income
{
string firstname;
string lastname;
double income;
};
struct World
{
Income people[100];
} myWorld;
我希望打印并显示收入前五名的名字和姓氏。
什么是整理和阅读收入并按名字打印收入前五名的好方法?
假设我在这个结构中有100个值。
您想要使用std::sort
,它接受一个比较函数,类似于:
#include <algorithm>
using namespace std;
vector<Income> v;
for (auto i : myWorld.people) {
v.push_back(i);
}
sort(v.begin(), v.end(), [](const Income& i, const Income& j) { return i.income > j.income; });
然后简单地打印前5个,类似于:
for (size_t i = 0; i < 5; ++i) {
cout << v[i].firstname << " " << v[i].lastname << endl;
}
相关文章:
- 获取向量C++中第一个值和最后一个值的和
- 如何在 C/C++ 中生成具有 n 组 5 个值(重复项)的所有可能排列的矩阵
- C++递归来决定数组中的两个值
- 运算符 [] 重载更新 2 个值 c++
- 我正在尝试根据 4 个方程找出六个值
- 如何在C++向量中解压缩多个值
- 单个变量如何存储多个值?
- 如何创建具有多个值的多个变量
- 从 AVL 树中删除指向 1 个或多个值的键
- 为什么 getch 在按下函数或箭头键时返回三个值?
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 具有多个值的无序列图及其查找
- 从递归函数 C++ 返回多个值
- 如何将 txt 文件中的多个值输入到数组C++中
- 将多个值插入到特定位置的矢量中
- Visual Studio(C++):如何使用TCP套接字发送多个值
- 使用函数的元组返回多个值的问题
- C++:如何使用传递引用在一个方法中返回多个值
- 如何有效地用第二个值对向量对进行分组
- 如何在结构中记录前5个值