通过'next_permutation'算法发送struct对象的向量无法构建

时间:2013-10-13 02:36:11

标签: c++ algorithm struct

我无法使用强力算法。从下面的代码中可以看出,我正在尝试评估结构的向量,并找到订购一系列定时事件的最有效方法。这是我的简单结构布局,其中的对象放在'part1Vec'向量中:

struct person
{
    float swim;
    float bike;
    float run;

    float bikeRun;

    person();

    person(float swim, float bike, float run)
    {
        this->swim = swim;
        this->bike = bike;
        this->run = run;

        this->bikeRun = bike + run;
    };
};

然而,当我编译时,我在算法类中得到一个错误,我认为这个错误可以追溯到这一行:

while (next_permutation(part1Vec.begin(), part1Vec.end()))

我得到的错误信息是:

//Invalid operands to binary expression ('const person' and 'const person')

我相信我已正确实现了next_permutation功能,但我不明白为什么它会给我这个错误。任何帮助将不胜感激,谢谢。这是我的完整代码:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct person
{
    float swim;
    float bike;
    float run;

    float bikeRun;

    person();

    person(float swim, float bike, float run)
    {
        this->swim = swim;
        this->bike = bike;
        this->run = run;

        this->bikeRun = bike + run;
    };
};

//function declarations and global variables
vector<person> part1Vec;
vector<person> highestVec;

float highestRating;
float tempRating;

float rating(vector<person> vector);

int main()
{
    //PART 1

    //make objects
    person one(20, 25, 20), two(35, 20, 15), three(40, 20, 30);

    //insert into vector
    part1Vec.push_back(one);
    part1Vec.push_back(two);
    part1Vec.push_back(three);

    cout << "_________swim__bike__run__" << endl;

    for (int i=0; i<part1Vec.size(); i++)
    {
        cout << "Vector #" << i+1 << ": "
        << part1Vec[i].swim << "  "
        << part1Vec[i].bike << "  "
        << part1Vec[i].run;
    }

    cout << endl << "Calculating..." << endl;

    //Next permutation function
    while (next_permutation(part1Vec.begin(), part1Vec.end())) //Invalid operands to binary expression ('const person' and 'const person')
    {
        //initialize highestVec
        if (highestVec.size() == 0)
        {
            highestRating = rating(part1Vec);
            highestVec = part1Vec;
        }
        //if Highest Rating is less than current permutation, update.
        else if (highestRating < (tempRating = rating(part1Vec)) )
        {
            highestVec = part1Vec;
            highestRating = tempRating;
        }
    }

    cout << "Best Solution:" << endl;
    for (int i=0; i<part1Vec.size(); i++)
    {
        cout << "Vector #" << i+1 << ": "
        << highestVec[i].swim << "  "
        << highestVec[i].bike << "  "
        << highestVec[i].run;
    }

    cout << endl << "Rating: " << highestRating << endl;

    return 0;
}

float rating(vector<person> thisVector)
{
    float rating = 0;
    float swimSum = 0;

    for (int i=0; i<thisVector.size()-1; i++)
    {
        swimSum += thisVector[i].swim;

        if (rating < swimSum + thisVector[i].bikeRun)
            rating = swimSum + thisVector[i].bikeRun;
    }

    return rating;
}

1 个答案:

答案 0 :(得分:2)

凯尔,你明白克里斯的意思吗?

我希望克里斯不会介意,但我会冒昧地详细说明以防万一。

首先,说明你正在使用什么编译器。一个原因是他们提供不同的错误消息。在这种情况下,Invalid operands to binary expression ('const person' and 'const person')有点有用,但没有尽可能有用。例如,如果我通过gcc运行它,它可能会告诉我更像std::next_permuation正在寻找未定义的运算符。

std::next_permuation使用井顺序生成排列。所以它需要带有自己定义的顺序的类型的参数,因此算法将始终终止,一致顺序(如果&lt;(b)和b&lt;(a)是一个排序是不一致的可能,这通常是不可取的。)

这就是克里斯所指的内容,以及你在C ++中使用类似结构类型的方式来实现它的方式,这种方式类型尚未由基类定义,是覆盖结构中的bool operator<(...

因为你只需要为你的排列生成订购,所以任何旧的排序都可以做到:一切都是有序的,排序是一致的,如上所述。

使用该覆盖再次查看here代码,并在其中显示一些unsigned int,注意:

    bool operator<(const person& rhs) const {
    return (this->swim + this->bike + this->run) < (rhs.swim + rhs.bike + rhs.run);
}

最佳。