1

我无法让蛮力算法正常工作。正如您从下面的代码中看到的那样,我正在尝试评估结构向量并找到对一系列定时事件进行排序的最有效方法。这是我将其对象放置在“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;
}
4

1 回答 1

2

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

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

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

std::next_permuation使用良好的排序来生成排列。所以它需要类型的参数,它们本身定义了一个顺序,因此算法总是会终止,一个一致的顺序(如果 a<(b) 和 b<(a) 是可能的,那么顺序是不一致的,无论如何这通常是不可取的)。

这就是 chris 所指的,而你在 C++ 中使用像你这样的结构类型(其顺序尚未由基类定义)的方式是bool operator<(...在结构中覆盖。

因为您只需要排列生成的排序,所以任何旧的排序都可以,只要:一切都是有序的并且排序是一致的,如上所述。

在此处再次查看您的代码,并使用该覆盖和几个unsigned ints 在它们应该在的位置,注意:

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

最好的。

于 2013-10-13T16:48:39.200 回答