我必须为类项目实现通用二进制搜索功能。已经为我提供了测试文件和头文件(类定义),并且无法修改。
我能够使用我测试过的3种测试对象类型中的2种,这就是让我难过的,我不知道如何进一步排除故障。
这是我的算法:
template <typename T, typename V>
int binarySearch(T* list[], const V& searchValue,
const int firstIndex, const int lastIndex) {
int half = (firstIndex + lastIndex) /2;
// if not completly split down already
if(firstIndex != lastIndex && half != 0){
if(searchValue < *list[half]){
// lower half of array
binarySearch(list, searchValue, firstIndex, half);
}
else if(searchValue > *list[half]){
// upper half of array
binarySearch(list, searchValue, ++half, lastIndex);
}
}
else if(searchValue == *list[half]){
return half; // found it
}
return -1; // didnt find it
}
以下是我的3个对象测试用例数组:
// pointers to child class objects
Customer* customer[] = { new Customer(1002, 100000.50, "F4", "L1"),
new Customer(1004, 45000.90, "F1", "L3"),
new Customer(1003, 120000, "F3", "L2"),
new Customer(1001, 340000, "F2", "L4")
};
// pointers to child class objects
Employee* employee[] = { new Employee(102, 65000, "F2", "L1"),
new Employee(104, 45000, "F4", "L3"),
new Employee(103, 120000, "F1", "L2"),
new Employee(101, 35000, "F3", "L4")
};
// pointers to parent class objects
Person* person[] = { customer[0],
customer[3],
employee[3],
employee[0],
employee[2],
customer[1],
employee[1],
customer[2]
};
我正在调用每个对象的函数,如下所示:
// Search the customer array. -> WORKS
cout << endl
<< "Searching customer array for customer with cId = 1002: "
<< (binarySearch(customer, 1002, 0, 3) != -1? "found it." : "did not find it.")
<< endl;
// Search the employee array. -> WORKS
cout << "Searching employee array for employee with eId = 105: "
<< (binarySearch(employee, 105, 0, 3) != -1? "found it." : "did not find it.")
<< endl;
// Search the person array. -> OPERATOR ERRORS
cout << "Searching people array for person with name = 'Mickey Mouse': "
<< (binarySearch(person, "Mickey Mouse", 0, 7) != -1? "found it." : "did not find it.")
<< endl;
搜索功能在Employee和Customer对象数组上都运行良好。当尝试在Person数组上运行搜索时,我为每个使用的比较运算符得到3个错误,例如:[binary'&lt;'没有操作数采用'Person'类型的右手操作数...]
我实现了运算符,从已经提供的函数定义中对所有三个对象完全相同的方式重载。在person类中,我实现了以下重载运算符:
bool operator ==(const Person& lhs, const Person& rhs){
if(lhs.getKeyValue() == rhs.getKeyValue())
return true;
return false;
}
bool operator <(const Person& lhs, const Person& rhs){
if(lhs.getKeyValue() < rhs.getKeyValue())
return true;
return false;
}
bool operator >(const Person& lhs, const Person& rhs){
if(lhs.getKeyValue() > rhs.getKeyValue())
return true;
return false;
}
当对两个人对象进行简化测试比较时,他们比较好。即:
cout << "test person compare: " << ("mickey mouse" < person[1] ? "true" : "false");
我不确定从哪里开始,并且非常感谢方向。
编辑:添加(完整人头文件):
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <iostream>
using namespace std;
namespace P03 {
class Person {
private:
string firstName;
string lastName;
public:
/* Initializes the object.
*/
Person(const string& firstName = "na", const string& lastName = "na");
/* Getter methods retun the field value.
*/
string getFirstName() const;
string getLastName() const;
/* Returns the eid.
*/
string getKeyValue() const;
/* Returns the compound value: <lastName><space><firstName>
*/
string getName() const;
/* Setter methods, set the object.
*/
void setFirstName(const string& firstName);
void setLastName(const string& lastName);
/* Returns the object formatted as:
* Person{ firstName=<firstName>, lastName=<lastName> }
*/
virtual string toString() const;
}; // end Person
/* Displays a Person to the screen.
* Calls the toString() method.
*/
ostream& operator <<(ostream& out, const Person& person);
/* The following relational operators compare two instances of the
* Person class. The comparison is made on the compound value of:
* <lastName><space><firstName>
*/
bool operator ==(const Person& lhs, const Person& rhs);
bool operator !=(const Person& lhs, const Person& rhs);
bool operator <(const Person& lhs, const Person& rhs);
bool operator <=(const Person& lhs, const Person& rhs);
bool operator >(const Person& lhs, const Person& rhs);
bool operator >=(const Person& lhs, const Person& rhs);
} // end namespace P03
#endif
答案 0 :(得分:1)
您无法将字符串转换为人,因此这样的行会失败:
if(searchValue < *list[half]){
如果暂时将其更改为:
,您将获得更好的调试 if (T(searchValue) < *list[half]){
这是此代码可以使用的唯一方式,因为唯一可以operator<
的{{1}}在另一方需要*list[half]
。