C++我自己的函数进行向量排序


Info:
Parent Class: Vehicle
Child Class : Car & Lorry

我得到了一个向量,我试图按升序区域对其进行排序

sort(myVector.begin(),myVector.end(),compareByArea);

所以在车辆上。

我做了

class VehicleTwoD
{
private:
//some variable
public:
bool compareByArea(const VehicleTwoD,const VehicleTwoD);
}

在车辆.cpp我做到了

bool VehicleTwoD::compareByArea(const VehicleTwoD &a,const VehicleTwoD &b)
{
return a.getArea() < b.getArea();
}

错误是 VehicleTwoD 没有名为 getArea 的成员。

它位于我的孩子汽车和卡车和双区域也在孩子声明。

但问题是,如果我想通过这种方式进行排序,我可以通过使用虚拟来实现它,然后我在 child 上创建相同的方法,但重载它并让我的区域按这种方式排序

有没有更好的方法可以做到这一点.

感谢大家的帮助!

Additional Info:

我在Car&Lorry有这个getArea()函数,它只是一个简单的

double Car::getArea()
{ return area;}
double Lorry::getArea()
{ return area;}

但是现在当我在我的向量中得到一个对象列表时,它们中的每一个都是不同类的子项。 但是都有getArea函数,我想对这个向量进行排序,它是

sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),compareArea);

//但排序不起作用。

我不知道在哪里创建比较区域,它可以将对象 1 的Area()与对象 2 进行比较并返回布尔结果;

我尝试使用它在 VehicleTwoD 中创建它

bool VehicleTwoD::compareByArea(const VehicleTwoD &a,const VehicleTwoD &b)
{
return a.getArea() < b.getArea();
}

但问题是 VehicleTwoD 没有函数 getArea,它们位于孩子。 我应该如何获得此修复程序.谢谢。。

感谢帮助:

我尝试了以下方法

template<typename T> bool compareByArea(const T &a, const T &b) {
return a.getArea() < b.getArea();
}

我也在 .h 文件中声明它,

public:
template<typename T>
bool compareByArea(const T,const T);
virtual double getArea();

然后在我的主要.cpp我尝试了

sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea);

它给了我一个错误

在sortVector.std::vector<_TP中对成员compareByArea的错误请求,_ALLOC.......属于非类类型"车辆二D*">

如果我这样做

sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea);

我会得到错误比较区域没有在此范围内声明。

我该怎么办?我在 VehicleTwoD 上声明了 compareByArea,它包含在 main 中.cpp

VehicleTwoD 是父类,我也虚拟双 getArea(),所以当它调用 getArea 时,它将改用子 getArea,因为私有变量 - area 在子级,函数 getArea() 返回区也在子

我该怎么办......困惑和卡住了。 谢谢!

再次更新:

我正在尝试通过 compareByArea 对向量进行排序,较小的区域将在最高处排序,而较大的 1 将在底部排序。

问题是getArea是Car & Lorry(儿童类)的一个函数,我在main创建了这个compareByArea.cpp

sortVector 是 vehicletwod 的向量副本

我为我的车辆设置值的方式是这样的..

if(vehicleType=="Car")
{
vehicletwod[arrayCount] = new Car();
vehicletwod[arrayCount].setDimension();
//set area
vehicletwod[arrayCount].setArea();
cout << "Done setting the data";
}

如何实现按区域升序排序。

我在main创建了这个函数.cpp

template<typename T> bool compareByArea(const T &a, const T &b) {
return a.getArea() < b.getArea();
}

然后我做了这个

sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),compareByArea);

编译错误: 编译错误:

no matching function for call to 'sort(std::vector<VehicleTwoD*>::iterator, std::vector<VehicleTwoD*>::iterator, <unresolved overloaded function type>)'
note: template<class _RAIter> void std::sort (_RAIter, _RAIter)
note: template<class _RAIter, class _Compare> void std::sort(_RAiter, _RAIter, _Compare)

谢谢

我遇到了一个新的编译错误

error: passing 'const VehicleTwoD' as 'this' argument of 'virtual double VehicleTwoD::getArea()' discards qualifers.
error: passing 'const VehicleTwoD' as 'this' argument of 'virtual double VehicleTwoD::getArea()' discards qualifers.

关于我的得到区域是这个

父级是这样的

double VehicleTwoD::getArea()
{
double area;
area=0.00;
return area;
}

汽车 - 儿童是

double Car::getArea()
{
return area;
}

如果VehicleTwoD::getArea是公共的,那么最简单的方法是使compareByArea成为一个自由函数而不是一个VehicleTwoD的方法:

bool compareByArea(const VehicleTwoD &a,const VehicleTwoD &b) {
return a.getArea() < b.getArea();
}

然后,您可以轻松地使用它进行排序:

std::sort(myVector.begin(), myVector.end(), compareByArea);

在 C++11 中,您可以使用 lambda 表达式:

std::sort(std::begin(myVector), std::end(MyVector),
[](const VehicleTwoD &a, const VehicleTwoD &b) {
return a.getArea() < b.getArea();
});

如果getArea不是公开的,你仍然可以将compareByArea用作自由函数,但将其声明为类的好友。

更新:一个完整的工作示例:

#include <algorithm>
#include <iostream>
#include <vector>
class VehicleTwoD {
public:
VehicleTwoD(double area) : m_area(area) {}
double getArea() const { return m_area; }
private:
double m_area;
};
bool compareByArea(const VehicleTwoD &a,const VehicleTwoD &b) {
return a.getArea() < b.getArea();
}
int main() {
std::vector<VehicleTwoD> vehicles;
vehicles.push_back(VehicleTwoD(3.0));
vehicles.push_back(VehicleTwoD(1.0));
vehicles.push_back(VehicleTwoD(2.0));
std::sort(vehicles.begin(), vehicles.end(), compareByArea);
for (auto it = vehicles.begin(); it != vehicles.end(); ++it) {
std::cout << it->getArea() << std::endl;
}
return 0;
}

制作一个模板函数:

template<typename T> bool compareByArea(const T *a, const T *b) {
return a->getArea() < b->getArea();
}

然后你可以把它传递给排序:

sort(sortVector.begin(), sortVector.end(), compareByArea<VehicleTwoD>);

更新:

车辆.h:

class VehicleTwoD {
...
public:
virtual double getArea() const;
};
template<typename T> bool compareByArea(const T *a, const T *b) {
return a->getArea() < b->getArea();
}

主.cpp:

#include "Vehicle.h"
#include <vector>
...
std::vector<VehicleTwoD*> sortVector;
// fill sortVector
sort(sortVector.begin(), sortVector.end(), compareByArea<VehicleTwoD>);
...

最新更新