Reputation: 33
I'm very new to C++ and my question may sound silly but i'm working on a sort function using vectors.
the code is able to compile and run but it's just not sorting. may I know the reason why?
missionplan.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;
};
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);
};
I've got another question.. bool myfunction (int i,int j) { return (i
the result I'm expecting
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
and the result i receive from my prog, it's exactly the same seq as how i input them.
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
Upvotes: 1
Views: 1069
Reputation: 66224
Unless I'm severely reading your code incorrectly, you are calling the wrong getCivIndex()
. The one you're calling is using a static class variable. This means ALL instances of LocationData
(including the internal instances in your PostTwoD
objects), share the same static variable, and therefore have the same identical values. In other words, your comparator always returns false because N > N is never true. And thus your sort simply doesn't work.
I believe you want be using the getcivIndex()
instance member of PointTwoD
; not the static class member of LocationData
Several things should change for this to be "fixed"
First, change this:
float getcivIndex();
To this: (reasons will be apparent in a minute)
float getcivIndex() const;
You will also have to change the implementation of this function, wherever it is, to add const
to its definition as well.
Next, change this:
bool MissionPlan::sortByCiv(const PointTwoD &t1, const PointTwoD &t2)
{
return t1.locationdata.getCivIndex() > t2.locationdata.getCivIndex();
}
To this:
bool MissionPlan::sortByCiv(const PointTwoD &t1, const PointTwoD &t2)
{
// use object instance-member. note: this is why getcivIndex()
// had to be made const. The t1 and t2 objects are const and as
// such only const-member-functions are callable.
return t1.getcivIndex() > t2.getcivIndex();
}
And I suggest you see my comment about using std::partial_sort
rather than a full-blown sort if all you want is the "top N" results in a sequence.
Upvotes: 2