我对 C++ 很陌生,我的问题可能听起来很傻,但我正在研究使用向量的排序函数。
该代码能够编译和运行,但它只是没有排序。我可以知道原因吗?
任务计划.cpp
bool MissionPlan::sortByCiv(const PointTwoD &t1, const PointTwoD &t2)
{
return t1.locationdata.getCivIndex() > t2.locationdata.getCivIndex();
}
void MissionPlan::topfives()
{
topfive.assign( point1.begin(), point1.end() );
sort(topfive.begin(), topfive.end(), sortByCiv);
for(int i=0; i < 5; i++)
{
topfive.at(i).displayPointdata();
}
}
pointtwod.h
class PointTwoD
{
private:
int xcord,ycord;
float civIndex;
//LocationData locationdata;
public:
PointTwoD();
PointTwoD(int, int, float);
string toString();
void setPointDetail(int x, int y, float civ);
void displayPointdata();
void storedata(int, int, float);
//set/mutator function
void setxcord(int);
void setycord(int);
//get/accessor function
int getxcord();
int getycord();
float getcivIndex();
LocationData locationdata;
};
位置数据.h
class LocationData
{
private:
string sunType;
int noOfEarthLikePlanets, noOfEarthLikeMoons;
float aveParticulateDensity, avePlasmaDensity;
static float civIndex;
public:
LocationData(); //default constructor
LocationData(string, int, int, float, float); // no default constructor
void setLocationData(string, int, int, float, float);
void displaydata();
string toString();
//'set' mustator function
void setsunType(string);
void setnoOfEarthLikePlanets(int);
void setnoOfEarthLikeMoons(int);
void setaveParticulateDensity(float);
void setavePlasmaDensity(float);
//'get' accessor function
string getsunType();
int getnoOfEarthLikePlanets();
int getnoOfEarthLikeMoons();
float getaveParticulateDensity();
float getavePlasmaDensity();
static float getCivIndex();
static float computeCivIndex(string st, int earth, int moons, float particle, float plasma);
};
我还有一个问题.. bool myfunction (int i,int j) { return (i
我期待的结果
X: 4 Y: 9 CIV: 10
X: 1 Y: 2 CIV: 5
X: 5 Y: 4 CIV: 4
X: 6 Y: 1 CIV: 3
X: 10 Y: 6 CIV: 1
以及我从我的 prog 收到的结果,它与我输入它们的方式完全相同。
X: 10 Y: 6 CIV: 1
X: 5 Y: 4 CIV: 4
X: 4 Y: 9 CIV: 10
X: 1 Y: 2 CIV: 5
X: 6 Y: 1 CIV: 3
X: 5 Y: 9 CIV: 8