使用我自己的函数进行C ++向量排序 [英] C++ vector sorting with my own Function
问题描述
Info:
Parent Class: Vehicle
Child Class : Car & Lorry
我有一个向量,我试图按升序对它进行排序
I got a vector and i trying to sort it by ascending area
sort(myVector.begin(),myVector.end(),compareByArea);
sort(myVector.begin(),myVector.end(),compareByArea);
所以在Vehicle.h
so at the Vehicle.h
我做了
class VehicleTwoD
{
private:
//some variable
public:
bool compareByArea(const VehicleTwoD,const VehicleTwoD);
}
在Vehicle.cpp我做到了
and at Vehicle.cpp i did this
bool VehicleTwoD::compareByArea(const VehicleTwoD &a,const VehicleTwoD &b)
{
return a.getArea() < b.getArea();
}
错误是VehicleTwoD没有名为getArea的成员.
Error was VehicleTwoD has no member named getArea.
它位于我的孩子Car&货车和双人区也在孩子中宣告.
It is located in my child Car & Lorry and double area is also declare in the child .
但是问题是,如果我想通过这种方式进行排序,可以通过使用virtual来实现它,然后在child上创建相同的方法,但是将其重载并以此方式对我的区域进行排序
But the issue is if i want to sort by this way, could i achieve it by using virtual and then I create the same method at child but overload it and get my area sort by that way
有更好的方法吗?
感谢所有帮助!
Additional Info:
我在Car&货车,这只是一个简单的
I have this getArea() function at Car & Lorry which is just a simple
double Car::getArea()
{ return area;}
double Lorry::getArea()
{ return area;}
但是现在当我在向量中得到一个对象列表时,它们每个都是不同类的孩子.但是所有的都有getArea函数,我想对这个向量进行排序
But now as i got a list of object in my vector, each of them is a child of different class. but all got getArea function, i want to sort this vector which is
sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),compareArea);
//但排序不起作用.
我不知道在哪里创建compareArea,它可以针对对象2获得对象1的getArea()并返回布尔结果;
I don't know where to create compareArea which can getArea() of object 1 against object 2 and return bool result;
我尝试使用此工具在VehicleTwoD中创建
I try to create it in VehicleTwoD using this
bool VehicleTwoD::compareByArea(const VehicleTwoD &a,const VehicleTwoD &b)
{
return a.getArea() < b.getArea();
}
但是问题是VehicleTwoD没有功能getArea,它们位于子对象上.我应该如何获得此修复程序.谢谢.
But the issue is VehicleTwoD don't have the function getArea, they are located at child. how should i get this fix . Thanks..
感谢帮助:
我尝试了以下
template<typename T> bool compareByArea(const T &a, const T &b) {
return a.getArea() < b.getArea();
}
我也在.h文件中声明了它,
and i declare it at the .h file too,
public:
template<typename T>
bool compareByArea(const T,const T);
virtual double getArea();
然后在我尝试的main.cpp中
then at my main.cpp i tried
sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea);
它给我一个错误
sortVector.std :: vector中的成员compareByArea的错误请求< _TP,_ALLOC .......属于非类类型'VehicleTwoD *'
error request for member compareByArea in sortVector.std::vector<_TP, _ALLOC ....... which is of non class type 'VehicleTwoD*'
如果我这样做
sortVector.assign(vehicletwod, vehicletwod + arrayCounter);
sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea);
在此范围内,未声明compareByArea会导致错误.
I will get error compareByArea was not declared in this scope.
我该怎么办?我在main.cpp
What should i do? I declared compareByArea at VehicleTwoD which is included in main.cpp
VehicleTwoD是父类,并且我还虚拟化了double getArea(),因此当它调用getArea时,它将使用子getArea代替,因为私有变量-区域位于子区域,而函数getArea()返回区域也位于孩子
VehicleTwoD is the parent class, and i also virtual double getArea() so when it call getArea it will use the child getArea instead, because the private variable - area is at child and the function getArea() return area is also at child
我该怎么办..困惑&卡住.谢谢!
再次进行最新更新:
我正在尝试通过compareByArea对向量进行排序,其中较小的区域将被排序为最高,而较大的区域将被排序为底部.
I am trying to sort the vector by compareByArea which the smaller area will be sort at the highest while the bigger 1 will be at bottom.
问题是getArea是Car& Car的功能.Lorry(子类),我在main.cpp中创建了compareByArea
The problem is getArea is a function of Car & Lorry (child class) and i create this compareByArea at main.cpp
sortVector是vehicletwod的向量副本
sortVector is a vector copy of vehicletwod
我将价值设置到我的vehicletwod的方法是这种方式.
The way i set value into my vehicletwod is this way..
if(vehicleType=="Car")
{
vehicletwod[arrayCount] = new Car();
vehicletwod[arrayCount].setDimension();
//set area
vehicletwod[arrayCount].setArea();
cout << "Done setting the data";
}
如何实现按面积递增的排序.
How do i achieve my sorting by area ascending.
我在main.cpp中创建了此功能
I created this function at 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)
谢谢
我遇到了一个新的编译错误
I got a new compile error
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.
关于我的getArea是这个
Regarding my getArea is this
//在父母中是这种方式
//in parent is this way
double VehicleTwoD::getArea()
{
double area;
area=0.00;
return area;
}
汽车-孩子是
double Car::getArea()
{
return area;
}
推荐答案
启用模板功能:
template<typename T> bool compareByArea(const T *a, const T *b) {
return a->getArea() < b->getArea();
}
然后您可以将其传递给排序:
then you can pass it to sort:
sort(sortVector.begin(), sortVector.end(), compareByArea<VehicleTwoD>);
更新:
Vehicle.h:
Vehicle.h:
class VehicleTwoD {
...
public:
virtual double getArea() const;
};
template<typename T> bool compareByArea(const T *a, const T *b) {
return a->getArea() < b->getArea();
}
main.cpp:
#include "Vehicle.h"
#include <vector>
...
std::vector<VehicleTwoD*> sortVector;
// fill sortVector
sort(sortVector.begin(), sortVector.end(), compareByArea<VehicleTwoD>);
...
这篇关于使用我自己的函数进行C ++向量排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!