gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:57:47 25 4
gpt4 key购买 nike

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

主要:

#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;
}

原型(prototype):

#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;
}

最佳答案

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

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

如果您的老师还没有解释引用,也许不要在参数中使用const&。但如果他/她这样做了(因为我在你的代码中看到了它们),你应该问为什么参数类型中没有。

关于c++ - 需要对名称列表进行排序,如何重载和定义 bool operator <?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33823155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com