-2

我正在为矢量排序而苦苦挣扎。我遇到了一个早些时候在网上找到的例子,我试图自己做一些事情,但不知何故,事情进展不顺利,我在网上阅读了很多参考资料,但他们都把主函数中的“排序”功能..

我正在尝试的是看看我是否可以在函数中使用包含排序。

任务计划.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();
}


 }
4

2 回答 2

3

The problem right now is that you've tried to define sortByCiv nested inside of topfives. You can't define one function inside of another like that.

You have a couple of choices here. One (that works with essentially any compiler) is to define your sortByCiv outside of topfives:

bool sortByCiv(const  PointTwoD &t1, const  PointTwoD &t2)
{
    return t1.getciv < t2.getciv;
}

void MissionPlan::topfives() { 
    topfive.assign( point1.begin(), point1.end() ); 

    sort(topfive.begin(), topfive.end(), sortByCiv);
    // ...
 }

Another possibility (that is sometimes preferred is to define your comparison as a class or struct that overloads operator():

struct sortByCiv {
    bool operator()(PointTwoD const &t1, PointTwoD const &t2) { 
        return t1.getciv < t2.getciv;
    }
};

When you use this, you need to add a pair of parens to the name to create an instance of the class that will be passed when sorting:

sort(topfive.begin(), topfive.end(), sortByCiv());

The final possibility (that only works with relatively recent compilers is to use a "lambda" expression:

void MissionPlan::topfives() { 
    topfive.assign( point1.begin(), point1.end() ); 

    sort(topfive.begin(), topfive.end(),
         [](PointTD const &t1, PointTwoD const &t2) {return t1.getciv < t2.getciv; });
    // ...
 }

If your compiler supports it, the lambda expression is usually preferred, as it lets you specify the sorting criteria "in place".

Although it's not related to the sorting, your code to display the results looks somewhat borked as well. I think what you want is something closer to:

for(int i=0; i < 5; i++)
    topfive.at(i).displayPointdata();
于 2013-10-25T14:23:58.220 回答
1

从另一个函数中调用 没有错。std::sort尝试在另一个函数中声明一个函数有问题(C++ 不支持嵌套函数)。因此,以下代码将无法编译:

oid MissionPlan::topfives()
{   
    bool sortByCiv(const PointTwoD &t1, const  PointTwoD  &t2);

    // ...

    bool sortByCiv(const  PointTwoD &t1, const  PointTwoD &t2)
    {
        return t1.getciv < t2.getciv;
    }
}

您可以sortByCiv在函数外部声明topfives(作为类成员或作为独立函数 - 尽管它需要是 afriend才能访问私有成员数据),或者使用 lambda 函数(如果您可以使用 C++ 11 个特征)为您的比较谓词。

于 2013-10-25T14:21:37.397 回答