Sortting a vector

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()) return false;
	if(!b.valid()) return true;

	if(a.last()!=b.last()) return (!a.last()<b.last);

	return false;
}

 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool is_before( const person& a, const person& b )
{
    if( a.last_name < b.last_name ) return true ;
    else if( a.last_name > b.last_name ) return false ;

    // last names are the same
    if( a.first_name < b.first_name ) return true ;
    else if( a.first_name > b.first_name ) return false ;

    // first names too are the same
    return a.age < b.age ;

   /* essentially:
           return std::make_tuple( a.first_name, a.last_name, a.age ) < 
                  std::make_tuple( b.first_name, b.last_name, b.age ) ; */
}
Last edited on
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
Last edited on
> I have a Person person

If the name of the type is Person (with an upper case P), then:

1
2
// bool is_before( const person& a, const person& b ) ;
bool is_before( const Person& a, const Person& b ) ;



> does the sort() take a bool as an argument?

An overloaded version of std::sort() takes a predicate to compare the objects as the third argument.
See the example here: http://www.cplusplus.com/reference/algorithm/sort/
Last edited on
I have a constructor for Person and getters and setters but I cant access any of them in the isBefore function
Is the isBefore function allowed access? Do you have getters and setters set as protected/private?
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 ) return true ; //getFname() is the function returning the first name
    else if( a.getLname > b.getLname ) return false ;

    // last names are the same
    if( a.getFname < b.getFname ) return true ;
    else if( a.getFname > b.getFname ) return false ;

    // first names too are the same
    return a.getage< b.getage ;


Last edited on
How would you call the constructor you mean? I would imagine that
Person * person = new Person;
would work just fine.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct person
{
    std::string first_name ;
    std::string last_name ;
    int age ;

    // ...

    person() : age(1) {}

    person( const std::string& fn, const std::string& ln, int a )
      : first_name(fn), last_name(ln), age(a) { /* assert( valid() ) ; */ }
};

int main()
{
    std::vector<person> db ;

    std::string first_name ;
    std::string last_name ;
    int age ;

    while( std::cin >> first_name >> last_name >> age )
    {
        db.push_back( person( first_name, last_name, age ) ) ;
        // or, better: db.emplace_back( first_name, last_name, age ) ; // c++11
        // or: db.push_back( { first_name, last_name, age } ) ; // C++11
    }
}
Topic archived. No new replies allowed.