需要对名称列表进行排序,如何重载和定义 bool operator <?

Need to sort list of names, how do I overload and define bool operator <?

所以老师构建了一个 class 的主体和原型,构建和修改了 phone 本书。现在我几乎没有定义 class 函数,但是 运行 在定义他的原型之一 bool operator<(Person p) 时遇到了麻烦。我认为它应该以某种默认方式对名称列表进行排序,但我不知道要在括号中填写什么以重载此运算符。 我列出了老师建造的主要建筑、他给我们的原型,以及我迄今为止在定义它们方面所做的工作。您可以在原型和我的作品中找到 bool operator<(Person p)。 请注意,我不允许对教师主要或原型进行任何更改。

主要:

#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include "phonebook.h"

using namespace std;

int main()
{

    vector<Person> phone_book;
    string name;
    int number;
    int answer;

    srand((int)(time(0)));

    phone_book.push_back(Person("Bruin, Joe", 5556456));
    phone_book.push_back(Person("Simpson, Homer", 5557471));
    phone_book.push_back(Person("Duffman, Barry", 5533331));


    cout <<"\n";
    cout << "Your phone book contains the following names and numbers: \n";

    for (int i=0; i < phone_book.size(); i++)
    {

        phone_book[i].print();
        cout << "\n";
    }
    cout <<"\n";
    answer=0;

    while (answer != 8)
    {
        cout << "\nChoose from the following options:\n\n";

        cout << "1) Add people to the phone book.\n";
        cout << "2) Erase a person from the phone book.\n";
        cout << "3) Sort the phone book.\n";
        cout << "4) Shuffle the phone book.\n";
        cout << "5) Reverse the phone book.\n";
        cout << "6) Print the phone book.\n";
        cout << "7) Look up a person in the phone book.\n";
        cout << "8) Quit.\n\n";

        cin >> answer;

        string clear;

        getline(cin, clear);

        if (answer == 1)
            add_people(phone_book);
        else if (answer == 2)
        {
            cout << "Enter a name: ";
            getline(cin, name);
            erase(phone_book, name);

        }
        else if (answer == 3)
            sort(phone_book);
        else if (answer == 4)
            shuffle (phone_book);
        else if (answer == 5)
            reverse(phone_book);
        else if (answer == 6)
        {
            cout <<"\n";
            cout << "Your phone book contains the following names and numbers: \n";
            print(phone_book);
        }
        else if (answer ==7)
        {                   
            cout << "Enter a name: ";
            getline(cin, name);
            int number = lookup(phone_book, name); 
            if (number > 0)
            {
                cout << "\n\nThe number for " << name << " is: " << number << "\n\n";
            }
            else
                cout << name << " not found in the phone book.\n";
        }
    }
    return 0;
}

原型:

#ifndef PHONEBOOK_H
#define PHONEBOOK_H
#include <string>
#include <vector>
using namespace std;

class Person
{
public:
    Person();
    Person(string new_name, int new_phone);
    string get_name() const;
    int get_phone() const;
    bool operator < (Person p) const;
    void print() const;

private:
    string name;
    int phone;
};

void add_people(vector<Person> &phone_book);
void erase(vector<Person> &phone_book, string name);
void sort(vector<Person> &phone_book);
void shuffle(vector<Person> &phone_book);
void reverse(vector<Person> &phone_book);
void print(vector<Person> &phone_book);
int lookup(const vector<Person> &phone_book, string name);
#endif

我目前的工作:

#include <iostream>
#include <string>
#include <vector>
#include <time.h>
#include "phonebook.h"

Person::Person()
{
    name = "NONE";
    phone = 0000000;

}
Person::Person(string new_name, int new_phone)
{
    name=new_name;
    phone=new_phone;
}
string Person::get_name() const
{
    return name;
}
int Person::get_phone() const
{
    return phone;
}
bool Person::operator < (Person p) const
{
    return ??????????????????????????????????????;
}
void Person::print() const
{
    cout << name << " " << phone << endl;
}

你只是return他们名字的比较结果:

bool Person::operator<(Person const& p) const // better take by reference-to-const
{
    return name < p.name;
}

如果您的老师还没有解释参考资料,请不要在参数中使用 const&。但是如果 he/she 有(因为我在你的代码中看到了它们),你应该问为什么参数类型中没有。