如何排序员工数据在此计划从最老的人到最小的人? C ++ [英] How Do I Sort The Employee Data In This Program From Oldest Person To Youngest Person? C++

查看:138
本文介绍了如何排序员工数据在此计划从最老的人到最小的人? C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我有一个程序,将员工数据的文件读入结构数组。我有一个功能,搜索数组,找到最老的人,并打印出该人的数据。但是现在我需要修改一个函数,通过数组排序,并将最旧的人数据移动到数组中的第一个位置,第二个最旧的第二个位置等,并对所有1000名员工做相同的事情,然后打印从最旧到最小的所有员工数据。

Here i have a program that reads a file of employee data into an array of structs. I have a function that searches the array and finds the oldest person and prints out that person's data. But now i need to modify a function that sorts through the array and moves the oldest persons data to the 1st position in the array, the 2nd oldest to the 2nd position and so on and does the same for all 1000 employees so that it will sort then print all employee data from oldest to youngest. How could i do this?

这是前几行数据来了解布局(出生日期:YYYYMMDD

Here is the first few lines of data to give an idea of the layout(date of birth is laid out YYYYMMDD

114680858 19670607 Matilda Vincent MI

114930037 19471024 Desdemona Hanover ID

115550206 19790110 Xanadu Perlman ND

116520629 19630921 Alexander Hall SD



struct employees // employee data
{
int ss_number;//social security
int dob;//date of birth YYYY/MM/DD Ex.) 19870314=1987/03/14
string f_name;
string l_name;
string state; //state of residence 
};


void read_file()//read file into array of 1000 structs
{
ifstream data("/home/www/class/een118/labs/database1.txt");
employees array[1000]
if(!data.fail())
{
int i;
for(int i=0;i<1000;i++)
{
data>>array[i].ss_number
>>array[i].dob
>>array[i].f_name
>>array[i].l_name
>>array[i].state;
}
for(int i=0;i<1000;i++)
{
cout<<array[i].ss_number>>" "<<array[i].dob>>" "<<array[i].f_name>>" "<<
array[i].l_name>>" "<<array[i].state;
}}}


void print_person(employees e)
{
cout<<e.ss_number>>" "<<e.dob>>" "<<e.f_name>>" "<<e.l_name>>" "<<e.state;
}


void find_oldest(employees array[])// oldest person = smallest dob
{
int i;
int index=0
int oldest=1000000000;//dummy variable
for(i=1;i<1000;i++)//1000 is array length
{
if(array[i].dob<oldest)
{
index=i;
oldest=array[i].dob;
}
}
print_person(array[i]);
}


int main()
{
employees array[1000];
read_file(array);
find_oldest(array);
}


推荐答案

函数被称为find_oldest。

It is not clear why the function is called as find_oldest.

不过,您可以使用标准算法 std :: sort 与可以是lambda表达式的比较函数。例如

Nevertheless you could use standard algorithm std::sort with a compare function that can be a lambda expression. For example

#include <iostream>

void find_oldest( employees array[], size_t n )
{
   std::sort( array, array + n, 
              []( const employees &e1, const employees &e2 )
              {
                 return ( e1.dob > e2.dob );
              } );  

   for ( size_t i = 0; i < n; i++ ) print_person( array[i] );
}

而不是使用lambda表达式的另一种方法是declare operator> 用于结构员工或功能对象。
在我看来,最好定义一个功能对象。在这种情况下,对于任何类型的排序,例如通过名字或姓氏,你可以使用一个单独的功能对象。例如

The other way instead of using the lambda expression is either declare operator > for structure employees or a functional object. In my opinion it is better to define a functional object. In this case for any kind of sorting for example by first names or by last name you could use a separate functional object. For example

struct employees // employee data
{
   int ss_number;//social security
   int dob;//date of birth YYYY/MM/DD Ex.) 19870314=1987/03/14
   string f_name;
   string l_name;
   string state; //state of residence 
   struct sort_by_dob
   {
      bool operator ()( const employees &e1, const employees &e2 ) const
      {
         return ( e1.dob > e2.dob );
      }
   };
};



void find_oldest( employees array[], size_t n )
{
   std::sort( array, array + n, employees::sort_by_dob() );  

   for ( size_t i = 0; i < n; i++ ) print_person( array[i] );
}

这篇关于如何排序员工数据在此计划从最老的人到最小的人? C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆