It is not clear why the function is called as find_oldest.
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] );
}
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] );
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…