我正在为矢量排序而苦苦挣扎。我遇到了一个早些时候在网上找到的例子,我试图自己做一些事情,但不知何故,事情进展不顺利,我在网上阅读了很多参考资料,但他们都把主函数中的“排序”功能..
我正在尝试的是看看我是否可以在函数中使用包含排序。
任务计划.h
class MissionPlan
{
friend struct stortByCiv;
private:
int sizeofarray;
int sizeofarray2;
int xcordi;
int ycordi;
public:
MissionPlan();
MissionPlan(int, int, float);
int getx();
int gety();
float getciv();
void stats();
void storedata(int, int, float);
void test();
void displayall();
void compute();
void topfives();
float civnum;
}
struct sortByCiv
{ void f(MissionPlan &p){ p.civnum = getciv();};
bool operator()(MissionPlan const &t1, MissionPlan const &t2) { return t1.civnum < t2.civnum; }
}; ;
任务计划.cpp
#include "LocationData.h"
#include "PointTwoD.h"
#include "MissionPlan.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
vector <PointTwoD> point1;//set PointTwoD object as a vector array, name of array "point1"
vector <PointTwoD> topfive;
LocationData locationdata;
PointTwoD pointtwoD;
MissionPlan missionplan;
MissionPlan::MissionPlan()
{
xcordi = 0;
ycordi = 0;
civnum = 0;
}
MissionPlan::MissionPlan(int x, int y, float civ)
{
xcordi = x;
ycordi = y;
civnum = civ;
}
int MissionPlan::getx()
{
return pointtwoD.getxcord();
}
int MissionPlan::gety()
{
return pointtwoD.getycord();
}
float MissionPlan::getciv()
{
return locationdata.getCivIndex();
}
void MissionPlan::stats()
{
string sunType;
int earth;
int moon;
float particle;
float plasma;
int xcord;
int ycord;
cout<< "X axis: ";
cin >> xcord;
pointtwoD.setxcord(xcord);
cout<< "y axis: ";
cin >> ycord;
pointtwoD.setycord(ycord);
cout << "Suntype: ";
cout.flush();//flush getline problem
cin.ignore();
getline(cin, sunType);
locationdata.setsunType(sunType);
cout << "No of Earth Like Planets: ";
cin >> earth;
locationdata.setnoOfEarthLikePlanets(earth);
cout << "No of Earth Like Moons: ";
cin >> moon;
locationdata.setnoOfEarthLikeMoons(moon);
cout << "Ave Particle Density: ";
cin >> particle;
locationdata.setaveParticulateDensity(particle);
cout << "Ave Plasma Density: ";
cin >> plasma;
locationdata.setavePlasmaDensity(plasma);
locationdata.computeCivIndex(sunType, earth, moon, particle, plasma);
missionplan.test();
missionplan.displayall();
}
void MissionPlan::test()
{
int xcord = pointtwoD.getxcord();
int ycord = pointtwoD.getycord();
float civIndex = locationdata.getCivIndex();
pointtwoD.setPointDetail(xcord, ycord, civIndex);
point1.push_back(pointtwoD);//push/store new object into array
}
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();
}
}