需要帮助比较属于类的数组

Need help comparing an array that is part of a class

本文关键字:数组 属于 比较 帮助      更新时间:2023-10-16

我需要帮助比较存储在类中的数组。 它必须是一个成员函数,并包含我的教授设置的参数。 我在调用函数以及比较它时遇到问题。

我包含此函数是为了展示我如何能够比较公司名称并在它们相等时返回 true 或 false。 然后,我循环浏览以在列表中找到所有具有相同品牌的打印功能中的汽车。

bool AutoVehicleType::isMadeByCompany(string companyName){
if (companyName.compare(getAutoBrand())==0) 
return true;
else return false;
}
void printAllVehiclesMadeBy(AutoVehicleType vehicles[], int 
noOfVehicles,string brand){ 
for(int i=0; i<noOfVehicles;i++){
if(vehicles[i].isMadeByCompany(brand)){ 
vehicles[i].printVehicleInfo();
cout << endl << "---------------" << endl;
//loop that prints if vehicles made by the same brand
}
}
}

这是我正在尝试制作的函数。 我需要帮助比较函数以及从类调用成员函数。 我正在尝试使其类似于我制作的 bove 但遇到了问题,因为它是一个数组,不知道如何使用老师设置的参数进行比较。

bool AutoVehicleType::HaveIdenticalAmenities(AutoVehicleType 
otherVehicles){
if(vehicles[i].amenities==otherVehicles.amenities)
return true;
else return false;
}
void printVehiclesWithIdenticalAmenities(AutoVehicleType 
vehicles[], int noOfVehicles){
for (int i = 0; i < noOfVehicles; i++)
{
if(vehicles[i].HaveIdenticalAmenities(otherVehicles)) 
{
cout<<"Vehicles "<<Vehicles[i].getNumPlate()<<" 
and "<< otherVehicles[].getNumPlate()<<" Have 
Identical set of Amenites";
}
}
}

在摆弄这些函数时,我收到很多未声明的标识符错误,因为我猜我不知道如何正确调用它们。

您无法将数组与==进行比较(不使用数组的众多原因之一)。您需要手动进行比较,即检查数组大小是否相同,然后逐个比较每个元素。对于在方法中使用载具数组,您也有一些奇怪的困惑。HaveIdenticalAmenities方法只是比较一对车辆,而不是两个车辆阵列。

bool AutoVehicleType::HaveIdenticalAmenities(AutoVehicleType otherVehicle) {
if (numOfAmenities != otherVehicle.numOfAmenities)
return false;
for (int i = 0; i < numOfAmenities; ++i)
if (amenities[i] != otherVehicle.amenities[i])
return false;
return true;
}

numOfAmenities不在你上面发布的代码中,但我似乎记得你在之前的(现已删除的)帖子中有过。

调用该方法的方式也很混乱。我猜目的是在vehicles阵列中找到所有具有相同弹药的车辆对?如果是这样,那就是这个

for (int i = 0; i < noOfVehicles; i++)
{
for (int j = i + 1; j < noOfVehicles; j++)
{
if (vehicles[i].HaveIdenticalAmenities(vehicles[j])) 
{
cout<<"Vehicles "<<vehicles[i].getNumPlate()<<" and "<< 
vehicles[j].getNumPlate()<<" Have Identical set of Amenites";
}
}
}

完全未经测试的代码。

对于以下解释,我按如下方式存根您的类:

struct Amenities
{
int x = 0;
friend bool operator==(const Amenities& amenities1, const Amenities& amenities2)
{
// How you compare your Amenities goes here.
return amenities1.x == amenities2.x;
}
};
struct AutoVehicleType
{
std::string GetAutoBrand() const
{
return "";
}
bool IsMadeByCompany(std::string companyName) const;
void PrintVehicleInfo() const
{
}
bool HaveIdenticalAmentities(AutoVehicleType otherVehicle) const;
int GetNumPlate()
{
return 1;
}
Amenities amenities = {};
};
bool AutoVehicleType::IsMadeByCompany(std::string companyName) const
{
return (companyName == this->GetAutoBrand());
}
bool AutoVehicleType::HaveIdenticalAmentities(AutoVehicleType otherVehicle) const
{
return (this->amenities == otherVehicle.amenities);
}

我的第一个建议是使用 std::vector 而不是数组,如果您选择这样做,以下内容应该是您的问题的体面近似值:

void PrintAllVehiclesMadeByBrand(std::vector<AutoVehicleType> vehicles, std::string brand)
{
for (const auto& vehicle : vehicles)
{
if (vehicle.IsMadeByCompany(brand))
{
vehicle.PrintVehicleInfo();
}
}
}
void PrintVehiclesWithIdenticalAmenities(std::vector<AutoVehicleType> vehicles)
{
for (size_t x = 0; x < vehicles.size(); x++)
{
for (size_t y = x + 1; y < vehicles.size(); y++)
{
if (vehicles.at(x).HaveIdenticalAmentities(vehicles.at(y)))
{
std::cout << "Vehicles " << vehicles.at(x).GetNumPlate() << " and " << vehicles.at(y).GetNumPlate << " have identical amenities." << std::endl;
}
}
}
}

对于您的问题,我们只需将向量的每个元素与其之后的每个元素进行比较即可。

但是,由于这是一个家庭作业,我认为您可能需要使用数组(尽管我真的不建议这样做,除非这是在某个地方进行的),实现可能如下所示:

template <typename T, size_t N>
void PrintAllVehiclesMadeByBrand(T(&vehicles)[N], std::string brand)
{
for (size_t x = 0; x < N; x++)
{
if (vehicles[x].IsMadeByCompany(brand))
{
vehicles[x].PrintVehicleInfo();
}
}
}
template <typename T, size_t N>
void PrintVehiclesWithIdenticalAmenities(T(&vehicles)[N])
{
for (size_t x = 0; x < N; x++)
{
for (size_t y = x + 1; y < N; y++)
{
if (vehicles[x].HaveIdenticalAmenities(vehicles[y]))
{
std::cout << "Vehicles " << vehicles[x].GetNumPlate() << " and " << vehicles[y].GetNumPlate() << " have identical amenities." << std::endl;
}
}
}
}

其中 T(&vehicles)[N] 是传递数组大小的快捷方式,而不必使用单独的参数(尽管如果您对使用该技术感到不舒服,只需继续传递数组大小即可)。

在你的例子中,你有一个其他车辆,我认为这是你正在处理的问题(从哪里得到它)。在我提供的示例中,我们将每个元素与它前面的每个元素进行比较。因此,如果您的数组具有以下形式的元素:

1 2 3 4 1

它将比较 1 到 2,然后是 1 到 3、1 到 4 和 1 到 1,它将打印您的陈述。然后循环计数器 x 将递增,它将比较 2 到 3(我们可以安全地跳过比较 2 到 1,因为我们已经在之前的 x 迭代器中比较了 1 到 2)。因此,到最后,您将每个元素相互比较。

似乎您可能遇到的另一个问题是如何比较两种自定义类型。为此,我建议进一步研究运算符重载: 运算符重载 (请记住,在我使用的存根中,将操作员作为朋友而不是成员是一个成语)。

编辑:这并不完全是类型安全的,因为车辆是一个对象。因此,如果您选择使用模板化数组,则static_assert检查 T 是车辆还是尾随返回类型来实现 SFINAE 将是谨慎的。