I am stuck with the last part of my assignment due in 3 hours. The instructions are to implement an IsBefore() function:
• A is before B if A’s last name is alphabetically before B’s
• If the same then, A is before B if A’s first name is alphabetically before
B’s
• If the same then, A is before B if A is younger than B
• If the same then choose a arbitrary but consistent order
I have a class of persons and a vector of them. the class as a constructor for age, first name, last name.
I need to sort the list of type persons by the above function but I am totally lost
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int main(){
Person person;
std::vector<Person> db; //vector of class persons
}
//this is the function we need from the professor
bool IsBefore(P a, P, b){
if(!a.valid()) returnfalse;
if(!b.valid()) returntrue;
if(a.last()!=b.last()) return (!a.last()<b.last);
returnfalse;
}
why is it saying person& is undefined? I have a Person person ..
Also, why do I need to return a bool (not sure what I would use it with in main anyway) to sort a vector? does the sort() take a bool as an argument?
if its more clear what I am talking about here are the full instructions
Write a C++ program to manage a database of up to 100 Persons. A Person
consists of at least a name (first and last), age, address, and gender. Provide
the user with the following menu:
Add a person
Delete a person
Print database information
Report average age
List all names
Exit
Implement an IsBefore() function:
• A is before B if A’s last name is alphabetically before B’s
• If the same then, A is before B if A’s first name is alphabetically before
B’s
• If the same then, A is before B if A is younger than B
• If the same then choose a arbitrary but consistent order
I have everything working but the isBefore() function, not understanding it...
and make_tuple is something we have not gone over, its an introductory c++ class
Wow, had a ; after the default constructor so all the class proto types where private....
How would I call it in main?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
if(choice == 2){ //display names
std::sort(db.begin(), db.end(), isBefore()); //db is the Person vector variable
for(std::vector<Person>::size_type i = 0; i != db.size(); i++) {
db[i].printNames();}
}
bool is_before( const Person& a, const Person& b )
{
if( a.getFname() < b.getLname ) returntrue ; //getFname() is the function returning the first name
elseif( a.getLname > b.getLname ) returnfalse ;
// last names are the same
if( a.getFname < b.getFname ) returntrue ;
elseif( a.getFname > b.getFname ) returnfalse ;
// first names too are the same
return a.getage< b.getage ;